Created
September 10, 2019 21:28
-
-
Save live-wire/d9b475f469172107c936439e69c773cb to your computer and use it in GitHub Desktop.
JPEG-2000 to JPEG conversion using Python - PIL
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
# 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