Create pixmap directly from PIL image? #801
-
This is my problem: in one function, I generate an image using PIL. In another function, I get this image and create a pixmap so that I can insert it on a pdf. Currently, I have to save the PIL image into a folder, and load it when calling the Pixmap class. Is there anyway to avoid the saving process and directly load the PIL image data into a pymupdf pixmap? e.g.:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Several comments:
So if you have an PIL Image, do this to insert in a PDF page: import io
from PIL import Image
bio = io.BytesIO()
img.save(bio, "JPEG", ...)
# option 1: use io.BytesIO
page.insertImage(rect, stream=bio)
# option 2: use pixmap
pix = fitz.Pixmap(bio)
page.insertImage(rect, pixmap=pix) The point is that PIL Images must be saved somehow first. This can happen to memory using io.BytesIO. |
Beta Was this translation helpful? Give feedback.
-
Perfect! Didn't know about |
Beta Was this translation helpful? Give feedback.
Several comments:
io.BytesIO
are accepted as well.pix = fitz.Pixmap(stuff)
. If "stuff" is a string, it is interpreted as a filename. If it is abytes
,bytearray
orio.BytesIO
object, the constructor assumes an image.So if you have an PIL Image, do this to insert in a PDF page:
The point is that PI…