Spaces:
Sleeping
Sleeping
import gradio as gr | |
from PIL import Image, ImageDraw | |
import numpy as np | |
import cv2 | |
import os | |
# Tek bir görüntüye halftone efekti uygulama | |
def halftone_image(image, shape="circle", sample=10): | |
# Görüntüyü gri tonlamaya çevir | |
img = image.convert("L") | |
img_array = np.array(img) | |
height, width = img_array.shape | |
# Çıktı görüntüsü için tuval | |
output = Image.new("L", (width, height), 255) | |
draw = ImageDraw.Draw(output) | |
# Şekil çizimi | |
for y in range(0, height, sample): | |
for x in range(0, width, sample): | |
intensity = img_array[y, x] | |
radius = (255 - intensity) / 255 * (sample / 2) | |
if shape == "circle": | |
draw.ellipse( | |
[x - radius, y - radius, x + radius, y + radius], | |
fill=0 | |
) | |
elif shape == "square": | |
draw.rectangle( | |
[x - radius, y - radius, x + radius, y + radius], | |
fill=0 | |
) | |
elif shape == "triangle": | |
draw.polygon( | |
[(x, y - radius), (x - radius, y + radius), (x + radius, y + radius)], | |
fill=0 | |
) | |
return output | |
# Videoya halftone efekti uygulama | |
def halftone_video(video_path, shape="circle", sample=10): | |
# Video dosyasını aç | |
cap = cv2.VideoCapture(video_path) | |
if not cap.isOpened(): | |
return None | |
# Video özelliklerini al | |
fps = cap.get(cv2.CAP_PROP_FPS) | |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
# Çıktı videosu için geçici bir dosya | |
output_path = "output_halftone_video.mp4" | |
fourcc = cv2.VideoWriter_fourcc(*"mp4v") | |
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height), isColor=False) | |
while cap.isOpened(): | |
ret, frame = cap.read() | |
if not ret: | |
break | |
# Kareyi gri tonlamaya çevir ve PIL formatına dönüştür | |
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
pil_frame = Image.fromarray(gray_frame) | |
# Halftone efektini uygula | |
processed_frame = halftone_image(pil_frame, shape, sample) | |
processed_array = np.array(processed_frame) | |
# Çıktıya yaz | |
out.write(processed_array) | |
cap.release() | |
out.release() | |
return output_path | |
# Gradio arayüz fonksiyonu | |
def process_media(input_media, media_type, shape): | |
if input_media is None: | |
return None | |
if media_type == "Image": | |
# Görüntü işleme | |
img = Image.open(input_media) | |
result = halftone_image(img, shape=shape) | |
return result | |
elif media_type == "Video": | |
# Video işleme | |
result_path = halftone_video(input_media, shape=shape) | |
return result_path | |
# Gradio arayüzü | |
interface = gr.Interface( | |
fn=process_media, | |
inputs=[ | |
gr.File(label="Bir görüntü veya video yükle"), | |
gr.Radio(choices=["Image", "Video"], label="Medya Türü", value="Image"), | |
gr.Dropdown(choices=["circle", "square", "triangle"], label="Şekil Seçimi", value="circle") | |
], | |
outputs=gr.File(label="İşlenmiş Medya"), | |
title="Görüntü ve Video için Halftone Efekt Uygulaması", | |
description="Bir görüntü veya video yükleyin, medya türünü seçin ve halftone efekti için bir şekil seçin." | |
) | |
# Uygulamayı başlat | |
interface.launch() |