Jeffgold commited on
Commit
42d403c
·
verified ·
1 Parent(s): 69f4980

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -12,17 +12,22 @@ generated_gifs = []
12
  def preprocess_image(image_data):
13
  # Extract the image and crop data from the ImageEditor output
14
  image = image_data["image"].convert('RGBA')
15
- crop_data = image_data["crop"]
16
 
17
  if crop_data:
18
  # Apply cropping if crop data is available
19
  left, top, width, height = crop_data
20
  image = image.crop((left, top, left + width, top + height))
21
 
22
- # Ensure the image is square by padding it
23
- size = max(image.size)
24
- new_image = Image.new("RGBA", (size, size), (255, 255, 255, 0))
25
- new_image.paste(image, ((size - image.width) // 2, (size - image.height) // 2))
 
 
 
 
 
26
 
27
  return new_image
28
 
@@ -36,10 +41,7 @@ def create_gif(editor1_output, editor2_output, transition_type):
36
  img1 = preprocess_image(editor1_output)
37
  img2 = preprocess_image(editor2_output)
38
 
39
- # Set size for the GIF
40
  size = (256, 256)
41
- img1 = img1.resize(size, Image.LANCZOS)
42
- img2 = img2.resize(size, Image.LANCZOS)
43
 
44
  if transition_type == "slide":
45
  # Calculate step size for consistent speed
@@ -128,8 +130,8 @@ with gr.Blocks() as iface:
128
  label="Edit Image 1",
129
  brush=gr.Brush(colors=["#ff0000", "#00ff00", "#0000ff"]),
130
  eraser=gr.Eraser(default_size=10),
131
- height=400,
132
- width="100%",
133
  crop_size="1:1",
134
  layers=True,
135
  type="pil"
@@ -139,8 +141,8 @@ with gr.Blocks() as iface:
139
  label="Edit Image 2",
140
  brush=gr.Brush(colors=["#ff0000", "#00ff00", "#0000ff"]),
141
  eraser=gr.Eraser(default_size=10),
142
- height=400,
143
- width="100%",
144
  crop_size="1:1",
145
  layers=True,
146
  type="pil"
@@ -157,7 +159,7 @@ with gr.Blocks() as iface:
157
  elem_id="output_gallery",
158
  columns=3,
159
  rows=2,
160
- height=400,
161
  object_fit="contain"
162
  )
163
 
 
12
  def preprocess_image(image_data):
13
  # Extract the image and crop data from the ImageEditor output
14
  image = image_data["image"].convert('RGBA')
15
+ crop_data = image_data.get("crop")
16
 
17
  if crop_data:
18
  # Apply cropping if crop data is available
19
  left, top, width, height = crop_data
20
  image = image.crop((left, top, left + width, top + height))
21
 
22
+ # Resize image to fit within 256x256 while maintaining aspect ratio
23
+ image.thumbnail((256, 256), Image.LANCZOS)
24
+
25
+ # Create a new 256x256 image with a transparent background
26
+ new_image = Image.new("RGBA", (256, 256), (255, 255, 255, 0))
27
+
28
+ # Paste the resized image at the center
29
+ offset = ((256 - image.width) // 2, (256 - image.height) // 2)
30
+ new_image.paste(image, offset)
31
 
32
  return new_image
33
 
 
41
  img1 = preprocess_image(editor1_output)
42
  img2 = preprocess_image(editor2_output)
43
 
 
44
  size = (256, 256)
 
 
45
 
46
  if transition_type == "slide":
47
  # Calculate step size for consistent speed
 
130
  label="Edit Image 1",
131
  brush=gr.Brush(colors=["#ff0000", "#00ff00", "#0000ff"]),
132
  eraser=gr.Eraser(default_size=10),
133
+ height=350,
134
+ width=350,
135
  crop_size="1:1",
136
  layers=True,
137
  type="pil"
 
141
  label="Edit Image 2",
142
  brush=gr.Brush(colors=["#ff0000", "#00ff00", "#0000ff"]),
143
  eraser=gr.Eraser(default_size=10),
144
+ height=350,
145
+ width=350,
146
  crop_size="1:1",
147
  layers=True,
148
  type="pil"
 
159
  elem_id="output_gallery",
160
  columns=3,
161
  rows=2,
162
+ height=350,
163
  object_fit="contain"
164
  )
165