Lordecyhper commited on
Commit
ad281af
·
verified ·
1 Parent(s): fd56e22

Create image_alter.py

Browse files
Files changed (1) hide show
  1. image_alter.py +60 -0
image_alter.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+
5
+ def create_ghost_image(cover, reveal):
6
+ if cover is None or reveal is None:
7
+ return None
8
+
9
+ # 1. Convert to Grayscale ('L')
10
+ img1_raw = cover.convert('L')
11
+ img2_raw = reveal.convert('L')
12
+
13
+ # 2. Resize the reveal image to match the cover image size precisely
14
+ if img1_raw.size != img2_raw.size:
15
+ img2_raw = img2_raw.resize(img1_raw.size, Image.LANCZOS)
16
+
17
+ # 3. Math Processing (Numpy)
18
+ g1 = np.array(img1_raw, dtype=float) / 255.0
19
+ g2 = np.array(img2_raw, dtype=float) / 255.0
20
+
21
+ # Squash Image 1 (Cover) into the darker half (0.0 - 0.5)
22
+ g1 = g1 * 0.5
23
+
24
+ # Squash Image 2 (Reveal) into the brighter half (0.5 - 1.0)
25
+ g2 = 0.5 + (g2 * 0.5)
26
+
27
+ # Calculate Alpha
28
+ out_alpha = 1.0 - g2 + g1
29
+
30
+ # Calculate Grayscale
31
+ # Handle division by zero safely
32
+ out_gray = np.divide(g1, out_alpha, out=np.zeros_like(g1), where=out_alpha > 0.001)
33
+
34
+ # 4. Convert back to integer pixel data
35
+ final_alpha_int = (out_alpha * 255).clip(0, 255).astype(np.uint8)
36
+ final_gray_int = (out_gray * 255).clip(0, 255).astype(np.uint8)
37
+
38
+ # 5. Stack channels (R, G, B, A)
39
+ rgba_data = np.dstack((final_gray_int, final_gray_int, final_gray_int, final_alpha_int))
40
+
41
+ return Image.fromarray(rgba_data, 'RGBA')
42
+
43
+ # --- Gradio UI Setup ---
44
+ with gr.Blocks(title="Ghost Image Generator") as demo:
45
+ gr.Markdown("# 👻 Ghost Image Generator")
46
+ gr.Markdown("Create an image that looks different in Light Mode vs Dark Mode (Transparency Trick).")
47
+
48
+ with gr.Row():
49
+ with gr.Column():
50
+ img_dark = gr.Image(type="pil", label="Image seen in DARK Mode (Cover)")
51
+ img_light = gr.Image(type="pil", label="Image seen in LIGHT Mode (Reveal)")
52
+ btn = gr.Button("Generate Ghost Image", variant="primary")
53
+
54
+ with gr.Column():
55
+ output_img = gr.Image(label="Result (Download and test in Discord/Twitter)", type="pil", show_download_button=True)
56
+
57
+ btn.click(fn=create_ghost_image, inputs=[img_dark, img_light], outputs=output_img)
58
+
59
+ # Launch the app
60
+ demo.launch()