Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image, ImageDraw
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
def
|
| 7 |
# Görüntüyü gri tonlamaya çevir
|
| 8 |
img = image.convert("L")
|
| 9 |
img_array = np.array(img)
|
| 10 |
height, width = img_array.shape
|
| 11 |
|
| 12 |
-
# Çıktı görüntüsü için
|
| 13 |
output = Image.new("L", (width, height), 255)
|
| 14 |
draw = ImageDraw.Draw(output)
|
| 15 |
|
| 16 |
-
# Şekil
|
| 17 |
for y in range(0, height, sample):
|
| 18 |
for x in range(0, width, sample):
|
| 19 |
-
# Piksel yoğunluğunu hesapla (0-255)
|
| 20 |
intensity = img_array[y, x]
|
| 21 |
-
# Yoğunluğa göre şeklin boyutunu ölçekle
|
| 22 |
radius = (255 - intensity) / 255 * (sample / 2)
|
| 23 |
|
| 24 |
-
# Şekil çizimi
|
| 25 |
if shape == "circle":
|
| 26 |
draw.ellipse(
|
| 27 |
[x - radius, y - radius, x + radius, y + radius],
|
|
@@ -40,23 +39,70 @@ def halftone_effect(image, shape="circle", sample=10):
|
|
| 40 |
|
| 41 |
return output
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
def
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
return None
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
#
|
| 51 |
interface = gr.Interface(
|
| 52 |
-
fn=
|
| 53 |
inputs=[
|
| 54 |
-
gr.
|
|
|
|
| 55 |
gr.Dropdown(choices=["circle", "square", "triangle"], label="Şekil Seçimi", value="circle")
|
| 56 |
],
|
| 57 |
-
outputs=gr.
|
| 58 |
-
title="Halftone Efekt Uygulaması",
|
| 59 |
-
description="Bir görüntü yükleyin ve halftone efekti için bir şekil seçin."
|
| 60 |
)
|
| 61 |
|
| 62 |
# Uygulamayı başlat
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image, ImageDraw
|
| 3 |
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Tek bir görüntüye halftone efekti uygulama
|
| 8 |
+
def halftone_image(image, shape="circle", sample=10):
|
| 9 |
# Görüntüyü gri tonlamaya çevir
|
| 10 |
img = image.convert("L")
|
| 11 |
img_array = np.array(img)
|
| 12 |
height, width = img_array.shape
|
| 13 |
|
| 14 |
+
# Çıktı görüntüsü için tuval
|
| 15 |
output = Image.new("L", (width, height), 255)
|
| 16 |
draw = ImageDraw.Draw(output)
|
| 17 |
|
| 18 |
+
# Şekil çizimi
|
| 19 |
for y in range(0, height, sample):
|
| 20 |
for x in range(0, width, sample):
|
|
|
|
| 21 |
intensity = img_array[y, x]
|
|
|
|
| 22 |
radius = (255 - intensity) / 255 * (sample / 2)
|
| 23 |
|
|
|
|
| 24 |
if shape == "circle":
|
| 25 |
draw.ellipse(
|
| 26 |
[x - radius, y - radius, x + radius, y + radius],
|
|
|
|
| 39 |
|
| 40 |
return output
|
| 41 |
|
| 42 |
+
# Videoya halftone efekti uygulama
|
| 43 |
+
def halftone_video(video_path, shape="circle", sample=10):
|
| 44 |
+
# Video dosyasını aç
|
| 45 |
+
cap = cv2.VideoCapture(video_path)
|
| 46 |
+
if not cap.isOpened():
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
# Video özelliklerini al
|
| 50 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 51 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 52 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 53 |
+
|
| 54 |
+
# Çıktı videosu için geçici bir dosya
|
| 55 |
+
output_path = "output_halftone_video.mp4"
|
| 56 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 57 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height), isColor=False)
|
| 58 |
+
|
| 59 |
+
while cap.isOpened():
|
| 60 |
+
ret, frame = cap.read()
|
| 61 |
+
if not ret:
|
| 62 |
+
break
|
| 63 |
+
|
| 64 |
+
# Kareyi gri tonlamaya çevir ve PIL formatına dönüştür
|
| 65 |
+
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 66 |
+
pil_frame = Image.fromarray(gray_frame)
|
| 67 |
+
|
| 68 |
+
# Halftone efektini uygula
|
| 69 |
+
processed_frame = halftone_image(pil_frame, shape, sample)
|
| 70 |
+
processed_array = np.array(processed_frame)
|
| 71 |
+
|
| 72 |
+
# Çıktıya yaz
|
| 73 |
+
out.write(processed_array)
|
| 74 |
+
|
| 75 |
+
cap.release()
|
| 76 |
+
out.release()
|
| 77 |
+
|
| 78 |
+
return output_path
|
| 79 |
+
|
| 80 |
+
# Gradio arayüz fonksiyonu
|
| 81 |
+
def process_media(input_media, media_type, shape):
|
| 82 |
+
if input_media is None:
|
| 83 |
return None
|
| 84 |
+
|
| 85 |
+
if media_type == "Image":
|
| 86 |
+
# Görüntü işleme
|
| 87 |
+
img = Image.open(input_media)
|
| 88 |
+
result = halftone_image(img, shape=shape)
|
| 89 |
+
return result
|
| 90 |
+
elif media_type == "Video":
|
| 91 |
+
# Video işleme
|
| 92 |
+
result_path = halftone_video(input_media, shape=shape)
|
| 93 |
+
return result_path
|
| 94 |
|
| 95 |
+
# Gradio arayüzü
|
| 96 |
interface = gr.Interface(
|
| 97 |
+
fn=process_media,
|
| 98 |
inputs=[
|
| 99 |
+
gr.File(label="Bir görüntü veya video yükle"),
|
| 100 |
+
gr.Radio(choices=["Image", "Video"], label="Medya Türü", value="Image"),
|
| 101 |
gr.Dropdown(choices=["circle", "square", "triangle"], label="Şekil Seçimi", value="circle")
|
| 102 |
],
|
| 103 |
+
outputs=gr.File(label="İşlenmiş Medya"),
|
| 104 |
+
title="Görüntü ve Video için Halftone Efekt Uygulaması",
|
| 105 |
+
description="Bir görüntü veya video yükleyin, medya türünü seçin ve halftone efekti için bir şekil seçin."
|
| 106 |
)
|
| 107 |
|
| 108 |
# Uygulamayı başlat
|