File size: 2,015 Bytes
3e42073
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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("Image-Processing") 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()