Possibility to provide PIL.Image.Image objects as input? #115

Closed
opened 3 years ago by mara0004 · 8 comments

I sometimes have to pre-process the images I convert to PDF with img2pdf. Currently, I save the PIL.Image.Image object into an io.BytesIO stream and then pass it to img2pdf. However, I guess this adds an avoidable layer of save/open, since img2pdf also uses Pillow internally. Would it be possible for img2pdf to directly accept PIL images as input?

I sometimes have to pre-process the images I convert to PDF with img2pdf. Currently, I save the `PIL.Image.Image` object into an `io.BytesIO` stream and then pass it to img2pdf. However, I guess this adds an avoidable layer of save/open, since img2pdf also uses Pillow internally. Would it be possible for img2pdf to directly accept PIL images as input?
josch commented 3 years ago
Owner

I don't see how this can be possible. Not all input images are read into a PIL object. JPEG images, for example, are copied into the PDF as they are and you cannot do that if you just give the PIL object to img2pdf.

I don't see how this can be possible. Not all input images are read into a PIL object. JPEG images, for example, are copied into the PDF as they are and you cannot do that if you just give the PIL object to img2pdf.
Poster

I see. Maybe img2pdf could detect the format and convert to a byte stream only if necessary, like in the case of JPEG?

I see. Maybe img2pdf could detect the format and convert to a byte stream only if necessary, like in the case of JPEG?
Poster

If you think this does not fit the design of img2pdf, feel free to close the issue.

If you think this does not fit the design of img2pdf, feel free to close the issue.
josch commented 3 years ago
Owner

I see. Maybe img2pdf could detect the format and convert to a byte stream only if necessary, like in the case of JPEG?

But that would be a lossy operation.

> I see. Maybe img2pdf could detect the format and convert to a byte stream only if necessary, like in the case of JPEG? But that would be a lossy operation.
Poster

But that would be a lossy operation.

Why? Consider I have a PIL image that was created from scratch, for instance. Where is the quality loss?
And if I open a JPEG file with PIL and saved it to JPEG again, it should be the exact same data, right?

> But that would be a lossy operation. ~~Why? Consider I have a PIL image that was created from scratch, for instance. Where is the quality loss?~~ ~~And if I open a JPEG file with PIL and saved it to JPEG again, it should be the exact same data, right?~~
josch commented 3 years ago
Owner

But that would be a lossy operation.

Why?
Consider I have a PIL image that was created from scratch, for instance. Where is the quality loss?

Because you were talking about a JPEG. If you open a JPEG with PIL and then hand that JPEG to img2pdf, then you cannot retrieve the original file just from the PIL object and storing that PIL object as JPEG is lossy.

> > But that would be a lossy operation. > > Why? > Consider I have a PIL image that was created from scratch, for instance. Where is the quality loss? Because you were talking about a JPEG. If you open a JPEG with PIL and then hand that JPEG to img2pdf, then you cannot retrieve the original file just from the PIL object and storing that PIL object as JPEG is lossy.
Poster

Ookay, surprising this is lossy. I'll test it.

Ookay, surprising this is lossy. I'll test it.
mara0004 closed this issue 3 years ago
Poster

Indeed. It is. I would have expected it to be lossless, if no modifications have taken place.

Indeed. It is. I would have expected it to be lossless, if no modifications have taken place.
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: josch/img2pdf#115
Loading…
There is no content yet.