Skip to content

Create pixmap directly from PIL image? #801

Discussion options

You must be logged in to vote

Several comments:

  1. You do not need a pixmap for PDF image insertion: a file or a binary string of the image file or a io.BytesIO are accepted as well.
  2. Similar is true for pixmaps themselves: among the many constructors use pix = fitz.Pixmap(stuff). If "stuff" is a string, it is interpreted as a filename. If it is a bytes, bytearray or io.BytesIO object, the constructor assumes an image.

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 PI…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by vs-777
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants