Last active
December 13, 2021 18:17
-
-
Save misterhay/5258d602900fbe228bce32a92819f3e7 to your computer and use it in GitHub Desktop.
A Python script to extract images from Minecraft Education Edition portfolio exports
This file contains 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
# python -m pip install PyMuPDF Pillow | |
import os # for files and directories | |
import io # for bytes | |
import fitz #PyMuPDF | |
from PIL import Image #Pillow | |
for root, dirs, files in os.walk("."): | |
for filename in files: | |
if filename.endswith('.pdf'): | |
file = os.path.join(root, filename) | |
pdf_file = fitz.open(file) | |
for i in range(len(pdf_file)): | |
page = pdf_file[i] | |
image_list = page.get_images() | |
for image_index, img in enumerate(image_list, start=1): | |
xref = img[0] # get image XREF | |
base_image = pdf_file.extract_image(xref) | |
image_bytes = base_image["image"] # extract image bytes | |
image_ext = base_image["ext"] # get image extension | |
image = Image.open(io.BytesIO(image_bytes)) # load image bytes to PIL | |
image_filename = filename.split('.')[0] + '_' + str(i) + '_' + str(image_index) + '.' + image_ext | |
image.save(image_filename, image_ext) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment