Lionelxt/Image_processor
0
1import gradio as gr
2import cv2
3from image_processor import ImageProcessor
4
5def process_image(image_path, operation, value=None):
6 if image_path is None:
7 return None
8
9 processor = ImageProcessor(image_path)
10
11 if operation == 'greyscale':
12 processor.grey_scale()
13 elif operation == 'brightness' and value is not None:
14 try:
15 processor.brightness_adjust(int(value))
16 except ValueError:
17 # Handle invalid input
18 return None
19 elif operation == 'flip' and value is not None:
20 processor.flippic(value) # value should be 'horizontal' or 'vertical'
21 elif operation == 'isolate-red':
22 processor.isolate_red()
23 elif operation == 'blur':
24 processor.blur()
25 elif operation == 'cartoonize':
26 processor.cartoonize()
27 elif operation == 'sepia':
28 processor.sepia()
29
30 rgb_image = cv2.cvtColor(processor.image, cv2.COLOR_BGR2RGB)
31 return rgb_image
32
33def update_value_input_visibility(operation):
34 if operation in ["brightness", "flip"]:
35 return gr.update(visible=True, value="")
36 else:
37 return gr.update(visible=False, value="")
38
39with gr.Blocks() as demo:
40 with gr.Row():
41 image_input = gr.Image(type="filepath", label="Input Image")
42
43 with gr.Row():
44 with gr.Column():
45 # Remove the 'vertical=True' parameter that was causing the error
46 operation_buttons = gr.Radio(
47 choices=["greyscale", "brightness", "flip", "isolate-red", "blur", "cartoonize", "sepia"],
48 label="Choose Operation"
49 )
50
51 # For flip operation, provide predefined options
52 flip_direction = gr.Radio(
53 choices=["horizontal", "vertical"],
54 label="Flip Direction",
55 visible=False
56 )
57
58 # For brightness, provide a slider instead of a text input
59 brightness_slider = gr.Slider(
60 minimum=-255, maximum=255, step=1, value=0,
61 label="Brightness Adjustment",
62 visible=False
63 )
64
65 # Button to apply changes
66 apply_button = gr.Button("Apply Filter")
67
68 with gr.Column():
69 output_image = gr.Image(label="Processed Image")
70
71 # Update UI based on selected operation
72 def update_ui_components(operation):
73 if operation == "flip":
74 return gr.update(visible=True), gr.update(visible=False)
75 elif operation == "brightness":
76 return gr.update(visible=False), gr.update(visible=True)
77 else:
78 return gr.update(visible=False), gr.update(visible=False)
79
80 operation_buttons.change(
81 update_ui_components,
82 inputs=[operation_buttons],
83 outputs=[flip_direction, brightness_slider]
84 )
85
86 # Process image when Apply button is clicked
87 def get_value_for_operation(operation, flip_value, brightness_value):
88 if operation == "flip":
89 return flip_value
90 elif operation == "brightness":
91 return str(brightness_value)
92 return None
93
94 apply_button.click(
95 fn=lambda img, op, flip, bright: process_image(
96 img, op, get_value_for_operation(op, flip, bright)
97 ),
98 inputs=[image_input, operation_buttons, flip_direction, brightness_slider],
99 outputs=output_image
100 )
101
102
103if __name__ == "__main__":
104 demo.launch()