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

Closed
opened 2021-08-02 18:00:41 +00:00 by mara0004 · 8 comments
Contributor

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?
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.
Author
Contributor

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?
Author
Contributor

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.
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.
Author
Contributor

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?~~
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.
Author
Contributor

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

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

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
No description provided.