Spaces:
Sleeping
Sleeping
Update chatgpt-ad-maker.py
Browse files- chatgpt-ad-maker.py +39 -64
chatgpt-ad-maker.py
CHANGED
|
@@ -1,48 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import cv2
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
def create_dot_effect(image, dot_size=10, spacing=2, invert=False):
|
| 7 |
-
# Convert to grayscale if image is color
|
| 8 |
if len(image.shape) == 3:
|
| 9 |
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 10 |
else:
|
| 11 |
gray = image
|
| 12 |
|
| 13 |
-
# Apply adaptive thresholding to improve contrast
|
| 14 |
gray = cv2.adaptiveThreshold(
|
| 15 |
gray,
|
| 16 |
255,
|
| 17 |
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
| 18 |
cv2.THRESH_BINARY,
|
| 19 |
-
25,
|
| 20 |
-
5
|
| 21 |
)
|
| 22 |
|
| 23 |
-
# Create a blank canvas with white background if inverted
|
| 24 |
height, width = gray.shape
|
| 25 |
canvas = np.zeros_like(gray) if not invert else np.full_like(gray, 255)
|
| 26 |
|
| 27 |
-
# Calculate number of dots based on spacing
|
| 28 |
y_dots = range(0, height, dot_size + spacing)
|
| 29 |
x_dots = range(0, width, dot_size + spacing)
|
| 30 |
|
| 31 |
-
# Create dots based on brightness
|
| 32 |
dot_color = 255 if not invert else 0
|
| 33 |
for y in y_dots:
|
| 34 |
for x in x_dots:
|
| 35 |
-
# Get the average brightness of the region
|
| 36 |
region = gray[y:min(y+dot_size, height), x:min(x+dot_size, width)]
|
| 37 |
if region.size > 0:
|
| 38 |
brightness = np.mean(region)
|
| 39 |
-
|
| 40 |
-
# Dynamic dot sizing based on brightness
|
| 41 |
relative_brightness = brightness / 255.0
|
| 42 |
if invert:
|
| 43 |
relative_brightness = 1 - relative_brightness
|
| 44 |
-
|
| 45 |
-
# Draw circle with size proportional to brightness
|
| 46 |
radius = int((dot_size/2) * relative_brightness)
|
| 47 |
if radius > 0:
|
| 48 |
cv2.circle(canvas,
|
|
@@ -50,31 +39,33 @@ def create_dot_effect(image, dot_size=10, spacing=2, invert=False):
|
|
| 50 |
radius,
|
| 51 |
(dot_color),
|
| 52 |
-1)
|
| 53 |
-
|
| 54 |
return canvas
|
| 55 |
|
| 56 |
def process_video(video_path, dot_size=10, spacing=2, invert=False):
|
| 57 |
-
# Read the video
|
| 58 |
cap = cv2.VideoCapture(video_path)
|
| 59 |
if not cap.isOpened():
|
| 60 |
return None
|
| 61 |
|
| 62 |
-
# Get video properties
|
| 63 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
# Calculate target dimensions (max 720p for performance)
|
| 68 |
max_height = 720
|
| 69 |
-
if
|
| 70 |
-
scale = max_height /
|
| 71 |
-
frame_width = int(
|
| 72 |
frame_height = max_height
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
try:
|
| 80 |
while cap.isOpened():
|
|
@@ -82,60 +73,44 @@ def process_video(video_path, dot_size=10, spacing=2, invert=False):
|
|
| 82 |
if not ret:
|
| 83 |
break
|
| 84 |
|
| 85 |
-
|
| 86 |
-
if frame.shape[0] > max_height:
|
| 87 |
frame = cv2.resize(frame, (frame_width, frame_height))
|
| 88 |
|
| 89 |
-
# Convert BGR to RGB for processing
|
| 90 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 91 |
-
|
| 92 |
-
# Apply dot effect (returns grayscale)
|
| 93 |
dotted_frame = create_dot_effect(frame_rgb, dot_size, spacing, invert)
|
| 94 |
-
|
| 95 |
-
# Convert grayscale to BGR for video writer
|
| 96 |
dotted_frame_bgr = cv2.cvtColor(dotted_frame, cv2.COLOR_GRAY2BGR)
|
| 97 |
-
|
| 98 |
-
# Write the frame
|
| 99 |
out.write(dotted_frame_bgr)
|
| 100 |
|
| 101 |
finally:
|
| 102 |
-
# Ensure resources are released
|
| 103 |
cap.release()
|
| 104 |
out.release()
|
| 105 |
|
| 106 |
return output_path
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
gr.Markdown("
|
| 111 |
-
gr.Markdown("Convert your image or video into a dotted pattern. Adjust dot size and spacing using the sliders.")
|
| 112 |
|
| 113 |
-
with gr.Tab("Image"):
|
| 114 |
-
image_input = gr.Image(label="Input Image")
|
| 115 |
with gr.Row():
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
outputs=image_output
|
| 124 |
-
)
|
| 125 |
|
| 126 |
-
with gr.Tab("Video"):
|
| 127 |
-
|
|
|
|
|
|
|
| 128 |
with gr.Row():
|
| 129 |
-
vid_dot_size = gr.Slider(
|
| 130 |
-
vid_spacing = gr.Slider(
|
| 131 |
-
vid_invert = gr.Checkbox(label="Invert"
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
video_button.click(
|
| 135 |
-
fn=process_video,
|
| 136 |
-
inputs=[video_input, vid_dot_size, vid_spacing, vid_invert],
|
| 137 |
-
outputs=video_output
|
| 138 |
-
)
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|
| 141 |
-
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, invert=False):
|
|
|
|
| 6 |
if len(image.shape) == 3:
|
| 7 |
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 8 |
else:
|
| 9 |
gray = image
|
| 10 |
|
|
|
|
| 11 |
gray = cv2.adaptiveThreshold(
|
| 12 |
gray,
|
| 13 |
255,
|
| 14 |
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
| 15 |
cv2.THRESH_BINARY,
|
| 16 |
+
25,
|
| 17 |
+
5
|
| 18 |
)
|
| 19 |
|
|
|
|
| 20 |
height, width = gray.shape
|
| 21 |
canvas = np.zeros_like(gray) if not invert else np.full_like(gray, 255)
|
| 22 |
|
|
|
|
| 23 |
y_dots = range(0, height, dot_size + spacing)
|
| 24 |
x_dots = range(0, width, dot_size + spacing)
|
| 25 |
|
|
|
|
| 26 |
dot_color = 255 if not invert else 0
|
| 27 |
for y in y_dots:
|
| 28 |
for x in x_dots:
|
|
|
|
| 29 |
region = gray[y:min(y+dot_size, height), x:min(x+dot_size, width)]
|
| 30 |
if region.size > 0:
|
| 31 |
brightness = np.mean(region)
|
|
|
|
|
|
|
| 32 |
relative_brightness = brightness / 255.0
|
| 33 |
if invert:
|
| 34 |
relative_brightness = 1 - relative_brightness
|
|
|
|
|
|
|
| 35 |
radius = int((dot_size/2) * relative_brightness)
|
| 36 |
if radius > 0:
|
| 37 |
cv2.circle(canvas,
|
|
|
|
| 39 |
radius,
|
| 40 |
(dot_color),
|
| 41 |
-1)
|
|
|
|
| 42 |
return canvas
|
| 43 |
|
| 44 |
def process_video(video_path, dot_size=10, spacing=2, invert=False):
|
|
|
|
| 45 |
cap = cv2.VideoCapture(video_path)
|
| 46 |
if not cap.isOpened():
|
| 47 |
return None
|
| 48 |
|
|
|
|
| 49 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 50 |
+
orig_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 51 |
+
orig_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 52 |
|
|
|
|
| 53 |
max_height = 720
|
| 54 |
+
if orig_height > max_height:
|
| 55 |
+
scale = max_height / orig_height
|
| 56 |
+
frame_width = int(orig_width * scale)
|
| 57 |
frame_height = max_height
|
| 58 |
+
else:
|
| 59 |
+
frame_width = orig_width
|
| 60 |
+
frame_height = orig_height
|
| 61 |
|
| 62 |
+
# Ensure even dimensions for video codec compatibility
|
| 63 |
+
frame_width = frame_width // 2 * 2
|
| 64 |
+
frame_height = frame_height // 2 * 2
|
| 65 |
+
|
| 66 |
+
output_path = "output.mp4"
|
| 67 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 68 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height), True)
|
| 69 |
|
| 70 |
try:
|
| 71 |
while cap.isOpened():
|
|
|
|
| 73 |
if not ret:
|
| 74 |
break
|
| 75 |
|
| 76 |
+
if frame.shape[0] != frame_height:
|
|
|
|
| 77 |
frame = cv2.resize(frame, (frame_width, frame_height))
|
| 78 |
|
|
|
|
| 79 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
|
|
|
|
|
| 80 |
dotted_frame = create_dot_effect(frame_rgb, dot_size, spacing, invert)
|
|
|
|
|
|
|
| 81 |
dotted_frame_bgr = cv2.cvtColor(dotted_frame, cv2.COLOR_GRAY2BGR)
|
|
|
|
|
|
|
| 82 |
out.write(dotted_frame_bgr)
|
| 83 |
|
| 84 |
finally:
|
|
|
|
| 85 |
cap.release()
|
| 86 |
out.release()
|
| 87 |
|
| 88 |
return output_path
|
| 89 |
|
| 90 |
+
with gr.Blocks(title="Dot Effect Generator") as iface:
|
| 91 |
+
gr.Markdown("# 🎨 Dot Effect Generator")
|
| 92 |
+
gr.Markdown("Transform media into stylized dot patterns")
|
|
|
|
| 93 |
|
| 94 |
+
with gr.Tab("🖼️ Image"):
|
|
|
|
| 95 |
with gr.Row():
|
| 96 |
+
img_input = gr.Image(label="Input")
|
| 97 |
+
img_output = gr.Image(label="Output")
|
| 98 |
+
with gr.Row():
|
| 99 |
+
img_dot_size = gr.Slider(2, 20, 10, step=1, label="Dot Size")
|
| 100 |
+
img_spacing = gr.Slider(0, 10, 2, step=1, label="Spacing")
|
| 101 |
+
img_btn = gr.Button("Generate", variant="primary")
|
| 102 |
+
img_btn.click(create_dot_effect, [img_input, img_dot_size, img_spacing], img_output)
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
with gr.Tab("🎥 Video"):
|
| 105 |
+
with gr.Row():
|
| 106 |
+
vid_input = gr.Video(label="Input", format="mp4")
|
| 107 |
+
vid_output = gr.Video(label="Output", format="mp4")
|
| 108 |
with gr.Row():
|
| 109 |
+
vid_dot_size = gr.Slider(2, 20, 10, step=1, label="Dot Size")
|
| 110 |
+
vid_spacing = gr.Slider(0, 10, 2, step=1, label="Spacing")
|
| 111 |
+
vid_invert = gr.Checkbox(label="Invert Colors")
|
| 112 |
+
vid_btn = gr.Button("Process Video", variant="primary")
|
| 113 |
+
vid_btn.click(process_video, [vid_input, vid_dot_size, vid_spacing, vid_invert], vid_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
+
iface.launch(server_port=7860, show_error=True)
|