what I need:
– there are two images: a background (large) and a proifile pic (smaller)
– on background there is an oblique box, where I want to merge the profile pic
Code:
# open the profile pic
im = PIL.Image.open(pic)
# resize it to dim of oblique box
im = im.resize((picX, picY))
# this is the degree of oblique box
degree = 13.67
# open the background
bg = PIL.Image.open(bgsrc)
bgosize = bg.size
bginfo = bg.info
# first, I rotate the background to be paralell with profile pic
bg = bg.rotate(-degree, resample = PIL.Image.BILINEAR, expand = True)
# paste the profile pic to background
bg.paste(im, (px1, py1, px2, py2))
# rotate back to original orientation
bg = bg.rotate(degree, resample = PIL.Image.BILINEAR, expand = False)
# crop the rotated image, because it's greater than original size,
# after first rotate - coords are stored
bg.crop(bgx1, bgy1, bgx2, bgy2)
PIL.ImageFile.MAXBLOCK = bg.size[0] * bg.size[1]
bg.save(dst, quality = 250, optimize = True, **bginfo)
After this transform the result image is nubbly a litlebit…
How can I get a good qualtiy image?
Thanks:
a.
Hint 1: Use
Image.BICUBICinstead ofImage.BILINEAR. Unfortunatelyrotatedoes not acceptImage.ANTIALIASwhich would provide an even better result.Hint 2: Instead of rotating the background and rotating it back later, rotate the image you want to paste.
Hint 3: Create an image in
'L'format that is pure white and the same size as the image you’re pasting. Rotate it the same way. Use it as a mask argument topaste.I haven’t tested this code but it should work. One question though, what is
quality = 250supposed to accomplish? The JPEG options for example only accept values from 1 to 95.