【发布时间】:2019-05-03 12:44:48
【问题描述】:
我正在使用某些像素的 Alpha 值来跟踪它们在旋转过程中的位置。我注意到在某些情况下我会丢失设置的 Alpha 值。显示问题的示例代码:
def showAlphas(rotation, img):
imgPixels = img.load()
foundAlpha = False
for w in range(0, img.width):
for h in range(0, img.height):
if imgPixels[w, h][3]==50:
print 'Found at {}, {} in Rotation {}'.format(w, h, rotation)
foundAlpha = True
if not foundAlpha:
print 'Alpha missing from rotation {}'.format(rotation)
img = Image.new('RGBA', (100, 50), color='black')
img.putpixel((10, 10), (255, 0, 0, 50))
for x in range(1, 360, 1):
rotated = img.rotate(x, expand=True)
showAlphas(x, rotated)
样本输出:
Found at 96, 41 in Rotation 194
Found at 96, 41 in Rotation 195
Alpha missing from rotation 196
Found at 98, 41 in Rotation 197
Found at 97, 41 in Rotation 198
Found at 98, 41 in Rotation 198
Alpha missing from rotation 199
Alpha missing from rotation 200
Found at 98, 41 in Rotation 201
Found at 97, 40 in Rotation 202
Found at 98, 40 in Rotation 202
使用 Pillow 5.3.0
我在轮换中做错了吗?据我所知,Expand=True 是我需要指定的唯一选项(以确保图像不会在某些方向被剪裁)?
我不喜欢使用 Pillow,但需要一些东西来跟踪各种调整大小和旋转操作中的特定像素坐标,并认为使用 Pillow 和跟踪 Alpha 值可以做到这一点。我真的不想尝试通过 RGB 值进行跟踪,但担心这可能会遇到同样的问题。还有其他建议吗?
【问题讨论】:
-
不保证源图像中的每个像素在旋转时都会出现在结果中。基本上,您将一个栅格(网格)旋转到另一个栅格(网格)之上,并且在输出栅格中的每个位置,您可以通过线性或三次插值或通过源上像素的最近邻来计算新点。
PIL.Image.NEAREST重采样可能运气更好。
标签: python python-imaging-library alpha