RGB-Measure / app.py
iamsuman's picture
fix color preview
ef28b8b
import gradio as gr
import numpy as np
from PIL import Image, ImageDraw
# Function to read and display RGB at a predefined point and plot the point
def get_rgb_and_plot_point(img, x, y, point_size):
img = np.array(img) # Convert the image to a numpy array
if x < 0 or y < 0 or x >= img.shape[1] or y >= img.shape[0]:
return "Point is outside the image bounds", img
rgb_value = img[y, x] # Get the RGB value
# Create a PIL Image to draw the point
pil_img = Image.fromarray(img)
draw = ImageDraw.Draw(pil_img)
# Draw a circle at the specified point
draw.ellipse((x - point_size // 2, y - point_size // 2, x + point_size // 2, y + point_size // 2), outline="red", width=2)
return f'RGB value at ({x}, {y}): {rgb_value}', pil_img
# Gradio interface function
def image_rgb_extractor(img, x, y, point_size):
return get_rgb_and_plot_point(img, int(x), int(y), int(point_size))
# Set up the Gradio interface
input_image = gr.Image(type="pil", label="Upload an Image")
input_x = gr.Number(label="X-coordinate")
input_y = gr.Number(label="Y-coordinate")
input_point_size = gr.Number(label="Point Size", value=10)
output_rgb = gr.Textbox(label="RGB Value at Point")
output_image = gr.Image(label="Image with Point")
# Launch the Gradio app
interface = gr.Interface(
fn=image_rgb_extractor,
inputs=[input_image, input_x, input_y, input_point_size],
outputs=[output_rgb, output_image],
title="Predefined Point RGB Inspector",
description="Upload an image and input coordinates (x, y) and point size to get the RGB value at that point and see it plotted on the image."
)
interface.launch()