Created
June 11, 2020 12:50
-
-
Save darktohka/23742be43999155e33746df0662459fc to your computer and use it in GitHub Desktop.
Converts PANDORA/Spotify image resources to PNG recursively.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from panda3d.core import PNMImage, Filename | |
| from PIL import Image | |
| from functools import reduce | |
| import operator | |
| import shutil, math, os | |
| # PLEASE RUN WITH PYTHON 3 | |
| # Made by Disyer 2020/06/11 for PANDORA | |
| # Version 1.1: duplicates now saved | |
| # Converts all .TIF, .RGB, .RGBA and .PIC resources into their PNG equivalents | |
| # Usage: Run with newer version of Panda3D in root folder of PANDORA | |
| # Change this to .jpg if you require .jpg files | |
| # WARNING! JPG does not support .RGBA files well as they require transparency. | |
| # Use with caution. | |
| extension = '.png' | |
| # Change this to "true" if you want to skip rewriting files that have already been converted. | |
| # Warning: stopping the script in the middle of execution will corrupt the last file | |
| skip_converted = False | |
| def convert_pnm(from_file, to_file): | |
| img = PNMImage() | |
| img.read(Filename(from_file)) | |
| img.write(Filename(to_file)) | |
| for root, _, files in os.walk('.'): | |
| for file in files: | |
| lower = file.lower() | |
| if not lower.endswith('.tif') and not lower.endswith('.rgb') and not lower.endswith('.rgba') and not lower.endswith('.pic') and not lower.endswith('.png'): | |
| continue | |
| full_file = os.path.join(root, file) | |
| new_root = os.path.join('converted_images', root[2:]) | |
| new_file = os.path.join(new_root, os.path.splitext(file)[0] + extension) | |
| if not os.path.exists(new_root): | |
| os.makedirs(new_root) | |
| print('Converting...', full_file) | |
| old_file = None | |
| if os.path.exists(new_file): | |
| print('Already exists...', new_file) | |
| if skip_converted: | |
| continue | |
| old_file = new_file | |
| i = 1 | |
| while True: | |
| new_file = os.path.join(new_root, os.path.splitext(file)[0] + '_duplicate_{}.png'.format(i)) | |
| if not os.path.exists(new_file): | |
| break | |
| i += 1 | |
| if lower.endswith('.pic'): | |
| convert_pnm(full_file, new_file) | |
| elif lower.endswith('.png'): | |
| shutil.copyfile(full_file, new_file) | |
| else: | |
| try: | |
| img = Image.open(full_file) | |
| img.save(new_file) | |
| except: | |
| print('Could not load {} with Pillow, trying PNMImage...'.format(new_file)) | |
| convert_pnm(full_file, new_file) | |
| if old_file is not None: | |
| h1 = Image.open(old_file).histogram() | |
| h2 = Image.open(new_file).histogram() | |
| rms = math.sqrt(reduce(operator.add, map(lambda a, b: (a - b) ** 2, h1, h2)) / len(h1)) | |
| if rms == 0.0: | |
| # Remove duplicate since image is a carbon copy | |
| os.remove(new_file) | |
| else: | |
| print('Writing duplicate...') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment