imgConvert / app.py
MaulikMadhavi's picture
fix block name warning
7978118
import gradio as gr
import cv2
from PIL import Image
from pillow_heif import register_heif_opener
import numpy as np
import imghdr
def process_image(input_image, width = None, height = None):
# Process the image here
# For example, you can apply some image filters or transformations
if input_image:
# Convert the PIL image to OpenCV format
image = Image.open(input_image)
image = np.array(image)
if width is None:
width = image.shape[1]
if height is None:
height = image.shape[0]
# Apply some image processing
print(f"Width: {width}, Height: {height}")
image = cv2.resize(image, (height, width))
print(f"Image shape: {image.shape}")
return image
else:
return None
def display_image_info(input_image):
if not input_image:
return "No image uploaded"
try:
format = imghdr.what(input_image)
return f"The format of the image is '{format}'"
except:
return f"The image format is unknown. However the file extension is '{input_image.split('.')[-1]}'"
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
input_image = gr.Image("input_image", type="filepath")
with gr.Column():
output_image = gr.Image("output_image", type="filepath")
# ========= For width and height ==========
with gr.Row():
width = gr.Slider(1, 1000, value=256, label="Width")
height = gr.Slider(1, 1000, value=192, label="Height")
with gr.Row():
output_textbox = gr.Textbox("output_textbox", type="text", label="Output Textbox")
input_image.change(process_image, [input_image, width, height], output_image)
input_image.change(display_image_info, input_image, output_textbox)
width.change(process_image, [input_image, width, height], output_image)
height.change(process_image, [input_image, width, height], output_image)
demo.launch()