From 230836baf6b8475247f39742426a84315b250836 Mon Sep 17 00:00:00 2001 From: FrostX-Official Date: Mon, 14 Oct 2024 19:31:29 +0300 Subject: [PATCH] resizing progress bar, memory leak fix and optimization * added `img.close()` to static emotes which actually heavily improved performance and fixed memory leak * added resizing resolution preview and progress bar to resizing animated * added small note to emote download fail --- README-russian.md | 1 + README.md | 1 + main.py | 29 +++++++++++++++++++++++------ requirements.txt | 3 ++- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/README-russian.md b/README-russian.md index c2953fc..c4b2100 100644 --- a/README-russian.md +++ b/README-russian.md @@ -27,6 +27,7 @@ https://gist.github.com/a268e881f4ecf6cd1f2af5d4031e993d * Pillow * wget * requests +* progress Вы можете установить Python на https://python.org и запустить файл `install-requirements.bat`. diff --git a/README.md b/README.md index 876b260..3057a86 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ All static emotes will be converted to png * Pillow * wget * requests +* progress You can install python on https://python.org and run `install-requirements.bat` file. diff --git a/main.py b/main.py index 2aab3b5..0253377 100644 --- a/main.py +++ b/main.py @@ -20,6 +20,8 @@ import wget import os +from progress.bar import ShadyBar + # settings import settings @@ -72,20 +74,21 @@ def getEmotesFromEmoteSetId(id): wget.download(f"https://cdn.7tv.app/emote/{emote['id']}/4x.{newformat}",f"{folder}/{emote['name']}.{newformat}") except Exception as e: if isinstance(e, OSError): - print(Fore.RED+f"\nFailed to download {emote['name']}: {e} | SKIPPING...") + print(Fore.RED+f"\nFailed to download {emote['name']}: {e} | This might be because emote contains invalid characters | SKIPPING...\n\n") continue print(Fore.RED+f"\nFailed to download {emote['name']}: {e}") raise e try: - print(Fore.YELLOW+f"\nResizing...") if isAnimated: img: Image.Image = Image.open(f"{folder}/{emote['name']}.webp") img.info.pop('background', None) width, height = img.size highest = max(width,height) buh = rescale_to/highest + + print(Fore.YELLOW+f"\nResizing {width}x{height} -> {int(width*buh)}x{int(height*buh)} ...") if buh == 1: emoteSizeResult = os.path.getsize(f"{folder}/{emote['name']}.{newformat}") downloadTookSpace += emoteSizeResult @@ -98,19 +101,28 @@ def getEmotesFromEmoteSetId(id): continue scaled_up = (int(width*buh), int(height*buh)) - frames = ImageSequence.Iterator(img) + frames = [] + for frame in ImageSequence.Iterator(img): + frames.append(frame) new_frames = [] + framenum = 0 + framestotal = len(frames) + + bar = ShadyBar(Fore.YELLOW+"Resizing...", max=framestotal, suffix="%(index)d frames resized | %(percent).1f%%") + for frame in frames: + framenum += 1 thumbnail: Image.Image = frame.copy() thumbnail = thumbnail.resize(scaled_up, Image.Resampling.NEAREST) thumbnail.info.pop('background', None) new_frames.append(thumbnail) + bar.next() + bar.finish() - # Save output - om = new_frames[0] # Handle first frame separately - om.info = img.info # Copy sequence info + om = new_frames[0] + om.info = img.info om.save(f"{folder}/{emote['name']}.webp", "webp", save_all=True, append_images=new_frames, loop=0, duration=om.info["duration"], quality=webp_quality) img.close() #print(f"\n{emote['name']}: resized gif and saved as webp\n") @@ -119,8 +131,13 @@ def getEmotesFromEmoteSetId(id): width, height = img.size highest = max(width,height) buh = rescale_to/highest + print(Fore.YELLOW+f"\nResizing {width}x{height} -> {int(width*buh)}x{int(height*buh)} ...") + bar = ShadyBar(Fore.YELLOW+"Resizing...", max=1, suffix="%(index)d frames resized | %(percent).1f%%") scaled_up = (int(width*buh), int(height*buh)) img.resize(scaled_up, Image.Resampling.NEAREST).save(f"{folder}/{emote['name']}.{newformat}") + bar.next() + bar.finish() + img.close() except Exception as e: if str(e) != "illegal image mode": print(Fore.RED+f"\nFailed to resize and save {emote['name']}: {e}") diff --git a/requirements.txt b/requirements.txt index 35df2b9..147d5f8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ colorama Pillow wget -requests \ No newline at end of file +requests +progress \ No newline at end of file