Update demo_generate/halftone_method.py
Browse files- demo_generate/halftone_method.py +46 -47
demo_generate/halftone_method.py
CHANGED
|
@@ -1,66 +1,65 @@
|
|
| 1 |
-
from PIL import Image, ImageDraw
|
| 2 |
import io
|
| 3 |
import logging
|
| 4 |
from datetime import datetime
|
|
|
|
|
|
|
| 5 |
|
| 6 |
logger = logging.getLogger(__name__)
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
"""
|
| 10 |
-
Apply a halftone effect to the input image.
|
| 11 |
-
"""
|
| 12 |
-
grayscale = img.convert('L')
|
| 13 |
-
width, height = grayscale.size
|
| 14 |
-
|
| 15 |
-
halftone_image = Image.new('L', (width, height), color=255)
|
| 16 |
-
draw = ImageDraw.Draw(halftone_image)
|
| 17 |
-
|
| 18 |
-
for x in range(0, width, dot_size):
|
| 19 |
-
for y in range(0, height, dot_size):
|
| 20 |
-
block_width = min(dot_size, width - x)
|
| 21 |
-
block_height = min(dot_size, height - y)
|
| 22 |
-
|
| 23 |
-
region = grayscale.crop((x, y, x + block_width, y + block_height))
|
| 24 |
-
avg_brightness = sum(region.getdata()) / (block_width * block_height)
|
| 25 |
-
|
| 26 |
-
max_radius = dot_size // 2
|
| 27 |
-
radius = int((255 - avg_brightness) / 255 * max_radius)
|
| 28 |
-
radius = max(0, min(radius, max_radius))
|
| 29 |
-
|
| 30 |
-
center_x = x + dot_size // 2
|
| 31 |
-
center_y = y + dot_size // 2
|
| 32 |
-
draw.ellipse(
|
| 33 |
-
(
|
| 34 |
-
center_x - radius,
|
| 35 |
-
center_y - radius,
|
| 36 |
-
center_x + radius,
|
| 37 |
-
center_y + radius
|
| 38 |
-
),
|
| 39 |
-
fill=0
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
return halftone_image
|
| 43 |
-
|
| 44 |
-
def apply_halftone(image_data, dot_size=10):
|
| 45 |
start_time = datetime.now()
|
| 46 |
logger.info(f"Applying halftone effect - {start_time}")
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
output_image.
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
duration = (datetime.now() - start_time).total_seconds()
|
| 62 |
logger.info(f"Successfully processed. Duration: {duration} seconds")
|
| 63 |
-
return
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
logger.error(f"Halftone processing error: {str(e)}", exc_info=True)
|
| 66 |
raise
|
|
|
|
|
|
|
| 1 |
import io
|
| 2 |
import logging
|
| 3 |
from datetime import datetime
|
| 4 |
+
from PIL import Image, ImageDraw, ImageOps
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
+
def apply_halftone(image, dot_size=10):
|
| 10 |
+
"""Görüntüye halftone efekti uygular"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
start_time = datetime.now()
|
| 12 |
logger.info(f"Applying halftone effect - {start_time}")
|
| 13 |
|
| 14 |
try:
|
| 15 |
+
# Eğer image bir dosya verisi (bytes) ise Image'a dönüştür
|
| 16 |
+
if isinstance(image, bytes):
|
| 17 |
+
image = Image.open(io.BytesIO(image))
|
| 18 |
+
|
| 19 |
+
# Görüntüyü gri tonlamalı yap
|
| 20 |
+
if image.mode != 'L':
|
| 21 |
+
image = image.convert('L')
|
| 22 |
+
|
| 23 |
+
# Orijinal boyutları sakla
|
| 24 |
+
orig_width, orig_height = image.size
|
| 25 |
|
| 26 |
+
# Yeniden boyutlandırma faktörü
|
| 27 |
+
resize_factor = dot_size / 5
|
| 28 |
|
| 29 |
+
# Geçici olarak küçült
|
| 30 |
+
small_width = int(orig_width / resize_factor)
|
| 31 |
+
small_height = int(orig_height / resize_factor)
|
| 32 |
+
small_image = image.resize((small_width, small_height), Image.BILINEAR)
|
| 33 |
|
| 34 |
+
# Yeni görüntü oluştur
|
| 35 |
+
output_image = Image.new('L', (orig_width, orig_height), 255)
|
| 36 |
+
draw = ImageDraw.Draw(output_image)
|
| 37 |
+
|
| 38 |
+
# Halftone noktalarını çiz
|
| 39 |
+
for y in range(small_height):
|
| 40 |
+
for x in range(small_width):
|
| 41 |
+
# Piksel parlaklığını al
|
| 42 |
+
brightness = 255 - small_image.getpixel((x, y))
|
| 43 |
+
|
| 44 |
+
# Nokta boyutunu hesapla
|
| 45 |
+
radius = int((brightness / 255) * (dot_size / 2))
|
| 46 |
+
|
| 47 |
+
# Noktayı çiz
|
| 48 |
+
if radius > 0:
|
| 49 |
+
center_x = int((x * resize_factor) + (resize_factor / 2))
|
| 50 |
+
center_y = int((y * resize_factor) + (resize_factor / 2))
|
| 51 |
+
bbox = [
|
| 52 |
+
center_x - radius,
|
| 53 |
+
center_y - radius,
|
| 54 |
+
center_x + radius,
|
| 55 |
+
center_y + radius
|
| 56 |
+
]
|
| 57 |
+
draw.ellipse(bbox, fill=0)
|
| 58 |
|
| 59 |
duration = (datetime.now() - start_time).total_seconds()
|
| 60 |
logger.info(f"Successfully processed. Duration: {duration} seconds")
|
| 61 |
+
return output_image
|
| 62 |
+
|
| 63 |
except Exception as e:
|
| 64 |
logger.error(f"Halftone processing error: {str(e)}", exc_info=True)
|
| 65 |
raise
|