Spaces:
Sleeping
Sleeping
Add halftone dot effect style
Browse files
app.py
CHANGED
|
@@ -61,6 +61,21 @@ def stylize(img, style, hex_color):
|
|
| 61 |
# fallback: high-contrast sharpened grayscale
|
| 62 |
return ImageOps.autocontrast(ImageEnhance.Sharpness(img.convert("L")).enhance(3.0), cutoff=2).convert("RGB")
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
if style == "duotone":
|
| 65 |
gray = ImageOps.autocontrast(img.convert("L"), cutoff=2)
|
| 66 |
gray = ImageOps.posterize(gray, 3)
|
|
@@ -615,7 +630,7 @@ with gr.Blocks(title="stamp maker ✦") as demo:
|
|
| 615 |
elem_classes=["color-pick"],
|
| 616 |
)
|
| 617 |
style_in = gr.Radio(
|
| 618 |
-
choices=["duotone", "grayscale", "pop art", "sepia", "engraving ✦"],
|
| 619 |
value="duotone",
|
| 620 |
label="style",
|
| 621 |
elem_classes=["style-pick"],
|
|
|
|
| 61 |
# fallback: high-contrast sharpened grayscale
|
| 62 |
return ImageOps.autocontrast(ImageEnhance.Sharpness(img.convert("L")).enhance(3.0), cutoff=2).convert("RGB")
|
| 63 |
|
| 64 |
+
if style == "halftone":
|
| 65 |
+
gray = np.array(ImageOps.autocontrast(img.convert("L"), cutoff=2), dtype=np.float32)
|
| 66 |
+
out = Image.new("RGB", img.size, (245, 245, 245))
|
| 67 |
+
draw = ImageDraw.Draw(out)
|
| 68 |
+
cell = 7
|
| 69 |
+
for cy in range(cell // 2, img.height, cell):
|
| 70 |
+
for cx in range(cell // 2, img.width, cell):
|
| 71 |
+
patch = gray[max(0, cy - cell//2):cy + cell//2 + 1,
|
| 72 |
+
max(0, cx - cell//2):cx + cell//2 + 1]
|
| 73 |
+
radius = (1 - patch.mean() / 255.0) * (cell / 2) * 0.95
|
| 74 |
+
if radius > 0.5:
|
| 75 |
+
draw.ellipse([cx - radius, cy - radius, cx + radius, cy + radius],
|
| 76 |
+
fill=(r, g, b))
|
| 77 |
+
return out
|
| 78 |
+
|
| 79 |
if style == "duotone":
|
| 80 |
gray = ImageOps.autocontrast(img.convert("L"), cutoff=2)
|
| 81 |
gray = ImageOps.posterize(gray, 3)
|
|
|
|
| 630 |
elem_classes=["color-pick"],
|
| 631 |
)
|
| 632 |
style_in = gr.Radio(
|
| 633 |
+
choices=["duotone", "grayscale", "pop art", "sepia", "halftone", "engraving ✦"],
|
| 634 |
value="duotone",
|
| 635 |
label="style",
|
| 636 |
elem_classes=["style-pick"],
|