Skip to content
Discussion options

You must be logged in to vote

For example, insert some Pillow image rotated left by 45°:

from PIL import Image
import fitz
import io
img=Image.open("beauty-contest.jpg")
img.size
(607, 741)
src = fitz.open()  # the 1-page PDF with the image
# give page width / height from image
spage = src.new_page(width=img.size[0], height=img.size[1])
fp = io.BytesIO()
img.save(fp, "JPEG")  # make image memory
# and insert in PDF as fullpage image
spage.insert_image(spage.rect, stream=fp.getvalue())
5
doc = fitz.open()  # your target PDF
page = doc.new_page()
rect = fitz.Rect(100,100,300,300)  # image should land inside this
page.show_pdf_page(rect, src, 0, rotate=45)
7
doc.ez_save("test.pdf")

Result looks like this:

Replies: 6 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by RamKromberg
Comment options

You must be logged in to vote
2 replies
@JorjMcKie
Comment options

@RamKromberg
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@JorjMcKie
Comment options

Comment options

You must be logged in to vote
2 replies
@JorjMcKie
Comment options

@RamKromberg
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants