[Python] Why is the embedded image extracted from pypdf of lower resolution than when i used...

Discussão em 'Python' iniciado por Stack, Maio 30, 2025.

  1. Stack

    Stack Membro Participativo

    I have a pdf document which I want to extract embedded images to be used.

    I used the code below which to extract the embedded images from the first page of the pdf document:

    from pypdf import PdfReader

    reader = PdfReader(io.BytesIO(file_data))

    page = reader.pages[0]

    for count, image_obj in enumerate(page.images):
    ext = image_obj.name.lower() # e.g., "jpeg", "png"
    mime_type = f"image/{'jpeg' if ext == 'jpg' else ext}"

    filename = f"extracted_page_img{count}.{ext}"
    with open(filename, "wb") as f:
    f.write(image_obj.data)


    I then used pdf2image to convert the first page of pdf to image as follows:

    images = convert_from_bytes(file_data, fmt="png", dpi=1200)
    images[0].save("output.png", format="PNG")


    However, when i compare the embedded image resolution, the resolution of that in the output from the pdf2image is much clearer and readable. Why is this so? I thought the first method would extract the original embedded image which would thus be much clearer.

    Continue reading...

Compartilhe esta Página