Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to paste a rotate image without cropping sides

Tags:

python

image

this is the original back image this is the original front image this is my code:

from PIL import Image
front=Image.open('/users/apple/Desktop/TMP.jpeg')
back=Image.open('/Users/apple/Desktop/原始图片/084.jpeg')
front=front.rotate(100,expand=True)
back.paste(front,(100,100))
back

this is what i get. i want a clean paste without cropping sides. can somebody help me ,thanks.

like image 547
dawen Avatar asked Nov 05 '25 22:11

dawen


1 Answers

You need to generate a mask that you can pass to paste to exclude the new black edges.

mask = Image.new('L', front.size, 255)
front = front.rotate(100, expand=True)
mask = mask.rotate(100, expand=True)
back.paste(front, (100,100), mask)

Result

like image 115
Mark Ransom Avatar answered Nov 07 '25 15:11

Mark Ransom