将静态图像制作成动态GIF

3

我有一组RGB值,需要将它们放在单独的像素中。我用PIL实现了这个功能,但我需要逐个绘制像素并查看进度,而不是得到最终图像。

from PIL import Image
im = Image.open('suresh-pokharel.jpg')
pixels = im.load()
width, height = im.size

for i in range(width):
    for j in range(height):
        print(pixels[i,j])  # I want to put this pixels in a blank image and see the progress in image

好的。请将前4-5个像素添加到您的代码中。您是想制作动画吗?请问这样做的目的是什么? - Mark Setchell
你说你想把像素放在一个空白画布上,那为什么要打开JPEG文件? - Mark Setchell
@MarkSetchell 是的,动画。 - psuresh
我需要编写一个程序,读取现有图像的所有像素值,并绘制每个像素以进行可视化。 - psuresh
你知道像素将从左上角到右下角逐个绘制吗?因此,物体不会开始出现并逐渐填充和变得可见。它们只会以自上而下的行扫描方式出现... - Mark Setchell
1个回答

2
你可以用以下代码生成类似这样的东西:

enter image description here

以下是代码(感谢 @Mark Setchell 提供的 numpy 提示):
最初的回答
import imageio
import numpy as np
from PIL import Image

img = Image.open('suresh-pokharel.jpg')
pixels = img.load()
width, height = img.size
img2 = Image.new('RGB', img.size, color='white')
pixels2 = img2.load()

i = 0
images = []
for y in range(height):
    for x in range(width):
        pixels2[x, y] = pixels[x, y]
        if i % 500 == 0:
            images.append(np.array(img2))
        i += 1

imageio.mimsave('result.gif', images)

或者这样:

在此输入图片描述

使用以下代码:


Translated:
import random
import imageio
import numpy as np
from PIL import Image

img = Image.open('suresh-pokharel.jpg')
pixels = img.load()
width, height = img.size
img2 = Image.new('RGB', img.size, color='white')
pixels2 = img2.load()

coord = []
for x in range(width):
    for y in range(height):
        coord.append((x, y))

images = []
while coord:
    x, y = random.choice(coord)
    pixels2[x, y] = pixels[x, y]
    coord.remove((x, y))
    if len(coord) % 500 == 0:
        images.append(np.array(img2))

imageio.mimsave('result.gif', images)

不错啊!但是为什么要将它们写入磁盘,然后再读取它们,接着再删除呢? - Mark Setchell
不知道如何直接从PIL将图像发送到imageio。 - Alderven
你可以使用numpyarray= np.array(PILimage)将PIL图像转换为Numpy数组。 - Mark Setchell
不知道这个!谢谢!我已经更新了我的答案。 - Alderven

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接