我正在使用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)发布于 2011-01-24 07:57:31
我做了一些实验,确实改变了图像的大小,但我不理解确切的行为。在我看来就像是PIL中的一个bug。你应该上报。
如果你只需要k*90度,那么你也可以使用numpy来做旋转。
img = Image.fromarray(numpy.rot90(numpy.array(img), n))n是旋转90度的次数。
发布于 2011-02-17 17:14:33
注意以下内容(CPython 2.6,PIL1.1.7):
import Image
i= Image.open("someimage.jpg")
ir0= i.rotate(-90)
ir1= i.rotate(-90, expand=1)
for img in i, ir0, ir1:
print(img.size)
# output follows
(720, 400)
(400, 720)
(401, 721)angle不是90的倍数时,expand is 因此,如果您只关心90°-180°-270°旋转,只需省略expand=1参数;更好的方法是使用transpose方法(请参阅PIL tutorial中的几何变换)
发布于 2011-06-19 16:12:38
当旋转时,算法(本质上)用“下一个”像素值对每个像素进行平均。对于图像“内部”的所有像素,定义下一个像素。对于图像边缘的所有像素,该像素都是未定义的。
因此,灰色是已知周长像素和未定义外部像素之间的平均值。
https://stackoverflow.com/questions/4777263
复制相似问题