Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading and writing PDF file damages it #180

Open
6801318d8d opened this issue Oct 29, 2023 · 3 comments
Open

Reading and writing PDF file damages it #180

6801318d8d opened this issue Oct 29, 2023 · 3 comments

Comments

@6801318d8d
Copy link

#!/usr/bin/env python3

import typing
from borb.pdf import PDF, Document

doc: typing.Optional[Document] = None
with open("test.pdf", "rb") as pdf_file_handle:
    doc = PDF.loads(pdf_file_handle)
assert doc is not None
with open("test2.pdf", "wb") as pdf_file_handle:
    PDF.dumps(pdf_file_handle, doc)

test.pdf:
image

test2.pdf:
image

test.pdf

@DrPlanecraft
Copy link

prolly something to do with PIL, looks like the image is loaded BGR instead of RGB

@DrPlanecraft
Copy link

DrPlanecraft commented Nov 20, 2023

#!/usr/bin/env python3

import typing
from borb.pdf import PDF, Document

doc: typing.Optional[Document] = None
with open("test.pdf", "rb") as pdf_file_handle:
    doc = PDF.loads(pdf_file_handle)
assert doc is not None
with open("test2.pdf", "wb") as pdf_file_handle:
    PDF.dumps(pdf_file_handle, doc)

Hi! I see that your Image is flipped from the usual RGB display pallet to the CMYK display pallet, to change this.

to solve this, I know of 1 way that will require you to install OpenCV, a library known to manipulate image data.

This is how I turn a BGR image to a RGB image:

from borb.toolkit import ImageExtraction
from pprint import pprint
from borb.pdf import PDF
import numpy as np
import cv2

imageExtraction = ImageExtraction()

with open("output.pdf","rb") as file:
    document = PDF.loads(file)

assert document is not None

pprint(document.get_page(0)["Resources"])

for key, value in document.get_page(0)["Resources"]["XObject"].items():
    image = np.array(value)
    open_cv_image = cv2.cvtColor(open_cv_image,cv2.COLOR_BGR2RGB)
    OpenCV_imageSize = open_cv_image.shape
    print(OpenCV_imageSize)
    open_cv_image = cv2.resize(src=open_cv_image, dsize=(300,200))
    cv2.imshow("key",open_cv_image)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

I will try to reproduce this issue and come back to you with a working solution in a few days time

@DrPlanecraft
Copy link

#!/usr/bin/env python3

import typing
from borb.pdf import PDF, Document

doc: typing.Optional[Document] = None
with open("test.pdf", "rb") as pdf_file_handle:
    doc = PDF.loads(pdf_file_handle)
assert doc is not None
with open("test2.pdf", "wb") as pdf_file_handle:
    PDF.dumps(pdf_file_handle, doc)

test.pdf: image

test2.pdf: image

test.pdf

I have did some investigation and Found that the reason why the file was "damaged" was because Borb read the image as an RGB instead of a CMYK image, this can only be fixed by reproducing the PDF with all images set to RGB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants