Skip to content

Commit

Permalink
resizing progress bar, memory leak fix and optimization
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
FrostX-Official committed Oct 14, 2024
1 parent a4b4348 commit 230836b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions README-russian.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ https://gist.github.com/a268e881f4ecf6cd1f2af5d4031e993d
* Pillow
* wget
* requests
* progress

Вы можете установить Python на https://python.org
и запустить файл `install-requirements.bat`.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 23 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import wget
import os

from progress.bar import ShadyBar

# settings

import settings
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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}")
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
colorama
Pillow
wget
requests
requests
progress

0 comments on commit 230836b

Please sign in to comment.