我需要什么: - 有两个图像:背景(大)和proifile pic(较小) - 在背景上有一个斜框,我想合并个人资料图片
代码:
# 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)
在此变换之后,结果图像可能是一个小小的......
如何获得良好的质量图像?
感谢:
一个。
答案 0 :(得分:3)
提示1:使用Image.BICUBIC
代替Image.BILINEAR
。很遗憾,rotate
不接受会提供更好结果的Image.ANTIALIAS
。
提示2:旋转想要粘贴的图像,而不是旋转背景并稍后旋转它。
提示3:以'L'
格式创建纯白色且与您粘贴的图像大小相同的图像。以同样的方式旋转它。将它用作paste
的掩码参数。
quality = 250
应该完成什么?例如,JPEG options仅接受1到95之间的值。
# open the profile pic
im = PIL.Image.open(pic)
# resize it to dim of oblique box
im = im.resize((picX, picY), PIL.Image.ANTIALIAS)
# this is the degree of oblique box
degree = 13.67
# open the background
bg = PIL.Image.open(bgsrc)
bgosize = bg.size
bginfo = bg.info
# create a copy of the profile that is all white
mask = PIL.Image.new('L', im.size, 0xff)
# rotate the profile and the mask
im = im.rotate(degree, resample = PIL.Image.BICUBIC, expand = True)
mask = mask.rotate(degree, resample = PIL.Image.BICUBIC, expand = True)
# paste the profile pic to background
bg.paste(im, (px1, py1, px2, py2), mask)
PIL.ImageFile.MAXBLOCK = bg.size[0] * bg.size[1]
bg.save(dst, quality = 250, optimize = True, **bginfo)
答案 1 :(得分:1)
尝试双三次插值?如果这没有帮助,请尝试将图像放大(例如,2倍甚至4倍大小),旋转,然后缩小到原始尺寸。