gpt4 book ai didi

python - 从静态图像创建动画 gif

转载 作者:太空宇宙 更新时间:2023-11-04 02:06:50 25 4
gpt4 key购买 nike

我有一组 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

最佳答案

你可以生成这样的东西:

enter image description here

使用以下代码(thx @Mark Setchell for numpy hint):

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)

关于python - 从静态图像创建动画 gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54535151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com