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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -23
app.py CHANGED
@@ -11,7 +11,7 @@ generated_gifs = []
11
 
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:
@@ -19,15 +19,10 @@ def preprocess_image(image_data):
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
 
@@ -37,11 +32,18 @@ def create_gif(editor1_output, editor2_output, transition_type):
37
  total_frames = 18 # Total number of frames
38
 
39
  try:
40
- # Preprocess images
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
@@ -52,10 +54,10 @@ def create_gif(editor1_output, editor2_output, transition_type):
52
  for i in range(0, full_width, step):
53
  frame = Image.new('RGBA', size)
54
  frame.paste(img1, (0, 0))
55
- frame.paste(img2.crop((0, 0, i, size[1])), (full_width - i, 0), mask=img2.crop((0, 0, i, size[1])))
56
 
57
  draw = ImageDraw.Draw(frame)
58
- draw.line((full_width - i, 0, full_width - i, size[1]), fill=(0, 255, 0), width=2)
59
 
60
  # Convert frame to P mode which is a palette-based image
61
  frame = frame.convert('P', palette=Image.ADAPTIVE)
@@ -64,11 +66,11 @@ def create_gif(editor1_output, editor2_output, transition_type):
64
  # Generate frames from right to left
65
  for i in range(full_width, step, -step):
66
  frame = Image.new('RGBA', size)
67
- frame.paste(img2, (0, 0))
68
- frame.paste(img1.crop((0, 0, i, size[1])), (full_width - i, 0), mask=img1.crop((0, 0, i, size[1])))
69
 
70
  draw = ImageDraw.Draw(frame)
71
- draw.line((full_width - i, 0, full_width - i, size[1]), fill=(0, 255, 0), width=2)
72
 
73
  # Convert frame to P mode which is a palette-based image
74
  frame = frame.convert('P', palette=Image.ADAPTIVE)
@@ -130,8 +132,8 @@ with gr.Blocks() as iface:
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,8 +143,8 @@ with gr.Blocks() as iface:
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,7 +161,7 @@ with gr.Blocks() as iface:
159
  elem_id="output_gallery",
160
  columns=3,
161
  rows=2,
162
- height=350,
163
  object_fit="contain"
164
  )
165
 
 
11
 
12
  def preprocess_image(image_data):
13
  # Extract the image and crop data from the ImageEditor output
14
+ image = image_data["composite"].convert('RGBA')
15
  crop_data = image_data.get("crop")
16
 
17
  if crop_data:
 
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
 
 
32
  total_frames = 18 # Total number of frames
33
 
34
  try:
35
+ # Open images
36
+ img1 = editor1_output["composite"].convert('RGBA')
37
+ img2 = editor2_output["composite"].convert('RGBA')
38
 
39
+ # Preprocess images to make them square and same size
40
+ img1 = preprocess_image(img1)
41
+ img2 = preprocess_image(img2)
42
+
43
+ # Set size for the GIF
44
  size = (256, 256)
45
+ img1 = img1.resize(size, Image.LANCZOS)
46
+ img2 = img2.resize(size, Image.LANCZOS)
47
 
48
  if transition_type == "slide":
49
  # Calculate step size for consistent speed
 
54
  for i in range(0, full_width, step):
55
  frame = Image.new('RGBA', size)
56
  frame.paste(img1, (0, 0))
57
+ frame.paste(img2.crop((i, 0, full_width, size[1])), (i, 0), mask=img2.crop((i, 0, full_width, size[1])))
58
 
59
  draw = ImageDraw.Draw(frame)
60
+ draw.line((i, 0, i, size[1]), fill=(0, 255, 0), width=2)
61
 
62
  # Convert frame to P mode which is a palette-based image
63
  frame = frame.convert('P', palette=Image.ADAPTIVE)
 
66
  # Generate frames from right to left
67
  for i in range(full_width, step, -step):
68
  frame = Image.new('RGBA', size)
69
+ frame.paste(img1, (0, 0))
70
+ frame.paste(img2.crop((i, 0, full_width, size[1])), (i, 0), mask=img2.crop((i, 0, full_width, size[1])))
71
 
72
  draw = ImageDraw.Draw(frame)
73
+ draw.line((i, 0, i, size[1]), fill=(0, 255, 0), width=2)
74
 
75
  # Convert frame to P mode which is a palette-based image
76
  frame = frame.convert('P', palette=Image.ADAPTIVE)
 
132
  label="Edit Image 1",
133
  brush=gr.Brush(colors=["#ff0000", "#00ff00", "#0000ff"]),
134
  eraser=gr.Eraser(default_size=10),
135
+ height=400,
136
+ width="100%",
137
  crop_size="1:1",
138
  layers=True,
139
  type="pil"
 
143
  label="Edit Image 2",
144
  brush=gr.Brush(colors=["#ff0000", "#00ff00", "#0000ff"]),
145
  eraser=gr.Eraser(default_size=10),
146
+ height=400,
147
+ width="100%",
148
  crop_size="1:1",
149
  layers=True,
150
  type="pil"
 
161
  elem_id="output_gallery",
162
  columns=3,
163
  rows=2,
164
+ height=400,
165
  object_fit="contain"
166
  )
167