Spaces:
Sleeping
Sleeping
Update app.py
Browse files9 unmodified lines10111213141516171819202122232425268 unmodified lines3536373839404142435 unmodified lines49505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394953 unmodified lines991001011021031041051061071081091101111121131141151161171181199 unmodified lines3) Return the color as a hex string, e.g. '#RRGGBB'."""if not image_filepath:return None # No file to open yet# Open the image so we can read its pixelsimg = Image.open(image_filepath).convert("RGBA")# evt.index is (x, y) in 0-based coordinatesx, y = evt.indexif x < 0 or y < 0 or x >= img.width or y >= img.height:return None # Out of bounds (shouldn't happen)r, g, b, *rest = img.getpixel((x, y)) # Could be RGBA or RGBreturn "#{:02x}{:02x}{:02x}".format(r, g, b)def rgb_distance(r1, g1, b1, r2, g2, b2):8 unmodified lines4) Save the final as a PNG with the same base name as the uploaded file."""if not image_filepath:return None, None # No file to process# Open image as RGBAimg = Image.open(image_filepath).convert("RGBA")pixels = list(img.getdata())5 unmodified linesint(chosen_hex[4:6], 16),)# Change chosen color to black, everything else -> transparentnew_pixels = []for (r, g, b, a) in pixels:dist = rgb_distance(r, g, b, *chosen_rgb)if dist <= threshold:# This pixel is close enough to the chosen color:# Make it black but preserve alphanew_pixels.append((0, 0, 0, a))else:# Make it fully transparentnew_pixels.append((r, g, b, 0))img.putdata(new_pixels)# Use the same base name as the original, but .pngbase_name = os.path.basename(image_filepath) # e.g. "photo.jpg"name_part, _ = os.path.splitext(base_name) # ("photo", ".jpg")output_filename = name_part + ".png" # => "photo.png"img.save(output_filename, "PNG")# Return: (preview Image, downloadable file)return img, output_filenamewith gr.Blocks() as demo:gr.Markdown("# Overlay Image Conversion\n""1. Upload an image.\n""2. Use **Eyedropper Color** card to pick a color.\n""3. Select eyedropper icon.\n""3. Click on color in image to keep\n""4. Adjust threshold and click **Apply**.\n""5. Download the transparent PNG file.")with gr.Row():# We use type="filepath" to preserve the original filename.# Gradio v3.23+ supports .select() events even when type="filepath".input_image = gr.Image(type="filepath", label="Upload & Click to Pick Color")with gr.Column():color_picker = gr.ColorPicker(label="Eyedropper Color", value="#ffffff")threshold_slider = gr.Slider(label="Threshold",minimum=0,3 unmodified lines)apply_button = gr.Button("Apply")result_image = gr.Image(type="pil", label="Result Preview")download_file = gr.File(label="Download PNG")# 1) Eyedropper step: when user clicks the image, we extract pixel colorinput_image.select(fn=get_pixel_color_from_filepath,inputs=input_image,outputs=color_picker)# 2) Processing step: user clicks "Apply" to transform the imageapply_button.click(fn=apply_transform,inputs=[input_image, color_picker, threshold_slider],outputs=[result_image, download_file])demo.launch()9 unmodified lines1011121314151617181920212223248 unmodified lines33343536373839405 unmodified lines464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293943 unmodified lines98991001011021031041051061071081091101111121131141151161171181191209 unmodified lines3) Return the color as a hex string, e.g. '#RRGGBB'."""if not image_filepath:return Noneimg = Image.open(image_filepath).convert("RGBA")x, y = evt.indexif x < 0 or y < 0 or x >= img.width or y >= img.height:return None # Out of bounds (shouldn't happen)r, g, b, *rest = img.getpixel((x, y))return "#{:02x}{:02x}{:02x}".format(r, g, b)def rgb_distance(r1, g1, b1, r2, g2, b2):8 unmodified lines4) Save the final as a PNG with the same base name as the uploaded file."""if not image_filepath:return None, Noneimg = Image.open(image_filepath).convert("RGBA")pixels = list(img.getdata())5 unmodified linesint(chosen_hex[4:6], 16),)new_pixels = []for (r, g, b, a) in pixels:dist = rgb_distance(r, g, b, *chosen_rgb)if dist <= threshold:# Pixel matches chosen color => make blacknew_pixels.append((0, 0, 0, a))else:# Otherwise, fully transparentnew_pixels.append((r, g, b, 0))img.putdata(new_pixels)# Use same base name -> .pngbase_name = os.path.basename(image_filepath) # e.g. "photo.jpg"name_part, _ = os.path.splitext(base_name) # ("photo", ".jpg")output_filename = name_part + ".png"img.save(output_filename, "PNG")return img, output_filenamewith gr.Blocks() as demo:gr.Markdown("# Overlay Image Conversion\n""1. **Upload an image** on the left.\n""2. Click on the image to **pick a color** (eyedropper).\n""3. Adjust threshold, **Apply** changes.\n""4. **Download** the transparent PNG file on the right.")with gr.Row():with gr.Column():# Uploaded image (left, top)input_image = gr.Image(type="filepath",label="Upload & Click to Pick Color")# Below the input imagecolor_picker = gr.ColorPicker(label="Eyedropper Color",value="#ffffff")threshold_slider = gr.Slider(label="Threshold",minimum=0,3 unmodified lines)apply_button = gr.Button("Apply")with gr.Column():# Result image (right side)result_image = gr.Image(type="pil", label="Result Preview")download_file = gr.File(label="Download PNG")# Eyedropper stepinput_image.select(fn=get_pixel_color_from_filepath,inputs=input_image,outputs=color_picker)# Apply transform buttonapply_button.click(fn=apply_transform,inputs=[input_image, color_picker, threshold_slider],outputs=[result_image, download_file])demo.launch()
app.py+29-28