【发布时间】:2020-02-28 12:28:31
【问题描述】:
我有图像和蓝色像素列表。我想遍历蓝色像素,将它们更改为红色并从中制作 gif。所以它必须是一条线,因此颜色从蓝色变为红色,但是出了点问题
im = Image.open(r"test2.png")
pixels = im.load()
images = []
blues = get_sorted_blues() # See func below
for x, y in blues:
...: pixels[x, y] = (255, 0, 0)
...: images.append(im)
images[0].save('result.gif',
...: save_all=True,
...: append_images=images[1:],
...: duration=100,
...: loop=0)
def get_sorted_blues():
...: blues = []
...: for x in range(im.width):
...: for y in range(im.height):
...: if pixels[x, y] == (0, 0, 255):
...: blues.append([x, y])
...: return sorted(blues)
result.gif 它只是一条红线,没有任何动画
【问题讨论】:
-
请分享您的起始图片,以便我们更好地为您提供帮助。此外,您应该避免使用 Python 图像处理的
for循环——它们是 sl-o-o-o-ow。使用诸如 OpenCV 或 Numpy 之类的矢量化工具进行处理。 -
所以问题是你没有让它动画?不熟悉枕头,但在你的循环中,你不断更新同一个图像并将同一个图像放入列表中,这意味着最后,列表中的所有条目都指向同一个图像,它都是红色的。在for循环中,代替
images.append(im),改为images.append(im.copy()) -
是的,这是一个答案。需要使用 im.copy()。我觉得很愚蠢,一个愚蠢的错误。
-
@ДавидШико 很高兴知道。我将我的评论作为答案,以便您接受。
标签: python python-imaging-library gif