Skip to content

Commit fd5e861

Browse files
committed
Cropping extracted PNG frames is now optional.
Some users do not want to automatically crop PNG sequences, added cropping as a checkbox toggle for these users. This is mostly related to the fact some people may want to remake spritesheets but more optimized or have an easier time lining up all animations into a single animated image for showcase purposes. This cropping option only affects PNG sequences, it won't affect "single frame animations" as it's useless on them anyways. It also doesn't affect GIFs/WebPs as they should be cropped automatically at all times.
1 parent 074ba90 commit fd5e861

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

TextureAtlas to GIF and Frames.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ def store_input():
421421

422422
tk.Button(new_window, text="OK", command=store_input).pack()
423423

424-
def process_directory(input_dir, output_dir, progress_var, tk_root, create_gif, create_webp, set_framerate, set_loopdelay, set_minperiod, set_scale, set_threshold, keep_frames, var_delay, hq_colors):
424+
def process_directory(input_dir, output_dir, progress_var, tk_root, create_gif, create_webp, set_framerate, set_loopdelay, set_minperiod, set_scale, set_threshold, keep_frames, crop_pngs, var_delay, hq_colors):
425425

426426
total_frames_generated = 0
427427
total_anims_generated = 0
@@ -458,7 +458,7 @@ def process_directory(input_dir, output_dir, progress_var, tk_root, create_gif,
458458
threshold = settings.get('threshold', set_threshold)
459459
scale = settings.get('scale', set_scale)
460460
indices = settings.get('indices')
461-
future = executor.submit(extract_sprites, os.path.join(input_dir, filename), xml_path if os.path.isfile(xml_path) else txt_path, sprite_output_dir, create_gif, create_webp, fps, delay, period, scale, threshold, indices, frames, var_delay, hq_colors)
461+
future = executor.submit(extract_sprites, os.path.join(input_dir, filename), xml_path if os.path.isfile(xml_path) else txt_path, sprite_output_dir, create_gif, create_webp, fps, delay, period, scale, threshold, indices, frames, crop_pngs, var_delay, hq_colors)
462462
futures.append(future)
463463

464464
for future in concurrent.futures.as_completed(futures):
@@ -495,7 +495,7 @@ def process_directory(input_dir, output_dir, progress_var, tk_root, create_gif,
495495
)
496496

497497
## Extraction logic
498-
def extract_sprites(atlas_path, metadata_path, output_dir, create_gif, create_webp, set_framerate, set_loopdelay, set_minperiod, set_scale, set_threshold, set_indices, keep_frames, var_delay, hq_colors):
498+
def extract_sprites(atlas_path, metadata_path, output_dir, create_gif, create_webp, set_framerate, set_loopdelay, set_minperiod, set_scale, set_threshold, set_indices, keep_frames, crop_pngs, var_delay, hq_colors):
499499
frames_generated = 0
500500
anims_generated = 0
501501
sprites_failed = 0
@@ -617,8 +617,9 @@ def extract_sprites(atlas_path, metadata_path, output_dir, create_gif, create_we
617617
bbox = frame_image.getbbox()
618618
if bbox is None:
619619
continue
620-
cropped_frame_image = scale_image(frame_image.crop(bbox), scale)
621-
cropped_frame_image.save(frame_filename)
620+
if bbox:
621+
final_frame_image = scale_image(frame_image.crop(bbox), scale)
622+
final_frame_image.save(frame_filename)
622623
frames_generated += 1
623624
continue
624625
else:
@@ -630,9 +631,12 @@ def extract_sprites(atlas_path, metadata_path, output_dir, create_gif, create_we
630631
bbox = frame_image.getbbox()
631632
if bbox is None:
632633
continue
633-
cropped_frame_image = scale_image(frame_image.crop(bbox), scale)
634+
if crop_pngs and bbox:
635+
final_frame_image = scale_image(frame_image.crop(bbox), scale)
636+
else:
637+
final_frame_image = scale_image(frame_image, scale)
634638
os.makedirs(frames_folder, exist_ok=True)
635-
cropped_frame_image.save(frame_filename)
639+
final_frame_image.save(frame_filename)
636640
frames_generated += 1
637641

638642
if create_gif or create_webp:
@@ -799,6 +803,7 @@ def create_scrollable_help_window():
799803
"Alpha Threshold (GIFs only):\nThis setting adjusts the level of transparency applied to pixels in GIF images.\nThe threshold value determines the cutoff point for transparency.\nPixels with an alpha value below this threshold become fully transparent, while those above the threshold become fully opaque.\n\n"
800804
"Indices (not available in global settings):\nSelect the frame indices to use in the animation by typing a comma-separated list of non-negative integers.\n\n"
801805
"Keep Individual Frames:\nSelect the frames of the animation to save by typing 'all', 'first', 'last', 'none', or a comma-separated list of integers or integer ranges. Negative numbers count from the final frame.\n\n"
806+
"Crop Individual Frames:\nCrops every extracted png frame. (This doesn't affect GIFs, WebP's or single frame animations)\n\n"
802807
"Show User Settings:\nOpens a window displaying a list of animations and spritesheets with settings that override the global configuration.\n\n"
803808
"Start Process:\nBegins the tasks you have selected for processing.\n\n"
804809
"_________________________________________ Menubar: File _________________________________________\n\n"
@@ -886,7 +891,7 @@ def on_closing():
886891

887892
menubar = tk.Menu(root)
888893
root.title("TextureAtlas to GIF and Frames")
889-
root.geometry("900x540")
894+
root.geometry("900x560")
890895
root.resizable(False, False)
891896
root.protocol("WM_DELETE_WINDOW", on_closing)
892897
root.config(menu=menubar)
@@ -994,17 +999,20 @@ def on_closing():
994999
keepframes_label = tk.Label(root, text="Keep individual frames:")
9951000
keepframes_label.pack()
9961001
keepframes_entry = tk.Entry(root, textvariable=keep_frames)
997-
keepframes_entry.pack()
1002+
keepframes_entry.pack(pady=2)
1003+
1004+
crop_pngs = tk.BooleanVar()
1005+
crop_pngs_checkbox = tk.Checkbutton(root, text="Crop individual frames", variable=crop_pngs)
1006+
crop_pngs_checkbox.pack(pady=1)
9981007

9991008
button_frame = tk.Frame(root)
10001009
button_frame.pack(pady=8)
10011010

10021011
show_user_settings = tk.Button(button_frame, text="Show User Settings", command=create_settings_window)
10031012
show_user_settings.pack(side=tk.LEFT, padx=4)
10041013

1005-
process_button = tk.Button(button_frame, text="Start process", cursor="hand2", command=lambda: process_directory(input_dir.get(), output_dir.get(), progress_var, root, create_gif.get(), create_webp.get(), set_framerate.get(), set_loopdelay.get(), set_minperiod.get(), set_scale.get(), set_threshold.get(), keep_frames.get(), variable_delay.get(), better_colors.get()))
1006-
process_button.pack(side=tk.LEFT, padx=4)
1007-
1014+
process_button = tk.Button(button_frame, text="Start process", cursor="hand2", command=lambda: process_directory(input_dir.get(), output_dir.get(), progress_var, root, create_gif.get(), create_webp.get(), set_framerate.get(), set_loopdelay.get(), set_minperiod.get(), set_scale.get(), set_threshold.get(), keep_frames.get(), crop_pngs.get(), variable_delay.get(), better_colors.get()))
1015+
process_button.pack(side=tk.LEFT, padx=2)
10081016

10091017
author_label = tk.Label(root, text="Project started by AutisticLulu")
10101018
author_label.pack(side='bottom')

0 commit comments

Comments
 (0)