File size: 2,368 Bytes
87c889e
 
 
650c1f2
 
87c889e
 
 
650c1f2
 
87c889e
 
 
 
650c1f2
 
 
 
 
 
 
 
 
 
87c889e
650c1f2
 
87c889e
650c1f2
 
 
 
87c889e
650c1f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87c889e
 
 
650c1f2
 
87c889e
 
 
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
import io
import logging
from datetime import datetime
from PIL import Image, ImageDraw, ImageOps
import numpy as np

logger = logging.getLogger(__name__)

def apply_halftone(image, dot_size=10):
    """Görüntüye halftone efekti uygular"""
    start_time = datetime.now()
    logger.info(f"Applying halftone effect - {start_time}")
    
    try:
        # Eğer image bir dosya verisi (bytes) ise Image'a dönüştür
        if isinstance(image, bytes):
            image = Image.open(io.BytesIO(image))
        
        # Görüntüyü gri tonlamalı yap
        if image.mode != 'L':
            image = image.convert('L')
        
        # Orijinal boyutları sakla
        orig_width, orig_height = image.size
        
        # Yeniden boyutlandırma faktörü
        resize_factor = dot_size / 5
        
        # Geçici olarak küçült
        small_width = int(orig_width / resize_factor)
        small_height = int(orig_height / resize_factor)
        small_image = image.resize((small_width, small_height), Image.BILINEAR)
        
        # Yeni görüntü oluştur
        output_image = Image.new('L', (orig_width, orig_height), 255)
        draw = ImageDraw.Draw(output_image)
        
        # Halftone noktalarını çiz
        for y in range(small_height):
            for x in range(small_width):
                # Piksel parlaklığını al
                brightness = 255 - small_image.getpixel((x, y))
                
                # Nokta boyutunu hesapla
                radius = int((brightness / 255) * (dot_size / 2))
                
                # Noktayı çiz
                if radius > 0:
                    center_x = int((x * resize_factor) + (resize_factor / 2))
                    center_y = int((y * resize_factor) + (resize_factor / 2))
                    bbox = [
                        center_x - radius,
                        center_y - radius,
                        center_x + radius,
                        center_y + radius
                    ]
                    draw.ellipse(bbox, fill=0)
        
        duration = (datetime.now() - start_time).total_seconds()
        logger.info(f"Successfully processed. Duration: {duration} seconds")
        return output_image
        
    except Exception as e:
        logger.error(f"Halftone processing error: {str(e)}", exc_info=True)
        raise