File size: 3,530 Bytes
ee0b822
 
 
265212d
 
ee0b822
265212d
 
ee0b822
 
 
 
 
265212d
ee0b822
 
 
265212d
ee0b822
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265212d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee0b822
265212d
 
 
 
 
 
 
 
 
 
ee0b822
265212d
ee0b822
265212d
ee0b822
265212d
 
ee0b822
 
265212d
 
 
ee0b822
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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()