akhaliq HF Staff commited on
Commit
2778a57
·
verified ·
1 Parent(s): fe00d6f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+
5
+ def halftone_effect(image, shape="circle", dot_size=10):
6
+ # Convert image to grayscale
7
+ img = Image.fromarray(image).convert('L')
8
+ img_array = np.array(img)
9
+
10
+ # Get image dimensions
11
+ height, width = img_array.shape
12
+
13
+ # Create output image
14
+ output = np.ones((height, width, 3), dtype=np.uint8) * 255
15
+
16
+ # Process each pixel
17
+ for y in range(0, height, dot_size):
18
+ for x in range(0, width, dot_size):
19
+ # Get average brightness in this region
20
+ region = img_array[y:y+dot_size, x:x+dot_size]
21
+ if region.size == 0:
22
+ continue
23
+ brightness = np.mean(region) / 255
24
+
25
+ # Calculate dot size based on brightness (darker = larger)
26
+ size = int(dot_size * (1 - brightness))
27
+
28
+ # Draw shape
29
+ if shape == "circle":
30
+ for i in range(max(0, y - size), min(height, y + size)):
31
+ for j in range(max(0, x - size), min(width, x + size)):
32
+ if (i - y) ** 2 + (j - x) ** 2 <= size ** 2:
33
+ output[i, j] = [0, 0, 0]
34
+
35
+ elif shape == "square":
36
+ for i in range(max(0, y - size), min(height, y + size)):
37
+ for j in range(max(0, x - size), min(width, x + size)):
38
+ output[i, j] = [0, 0, 0]
39
+
40
+ elif shape == "diamond":
41
+ for i in range(max(0, y - size), min(height, y + size)):
42
+ for j in range(max(0, x - size), min(width, x + size)):
43
+ if abs(i - y) + abs(j - x) <= size:
44
+ output[i, j] = [0, 0, 0]
45
+
46
+ return output
47
+
48
+ # Create Gradio interface
49
+ interface = gr.Interface(
50
+ fn=halftone_effect,
51
+ inputs=[
52
+ gr.Image(label="Input Image"),
53
+ gr.Dropdown(
54
+ choices=["circle", "square", "diamond"],
55
+ value="circle",
56
+ label="Dot Shape"
57
+ ),
58
+ gr.Slider(
59
+ minimum=5,
60
+ maximum=20,
61
+ value=10,
62
+ step=1,
63
+ label="Dot Size"
64
+ )
65
+ ],
66
+ outputs=gr.Image(label="Halftone Result"),
67
+ title="Halftone Effect Generator",
68
+ description="Upload an image and create a halftone effect with different shape options!"
69
+ )
70
+
71
+ # Launch the app
72
+ interface.launch()