Replies: 10 comments 9 replies
-
I have no experience with CV2. You must find a way to let it write to a general Python file object - as opposed to a filename string. |
Beta Was this translation helpful? Give feedback.
-
There seems no way to do that. |
Beta Was this translation helpful? Give feedback.
-
Blame Pillow |
Beta Was this translation helpful? Give feedback.
-
is_success, im_buf_arr = cv2.imencode('.png', imgsrc, params) above code can save to IO, |
Beta Was this translation helpful? Give feedback.
-
can you help try to test convert png from io.BytesIO() into pdf? To see whether the file size becomes super large, the only solution seems to use jpeg, which is not as good as png new_image.save(fp, "JPEG") # saving the image in memory |
Beta Was this translation helpful? Give feedback.
-
No option used. see below code
|
Beta Was this translation helpful? Give feedback.
-
Any sample code to compress from io?
…------------------ 原始邮件 ------------------
发件人: "Jorj X. McKie" ***@***.***>
发送时间: 2022-12-06 21:30:57
收件人: pymupdf/PyMuPDF ***@***.***>
抄送: nissansz ***@***.***>,Author ***@***.***>
主题: Re: [pymupdf/PyMuPDF] Want to pass CV2 png in ram to pdf stream, howto do it? (Discussion #2106)
Well, give PyMuPDF a chance to compress the stuff by all means!
Reading the documentation, you will find the recommendation to do that, especially for PNG, which may contain an alpha channel.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
tried below, size is large than jpeg 95
|
Beta Was this translation helpful? Give feedback.
-
The strange thing is that
|
Beta Was this translation helpful? Give feedback.
-
I found below the problem may be due to the first step to read image from pdf. After below reading, the image became large than original png before combining pdf. After I read page from combined pdf, then each image became larger, so no matter how I compress, the image still is large from the beginning from the reading step, any method to read image in original size?
|
Beta Was this translation helpful? Give feedback.
-
Below code is fine to write to local disk.
params = [cv2.IMWRITE_PNG_COMPRESSION, 9]
cv2.imencode('.png', new, params)[1].tofile( target)
But, I want to use it directly in ram, instead of writing to local file, how to insert above compressed result into pdf ?
page.insert_image(page.rect, stream=fp.getvalue())
import fitz
import io
from PIL import Image
...
img = doc.extractImage(xref)
bio_in = io.BytesIO(img["image"]) # just to create a file-like object
pil = Image.open(bio_in) # to be used for Pillow
bio_out = io.BytesIO() # the output file-like
pil.save(bio_out, format="jpeg") # write to it in a space-saving format
imgdoc = fitz.open("jpg", bio_out.getvalue()) # open it as a PyMuPDF document
pdfbytes = imgdoc.convertToPDF() # return the PDF image of that
... and so on like in one of the previous posts
Beta Was this translation helpful? Give feedback.
All reactions