dramp77 commited on
Commit
52ce05b
1 Parent(s): 1ff9fa6

change code

Browse files
Files changed (2) hide show
  1. app.py +38 -12
  2. other.py +8 -0
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import gradio as gr
2
- import numpy as np
3
  import PIL
4
- from PIL import Image
 
 
5
 
6
  size_options ={"1200 x 1500": (1200, 1500),
7
  "1200 x 1200": (1200, 1200),
@@ -21,23 +22,48 @@ size_options ={"1200 x 1500": (1200, 1500),
21
  "Tiktok Pangle - 720 x 1067": (720, 1067),
22
  "Tiktok Pangle - 480 x 320": (480, 320)}
23
 
24
- def change_size(image, size):
25
- new_size = size_options[size]
26
- image = image.resize(new_size, PIL.Image.Resampling.LANCZOS) # def change size
27
- return image
 
 
 
28
 
29
- def gradio_interface():# this is for gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  with gr.Blocks() as demo:
31
- gr.Markdown("# Image Resizer with Predefined Sizes")
32
  with gr.Row():
33
  image_input = gr.Image(type="pil", label="Upload Image")
34
- size_input = gr.Dropdown(list(size_options.keys()), label="Select Size")
35
- image_output = gr.Image(type="pil", label="Resized Image")
 
 
36
  submit_button = gr.Button("Resize Image")
37
 
38
- submit_button.click(fn=change_size, inputs=[image_input, size_input], outputs=image_output)
 
 
39
 
40
  return demo
41
 
42
  demo = gradio_interface()
43
- demo.launch()
 
1
  import gradio as gr
 
2
  import PIL
3
+ from PIL import Image, ImageOps
4
+ from collections import Counter
5
+ import numpy as np
6
 
7
  size_options ={"1200 x 1500": (1200, 1500),
8
  "1200 x 1200": (1200, 1200),
 
22
  "Tiktok Pangle - 720 x 1067": (720, 1067),
23
  "Tiktok Pangle - 480 x 320": (480, 320)}
24
 
25
+ def get_dominant_color(image):
26
+ image = image.resize((50, 50)) # Resize to speed up
27
+ pixels = np.array(image)
28
+ pixels = pixels.reshape((-1, 3))
29
+ counter = Counter(map(tuple, pixels))
30
+ dominant_color = counter.most_common(1)[0][0]
31
+ return dominant_color
32
 
33
+ def change_size(image, selected_size, size_align):
34
+ target_size = size_options[selected_size]
35
+
36
+ # Maintain aspect ratio without stretching
37
+ image.thumbnail(target_size, PIL.Image.Resampling.LANCZOS)
38
+
39
+ dominant_color = get_dominant_color(image)
40
+
41
+ new_image = Image.new("RGB", target_size, dominant_color)
42
+
43
+ # Calculate offset based on alignment choice
44
+ x_offset = (target_size[0] - image.width) // 2 if size_align == "Center" else 0 if size_align == "Left" else target_size[0] - image.width
45
+ y_offset = (target_size[1] - image.height) // 2 if size_align == "Center" else 0 if size_align == "Top" else target_size[1] - image.height
46
+
47
+ new_image.paste(image, (x_offset, y_offset))
48
+
49
+ return new_image
50
+
51
+ def gradio_interface():
52
  with gr.Blocks() as demo:
53
+ gr.Markdown("# Advanced Image Resizer with Background and Margins")
54
  with gr.Row():
55
  image_input = gr.Image(type="pil", label="Upload Image")
56
+ size_input = gr.Dropdown(list(size_options.keys()), label="Size Options")
57
+ with gr.Row():
58
+ size_alignment = gr.Radio(["Left", "Center", "Right"], value="Center", label="Size Alignment")
59
+ image_output = gr.Image(type="pil", label="Resized Image with Background")
60
  submit_button = gr.Button("Resize Image")
61
 
62
+ submit_button.click(fn=change_size,
63
+ inputs=[image_input, size_input, size_alignment],
64
+ outputs=image_output)
65
 
66
  return demo
67
 
68
  demo = gradio_interface()
69
+ demo.launch(share=True)
other.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ def template_graphics(): # processing template graphics with blank/color choices
2
+ pass
3
+
4
+ def logic_needed():
5
+ pass
6
+
7
+ def processing_image(): #processing the image
8
+ pass