Skip to content

Instantly share code, notes, and snippets.

@ChunkLightTuna
Created May 10, 2023 06:41
Show Gist options
  • Save ChunkLightTuna/b34bf2a10eb7bbe396498fe89758bbdb to your computer and use it in GitHub Desktop.
Save ChunkLightTuna/b34bf2a10eb7bbe396498fe89758bbdb to your computer and use it in GitHub Desktop.
Create thumbnails for dd2vtt files
#!/usr/bin/env python3
"""
Create thumbnails for dd2vtt files.
Usage: python3 dd2vtt_thumbnail.py some_map.dd2vtt Castle_*.dd2vtt
"""
import base64
import glob
import json
import sys
import time
from concurrent.futures import ProcessPoolExecutor
from io import BytesIO
from PIL import Image
Image.MAX_IMAGE_PIXELS = None
def process_map(map_name):
with open(map_name, 'r') as file:
data = json.load(file)
image_data = base64.b64decode(data['image'])
image = Image.open(BytesIO(image_data))
image.thumbnail((256, 256))
image_name = map_name[:-7].lstrip('./\\') + '.png'
image.save(image_name)
return image_name
def main():
if len(sys.argv) < 2:
print('please call w/ filenames!')
sys.exit()
start_time = int(time.time())
map_names = [file_name for arg in sys.argv[1:] for file_name in glob.glob(arg) if file_name.endswith('.dd2vtt')]
with ProcessPoolExecutor() as executor:
for image_name in executor.map(process_map, map_names):
print(image_name)
print(f'created thumbnails for {len(map_names)} maps in {str(int(time.time()) - start_time)} seconds.')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment