Skip to content

Instantly share code, notes, and snippets.

@live-wire
Created September 10, 2019 21:28
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 live-wire/d9b475f469172107c936439e69c773cb to your computer and use it in GitHub Desktop.
Save live-wire/d9b475f469172107c936439e69c773cb to your computer and use it in GitHub Desktop.
JPEG-2000 to JPEG conversion using Python - PIL
# Expects jp2 files in folder jp2-files/
# jpeg files are generated in folder jpeg-files/
import os
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
input_folder = "jp2-files"
output_folder = "jpeg-files"
if not os.path.exists(output_folder):
os.makedirs(output_folder)
if not os.path.exists(input_folder):
os.makedirs(input_folder)
input_files = os.listdir(input_folder)
ignore = ['.DS_Store']
input_files = list(filter(lambda x: x not in ignore, input_files))
for f in input_files:
print("Processing:", f)
im = Image.open(os.path.join(input_folder, f)) # just a path to a JP2 file
newfile = os.path.join(output_folder, f[:f.rfind(".")]+".jpeg")
if not os.path.isfile(newfile):
im.save(newfile)
else:
print("Already processed:", f)
im.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment