Spaces:
Sleeping
Sleeping
Commit ·
3e42073
1
Parent(s): b6292ac
Update README and add image processing functionality
Browse files- README.md +3 -3
- app.py +55 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.23.0
|
| 8 |
app_file: app.py
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Get Jpeg
|
| 3 |
+
emoji: 🚀
|
| 4 |
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.23.0
|
| 8 |
app_file: app.py
|
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from pillow_heif import register_heif_opener
|
| 5 |
+
import numpy as np
|
| 6 |
+
import imghdr
|
| 7 |
+
|
| 8 |
+
def process_image(input_image, width = None, height = None):
|
| 9 |
+
# Process the image here
|
| 10 |
+
# For example, you can apply some image filters or transformations
|
| 11 |
+
if input_image:
|
| 12 |
+
# Convert the PIL image to OpenCV format
|
| 13 |
+
image = Image.open(input_image)
|
| 14 |
+
image = np.array(image)
|
| 15 |
+
if width is None:
|
| 16 |
+
width = image.shape[1]
|
| 17 |
+
if height is None:
|
| 18 |
+
height = image.shape[0]
|
| 19 |
+
# Apply some image processing
|
| 20 |
+
print(f"Width: {width}, Height: {height}")
|
| 21 |
+
image = cv2.resize(image, (height, width))
|
| 22 |
+
print(f"Image shape: {image.shape}")
|
| 23 |
+
return image
|
| 24 |
+
else:
|
| 25 |
+
return None
|
| 26 |
+
|
| 27 |
+
def display_image_info(input_image):
|
| 28 |
+
if not input_image:
|
| 29 |
+
return "No image uploaded"
|
| 30 |
+
try:
|
| 31 |
+
format = imghdr.what(input_image)
|
| 32 |
+
return f"The format of the image is '{format}'"
|
| 33 |
+
except:
|
| 34 |
+
return f"The image format is unknown. However the file extension is '{input_image.split('.')[-1]}'"
|
| 35 |
+
|
| 36 |
+
with gr.Blocks("Image-Processing") as demo:
|
| 37 |
+
with gr.Row():
|
| 38 |
+
with gr.Column():
|
| 39 |
+
input_image = gr.Image("input_image", type="filepath")
|
| 40 |
+
with gr.Column():
|
| 41 |
+
output_image = gr.Image("output_image", type="filepath")
|
| 42 |
+
# ========= For width and height ==========
|
| 43 |
+
with gr.Row():
|
| 44 |
+
width = gr.Slider(1, 1000, value=256, label="Width")
|
| 45 |
+
height = gr.Slider(1, 1000, value=192, label="Height")
|
| 46 |
+
with gr.Row():
|
| 47 |
+
output_textbox = gr.Textbox("output_textbox", type="text", label="Output Textbox")
|
| 48 |
+
|
| 49 |
+
input_image.change(process_image, [input_image, width, height], output_image)
|
| 50 |
+
input_image.change(display_image_info, input_image, output_textbox)
|
| 51 |
+
|
| 52 |
+
width.change(process_image, [input_image, width, height], output_image)
|
| 53 |
+
height.change(process_image, [input_image, width, height], output_image)
|
| 54 |
+
|
| 55 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pillow
|
| 2 |
+
pillow-avif-plugin
|