Skip to content

Instantly share code, notes, and snippets.

@thurask
Last active July 12, 2019 12:22
Show Gist options
  • Save thurask/5eab001ee8e4d4ceb0823ca3ff0d0e5b to your computer and use it in GitHub Desktop.
Save thurask/5eab001ee8e4d4ceb0823ca3ff0d0e5b to your computer and use it in GitHub Desktop.
import os
import subprocess
MAGICDICT = {b"GIF8": ".gif", b"\x89PNG": ".png"}
def single_convert(afile):
with open(afile, "rb") as thefile:
header = thefile.read(4)
if header in MAGICDICT.keys():
os.rename(afile, "{0}{1}".format(afile, MAGICDICT[header]))
else:
print(afile, header)
def folder_convert(folder):
icondir = os.path.join(folder, "icons")
thumbdir = os.path.join(folder, "thumbnails")
preview = os.path.join(folder, "icon")
for icon in os.listdir(icondir):
single_convert(os.path.join(icondir, icon))
for thumb in os.listdir(thumbdir):
single_convert(os.path.join(thumbdir, thumb))
single_convert(preview)
def magick_convert(folder):
thumbdir = os.path.join(folder, "thumbnails")
oldthumbs = [os.path.join(thumbdir, x) for x in os.listdir(thumbdir)]
newthumbs = [x.replace(".png", ".512.png") for x in oldthumbs]
for thumb, newthumb in zip(oldthumbs, newthumbs):
subprocess.run("magick convert -background none -quality 100 -resize 512x512 {0} {1}".format(thumb, newthumb))
for thumb in oldthumbs:
os.remove(thumb)
for thumb, newthumb in zip(oldthumbs, newthumbs):
os.rename(newthumb, thumb)
def bulk_convert2():
folders = [os.path.abspath(x) for x in os.listdir() if os.path.isdir(x)]
for folder in folders:
print("UPSCALING: {0}".format(folder))
magick_convert(folder)
def bulk_convert():
folders = [os.path.abspath(x) for x in os.listdir() if os.path.isdir(x)]
for folder in folders:
print("CONVERTING: {0}".format(folder))
folder_convert(folder)
if __name__ == "__main__":
bulk_convert()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment