Skip to content

Instantly share code, notes, and snippets.

@shawngraham
Created August 23, 2019 19:10
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 shawngraham/e9fedb7c51b040eac00f8730cb9fef04 to your computer and use it in GitHub Desktop.
Save shawngraham/e9fedb7c51b040eac00f8730cb9fef04 to your computer and use it in GitHub Desktop.
"""
pip instal fitz
pip install PyMuPDF
"""
import fitz
doc = fitz.open("file.pdf")
for i in range(len(doc)):
for img in doc.getPageImageList(i):
xref = img[0]
pix = fitz.Pixmap(doc, xref)
if pix.n < 5: # this is GRAY or RGB
pix.writePNG("p%s-%s.png" % (i, xref))
else: # CMYK: convert to RGB first
pix1 = fitz.Pixmap(fitz.csRGB, pix)
pix1.writePNG("p%s-%s.png" % (i, xref))
pix1 = None
pix = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment