The documentation for the PyMuPDF
library you're using has an explicit section on extracting images from PDF documents, with this example code (which is a bit too long to include here, and under the GPL anyway).
It simplifies to something like
import fitz
doc = fitz.open(filename)
seen_xrefs = set()
for page_num in range(doc.page_count):
for img in doc.get_page_images(page_num):
xref = img[0]
if xref in seen_xrefs:
continue
image = doc.extract_image(xref)
imgfile = f"img{xref:05d}.{image['ext']}"
with open(imgfile, "wb") as fout:
fout.write(image["image"])
seen_xrefs.add(xref)
print(f"Page {page_num}: {imgfile} ({image['width']} x {image['height']}")
when not taking masks and color spaces into account.