meryadri commited on
Commit
c422481
·
1 Parent(s): 6e02926

img resizer is functional

Browse files
app.py CHANGED
@@ -1,7 +1,37 @@
1
  import gradio as gr
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  demo.launch()
 
1
  import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ from PIL import Image
5
+ import os
6
+ from logic.imgPreprocess import resize_img
7
 
8
+ def preprocess_image(image: Image.Image, size: int):
9
+ # Resize using the resize_img function directly on the PIL Image
10
+ resized_np = resize_img(image, size=(size, size))
11
+ # Convert back to RGB for display (cv2 uses BGR)
12
+ resized_rgb = cv2.cvtColor(resized_np, cv2.COLOR_BGR2RGB)
13
+ return Image.fromarray(resized_rgb)
14
 
15
+
16
+ # Only show example images for upload, no grid or extra info
17
+ example_dir = "data/images"
18
+ example_files = [os.path.join(example_dir, f) for f in [
19
+ "bird.JPG", "cheetah.JPG", "lion.JPG", "oryx.JPG", "ostrich.JPG", "rhino.JPG", "zebra.JPG"
20
+ ]]
21
+
22
+ inputs = [
23
+ gr.Image(type="pil", label="Upload or Select Example Image"),
24
+ ]
25
+
26
+ outputs = gr.Image(type="pil", label="Resized Image")
27
+
28
+ examples = [[f, 400] for f in example_files]
29
+
30
+ demo = gr.Interface(
31
+ fn=preprocess_image,
32
+ inputs=inputs,
33
+ outputs=outputs,
34
+ examples=examples,
35
+ title="Image Resizer"
36
+ )
37
  demo.launch()
data/.DS_Store ADDED
Binary file (6.15 kB). View file
 
logic/__init__.py ADDED
File without changes
logic/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (167 Bytes). View file
 
logic/__pycache__/imgPreprocess.cpython-313.pyc ADDED
Binary file (1.22 kB). View file
 
logic/imgPreprocess.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+ from sklearn.cluster import KMeans
4
+ from PIL import Image
5
+
6
+ # can try different img sizes and different sampling times
7
+ # try NEAREST, BILINEAR, CUBIC as well
8
+ def resize_img(image: Image.Image, size=(400, 400)) -> np.ndarray:
9
+ """
10
+ Resize a PIL Image to a fixed size (default 800x800) and return as a NumPy array (BGR format).
11
+ Args:
12
+ image (PIL.Image.Image): Input image.
13
+ size (tuple): Target size as (width, height).
14
+ Returns:
15
+ np.ndarray: Resized image as a NumPy array (BGR format).
16
+ """
17
+ image_np = np.array(image)
18
+ # Convert RGB (PIL) to BGR (OpenCV)
19
+ if image_np.shape[-1] == 3:
20
+ image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
21
+ resized_np = cv2.resize(image_np, size, interpolation=cv2.INTER_LANCZOS4)
22
+ return resized_np
23
+
24
+
25
+ # def color_quantize(image: Image.Image, n_colors: int = 8) -> Image.Image:
26
+ # """
27
+ # Apply color quantization to an image using KMeans clustering.
28
+ # Args:
29
+ # image (PIL.Image.Image): Input image.
30
+ # n_colors (int): Number of colors for quantization.
31
+ # Returns:
32
+ # PIL.Image.Image: Quantized image.
33
+ # """
34
+ # # Convert image to numpy array
35
+ # img_np = np.array(image)
36
+ # shape = img_np.shape
37
+ # # Flatten the image to (num_pixels, 3)
38
+ # img_flat = img_np.reshape(-1, 3)
39
+ # # Fit KMeans
40
+ # kmeans = KMeans(n_clusters=n_colors, random_state=42)
41
+ # labels = kmeans.fit_predict(img_flat)
42
+ # quantized_flat = kmeans.cluster_centers_[labels].astype(np.uint8)
43
+ # # Reshape back to original image shape
44
+ # quantized_img = quantized_flat.reshape(shape)
45
+ # return Image.fromarray(quantized_img)
requirements.txt CHANGED
@@ -1 +1,6 @@
1
  gradio
 
 
 
 
 
 
1
  gradio
2
+ numpy
3
+ scipy
4
+ Pillow
5
+ opencv-python
6
+ scikit-learn
resize_image.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+
3
+ def resize_image(image: Image.Image, size: tuple = (800, 800)) -> Image.Image:
4
+ """
5
+ Resize the input image to a fixed size (default 800x800 pixels).
6
+ Args:
7
+ image (PIL.Image.Image): Input image.
8
+ size (tuple): Target size as (width, height).
9
+ Returns:
10
+ PIL.Image.Image: Resized image.
11
+ """
12
+ return image.resize(size, Image.LANCZOS)