Skip to content

Instantly share code, notes, and snippets.

@misterhay
Last active December 13, 2021 18:17
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 misterhay/5258d602900fbe228bce32a92819f3e7 to your computer and use it in GitHub Desktop.
Save misterhay/5258d602900fbe228bce32a92819f3e7 to your computer and use it in GitHub Desktop.
A Python script to extract images from Minecraft Education Edition portfolio exports
# 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