Akjava commited on
Commit
d257a3c
·
1 Parent(s): 93a27a5

fix broken resize,add single file convert support

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -94,24 +94,33 @@ def process_create_gif(images,duration=100, quantize=False,disposal=1,blend=0,lo
94
 
95
  return output_buffer.getvalue()
96
 
97
- def save_to_image(image,extension="png"):
98
  id = get_image_id(image)
99
  path = os.path.join(dir_name,f"{id}.{extension}")
100
- image.save(path)
 
 
 
101
  return path
102
 
103
 
104
  def convert_webp_to_images(webp_path):
105
- # 各フレームをリストに保存
106
  frames = []
107
  durations = []
108
  Image.init()
109
  image = Image.open(webp_path)
110
- for i in range(image.n_frames):
111
- image.seek(i)
112
- frame = image.copy()
113
- frames.append(frame)
114
- durations.append(frame.info["duration"])
 
 
 
 
 
 
115
  return frames,durations
116
 
117
  def buffer_to_id(buffer_value):
@@ -139,10 +148,10 @@ def process_images(input_path,same_size=False,image_width=128,file_format="webp"
139
  new_width,new_height = frames[0].size
140
  if not same_size and image_width!=new_width:
141
 
142
- ratio = new_width/new_height
143
  new_height = int(image_width*ratio)
144
  new_width = image_width
145
- #print(f"new size {new_width} x {new_height}")
146
  for frame in frames:
147
  new_frame = frame.resize((new_width,new_height))# extremly slow Image.LANCZOS
148
  new_frames.append(new_frame)
@@ -176,8 +185,9 @@ def process_images(input_path,same_size=False,image_width=128,file_format="webp"
176
 
177
  images.append((gif_path,"gif"))
178
  else:
 
179
  for i ,frame in enumerate(new_frames):
180
- path = save_to_image(frame)
181
  images.append((path,f"index {i}"))
182
 
183
  return images
@@ -254,7 +264,7 @@ with gr.Blocks(css=css, elem_id="demo-container") as demo:
254
  # size slider
255
 
256
  file_format=gr.Dropdown(
257
- ["webp", "apng", "gif","images"], label="Animation Format", info="Convert to Animattion" )
258
 
259
  same_size = gr.Checkbox(label="Same Size",value=False)
260
 
 
94
 
95
  return output_buffer.getvalue()
96
 
97
+ def save_to_image(image,extension="png",quality=0.8):
98
  id = get_image_id(image)
99
  path = os.path.join(dir_name,f"{id}.{extension}")
100
+ if extension == "jpg" or extension == "jpeg" or extension == "webp":
101
+ image.save(path,quality=quality)
102
+ else:
103
+ image.save(path)
104
  return path
105
 
106
 
107
  def convert_webp_to_images(webp_path):
108
+ # add list
109
  frames = []
110
  durations = []
111
  Image.init()
112
  image = Image.open(webp_path)
113
+ if hasattr(image,"n_frames"):
114
+ for i in range(image.n_frames):
115
+ image.seek(i)
116
+ frame = image.copy()
117
+ frames.append(frame)
118
+ duration = getattr(frame.info,"duration",100)
119
+ durations.append(duration)
120
+ else:# webp never happen?
121
+ frames.append(image)
122
+ durations.append(100)#default
123
+
124
  return frames,durations
125
 
126
  def buffer_to_id(buffer_value):
 
148
  new_width,new_height = frames[0].size
149
  if not same_size and image_width!=new_width:
150
 
151
+ ratio = new_height/new_width
152
  new_height = int(image_width*ratio)
153
  new_width = image_width
154
+ #print(f"ratio = {ratio} new size {new_width} x {new_height}")
155
  for frame in frames:
156
  new_frame = frame.resize((new_width,new_height))# extremly slow Image.LANCZOS
157
  new_frames.append(new_frame)
 
185
 
186
  images.append((gif_path,"gif"))
187
  else:
188
+ f,extension = file_format.split("-")
189
  for i ,frame in enumerate(new_frames):
190
+ path = save_to_image(frame,extension,webp_quality)
191
  images.append((path,f"index {i}"))
192
 
193
  return images
 
264
  # size slider
265
 
266
  file_format=gr.Dropdown(
267
+ ["webp", "apng", "gif","images-png","images-jpg"], label="Animation Format", info="Convert to Animattion" )
268
 
269
  same_size = gr.Checkbox(label="Same Size",value=False)
270