akhaliq HF staff commited on
Commit
678fc41
·
verified ·
1 Parent(s): c4d908c

Update chatgpt-ad-maker.py

Browse files
Files changed (1) hide show
  1. chatgpt-ad-maker.py +36 -41
chatgpt-ad-maker.py CHANGED
@@ -1,57 +1,52 @@
1
  import gradio as gr
2
  import numpy as np
 
3
 
4
- def dot_effect(input_image, dot_size, threshold, dot_color):
5
- """
6
- Convert input image to customizable dotted effect
 
 
 
7
 
8
- Args:
9
- input_image (ndarray): Input image
10
- dot_size (int): Size of dots
11
- threshold (float): Brightness threshold for dot appearance
12
- dot_color (list): RGB color of dots
13
 
14
- Returns:
15
- ndarray: Image with dotted effect
16
- """
17
- # Convert input to grayscale
18
- gray_image = np.mean(input_image, axis=2)
19
 
20
- # Normalize pixel values
21
- normalized = (gray_image - gray_image.min()) / (gray_image.max() - gray_image.min())
22
-
23
- # Create dot mask
24
- height, width = gray_image.shape
25
- dot_mask = np.zeros((height, width, 3), dtype=np.uint8)
26
-
27
- for y in range(0, height, dot_size):
28
- for x in range(0, width, dot_size):
29
- if normalized[y, x] > threshold:
30
- radius = int(dot_size * normalized[y, x])
31
- center_x, center_y = x + dot_size//2, y + dot_size//2
32
 
33
- for dy in range(-radius, radius):
34
- for dx in range(-radius, radius):
35
- if dx*dx + dy*dy <= radius*radius:
36
- px, py = center_x + dx, center_y + dy
37
- if 0 <= px < width and 0 <= py < height:
38
- dot_mask[py, px] = dot_color
 
39
 
40
- return dot_mask
41
 
42
  # Create Gradio interface
43
  iface = gr.Interface(
44
- fn=dot_effect,
45
  inputs=[
46
- gr.Image(type="numpy", label="Input Image"),
47
- gr.Slider(minimum=1, maximum=20, value=5, label="Dot Size"),
48
- gr.Slider(minimum=0, maximum=1, value=0.3, label="Dot Threshold"),
49
- gr.ColorPicker(label="Dot Color", value="#FFFFFF")
50
  ],
51
- outputs=gr.Image(type="numpy"),
52
  title="ChatGPT Ad Maker",
53
- description="Transform images into customizable dotted effect"
54
  )
55
 
56
- # Launch the app
57
- iface.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
+ import cv2
4
 
5
+ def create_dot_effect(image, dot_size=10, spacing=2):
6
+ # Convert to grayscale if image is color
7
+ if len(image.shape) == 3:
8
+ gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
9
+ else:
10
+ gray = image
11
 
12
+ # Create a blank canvas
13
+ height, width = gray.shape
14
+ canvas = np.zeros_like(gray)
 
 
15
 
16
+ # Calculate number of dots based on spacing
17
+ y_dots = range(0, height, dot_size + spacing)
18
+ x_dots = range(0, width, dot_size + spacing)
 
 
19
 
20
+ # Create dots based on brightness
21
+ for y in y_dots:
22
+ for x in x_dots:
23
+ # Get the average brightness of the region
24
+ region = gray[y:min(y+dot_size, height), x:min(x+dot_size, width)]
25
+ if region.size > 0:
26
+ brightness = np.mean(region)
 
 
 
 
 
27
 
28
+ # Draw circle if the region is bright enough
29
+ if brightness > 30: # Threshold can be adjusted
30
+ cv2.circle(canvas,
31
+ (x + dot_size//2, y + dot_size//2),
32
+ dot_size//2,
33
+ (255),
34
+ -1)
35
 
36
+ return canvas
37
 
38
  # Create Gradio interface
39
  iface = gr.Interface(
40
+ fn=create_dot_effect,
41
  inputs=[
42
+ gr.Image(label="Input Image"),
43
+ gr.Slider(minimum=2, maximum=20, value=10, step=1, label="Dot Size"),
44
+ gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Dot Spacing")
 
45
  ],
46
+ outputs=gr.Image(label="Dotted Image"),
47
  title="ChatGPT Ad Maker",
48
+ description="Convert your image into a dotted pattern. Adjust dot size and spacing using the sliders."
49
  )
50
 
51
+ if __name__ == "__main__":
52
+ iface.launch()