创建一个从静态图像动画GIF

问题描述 投票:2回答:1

我有一组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
python image image-processing
1个回答
2
投票

您可以生成这样的事情:

enter image description here

用下面的代码(THX @马克瑟特查为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)

或这个:

enter image description here

用下面的代码:

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)
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.