我正在使用 PIL。
im = im.rotate(angle=-90, expand = True)
当我这样做时,它会为我的图像添加一个灰色边框。
为什么?
这是完整的代码。请注意,如果我不旋转,它不会添加边框
def fixRotation(f, quality=96, image_type="JPEG"):
#http://sylvana.net/jpegcrop/exif_orientation.html
d =getEXIF(f)
if d:
orientation = int(d['Orientation'])
im = Image.open(StringIO(f))
if orientation == 6:
im = im.rotate(angle=-90, expand = True)
elif orientation == 3:
im = im.rotate(angle=-180, expand=True)
elif orientation == 8:
im = im.rotate(angle=-270, expand=True)
else:
#It doesn't add a border here.
im = im.rotate(0, expand=True)
res = StringIO()
im.save(res, image_type, quality=quality)
res.seek(0)
return res
else:
return StringIO(f)
请您参考如下方法:
我做了一些实验,确实图像大小发生了改变,但我没有理解确切的行为。在我看来,这像是 PIL 中的错误……您应该报告它。
如果你只需要 k*90 度然后做旋转你也可以使用 numpy...
img = Image.fromarray(numpy.rot90(numpy.array(img), n))
n为旋转90度的次数。
