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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -9,11 +9,21 @@ import time
9
  # Global variable to store generated GIFs
10
  generated_gifs = []
11
 
12
- def preprocess_image(image):
 
 
 
 
 
 
 
 
 
13
  # Ensure the image is square by padding it
14
  size = max(image.size)
15
  new_image = Image.new("RGBA", (size, size), (255, 255, 255, 0))
16
  new_image.paste(image, ((size - image.width) // 2, (size - image.height) // 2))
 
17
  return new_image
18
 
19
  def create_gif(editor1_output, editor2_output, transition_type):
@@ -22,13 +32,9 @@ def create_gif(editor1_output, editor2_output, transition_type):
22
  total_frames = 18 # Total number of frames
23
 
24
  try:
25
- # Open images
26
- img1 = editor1_output["composite"].convert('RGBA')
27
- img2 = editor2_output["composite"].convert('RGBA')
28
-
29
- # Preprocess images to make them square and same size
30
- img1 = preprocess_image(img1)
31
- img2 = preprocess_image(img2)
32
 
33
  # Set size for the GIF
34
  size = (256, 256)
@@ -44,10 +50,10 @@ def create_gif(editor1_output, editor2_output, transition_type):
44
  for i in range(0, full_width, step):
45
  frame = Image.new('RGBA', size)
46
  frame.paste(img1, (0, 0))
47
- frame.paste(img2.crop((i, 0, full_width, size[1])), (i, 0), mask=img2.crop((i, 0, full_width, size[1])))
48
 
49
  draw = ImageDraw.Draw(frame)
50
- draw.line((i, 0, i, size[1]), fill=(0, 255, 0), width=2)
51
 
52
  # Convert frame to P mode which is a palette-based image
53
  frame = frame.convert('P', palette=Image.ADAPTIVE)
@@ -56,11 +62,11 @@ def create_gif(editor1_output, editor2_output, transition_type):
56
  # Generate frames from right to left
57
  for i in range(full_width, step, -step):
58
  frame = Image.new('RGBA', size)
59
- frame.paste(img1, (0, 0))
60
- frame.paste(img2.crop((i, 0, full_width, size[1])), (i, 0), mask=img2.crop((i, 0, full_width, size[1])))
61
 
62
  draw = ImageDraw.Draw(frame)
63
- draw.line((i, 0, i, size[1]), fill=(0, 255, 0), width=2)
64
 
65
  # Convert frame to P mode which is a palette-based image
66
  frame = frame.convert('P', palette=Image.ADAPTIVE)
 
9
  # Global variable to store generated GIFs
10
  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["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
 
29
  def create_gif(editor1_output, editor2_output, transition_type):
 
32
  total_frames = 18 # Total number of frames
33
 
34
  try:
35
+ # Preprocess images
36
+ img1 = preprocess_image(editor1_output)
37
+ img2 = preprocess_image(editor2_output)
 
 
 
 
38
 
39
  # Set size for the GIF
40
  size = (256, 256)
 
50
  for i in range(0, full_width, step):
51
  frame = Image.new('RGBA', size)
52
  frame.paste(img1, (0, 0))
53
+ frame.paste(img2.crop((0, 0, i, size[1])), (full_width - i, 0), mask=img2.crop((0, 0, i, size[1])))
54
 
55
  draw = ImageDraw.Draw(frame)
56
+ draw.line((full_width - i, 0, full_width - i, size[1]), fill=(0, 255, 0), width=2)
57
 
58
  # Convert frame to P mode which is a palette-based image
59
  frame = frame.convert('P', palette=Image.ADAPTIVE)
 
62
  # Generate frames from right to left
63
  for i in range(full_width, step, -step):
64
  frame = Image.new('RGBA', size)
65
+ frame.paste(img2, (0, 0))
66
+ frame.paste(img1.crop((0, 0, i, size[1])), (full_width - i, 0), mask=img1.crop((0, 0, i, size[1])))
67
 
68
  draw = ImageDraw.Draw(frame)
69
+ draw.line((full_width - i, 0, full_width - i, size[1]), fill=(0, 255, 0), width=2)
70
 
71
  # Convert frame to P mode which is a palette-based image
72
  frame = frame.convert('P', palette=Image.ADAPTIVE)