Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image, ImageDraw
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
def halftone_effect(image, shape_type, grid_spacing=10, max_size=8):
|
5 |
+
"""
|
6 |
+
Apply a halftone effect to the input image using the specified shape type.
|
7 |
+
|
8 |
+
Args:
|
9 |
+
image (PIL.Image): Input image.
|
10 |
+
shape_type (str): Type of shape ('circle', 'square', 'triangle').
|
11 |
+
grid_spacing (int): Distance between grid points in pixels (default: 10).
|
12 |
+
max_size (int): Maximum size of shapes in pixels (default: 8).
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
PIL.Image: Processed image with halftone effect.
|
16 |
+
"""
|
17 |
+
# Convert image to grayscale
|
18 |
+
gray_image = image.convert('L')
|
19 |
+
# Create a new image with white background
|
20 |
+
output = Image.new('RGB', image.size, (255, 255, 255))
|
21 |
+
draw = ImageDraw.Draw(output)
|
22 |
+
|
23 |
+
# Iterate over grid points
|
24 |
+
for x in range(0, image.width, grid_spacing):
|
25 |
+
for y in range(0, image.height, grid_spacing):
|
26 |
+
# Get grayscale value (0 = black, 255 = white)
|
27 |
+
g = gray_image.getpixel((x, y))
|
28 |
+
# Calculate shape size (larger for darker areas)
|
29 |
+
size = max_size * (1 - g / 255)
|
30 |
+
if size < 1:
|
31 |
+
continue # Skip if size is too small
|
32 |
+
|
33 |
+
# Draw the specified shape
|
34 |
+
if shape_type == 'circle':
|
35 |
+
bbox = [x - size/2, y - size/2, x + size/2, y + size/2]
|
36 |
+
draw.ellipse(bbox, fill='black')
|
37 |
+
elif shape_type == 'square':
|
38 |
+
bbox = [x - size/2, y - size/2, x + size/2, y + size/2]
|
39 |
+
draw.rectangle(bbox, fill='black')
|
40 |
+
elif shape_type == 'triangle':
|
41 |
+
p1 = (x, y - size/2)
|
42 |
+
p2 = (x - size/2, y + size/2)
|
43 |
+
p3 = (x + size/2, y + size/2)
|
44 |
+
draw.polygon([p1, p2, p3], fill='black')
|
45 |
+
|
46 |
+
return output
|
47 |
+
|
48 |
+
# Define Gradio interface
|
49 |
+
iface = gr.Interface(
|
50 |
+
fn=halftone_effect,
|
51 |
+
inputs=[
|
52 |
+
gr.Image(type="pil", label="Upload Image"),
|
53 |
+
gr.Dropdown(["circle", "square", "triangle"], label="Shape Type")
|
54 |
+
],
|
55 |
+
outputs=gr.Image(type="pil", label="Halftone Effect Output"),
|
56 |
+
title="Halftone Effect App",
|
57 |
+
description="Upload an image and select a shape to apply a halftone effect."
|
58 |
+
)
|
59 |
+
|
60 |
+
# Launch the interface
|
61 |
+
iface.launch()
|