Skip to content

Instantly share code, notes, and snippets.

@agrif
Created October 30, 2021 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agrif/fec0377c0a405494594b61c2e84ccfce to your computer and use it in GitHub Desktop.
Save agrif/fec0377c0a405494594b61c2e84ccfce to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
import io
import os.path
import shutil
import zipfile
from PIL import Image
parser = argparse.ArgumentParser(
description='Convert a CHITUBOX zip into a Lite3DP-S1 compatible print.')
parser.add_argument('src',help='source zip file')
parser.add_argument('dest', help='SD card path')
parser.add_argument('-v', '--verbose', action='store_true', help='be talkative')
if __name__ == '__main__':
p = parser.parse_args()
always_print = print
def print(*args, **kwargs):
if p.verbose:
always_print(*args, **kwargs)
name = os.path.basename(os.path.splitext(p.src)[0])
src = zipfile.ZipFile(p.src)
dest = os.path.join(p.dest, name)
try:
os.mkdir(dest)
except FileExistsError:
pass
for info in src.infolist():
if info.is_dir():
continue
infodest = os.path.join(dest, info.filename)
if info.filename.endswith('.png'):
infodest = os.path.splitext(infodest)[0] + '.bmp'
print(infodest)
# read it all at once -- PIL can seek, and zipfile doesn't like it
im = Image.open(io.BytesIO(src.read(info)))
im.convert('RGB').save(infodest)
else:
print(infodest)
with src.open(info) as fsrc:
with open(infodest, 'wb') as fdest:
shutil.copyfileobj(fsrc, fdest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment