你可以用以下代码生成类似这样的东西:
![enter image description here](https://istack.dev59.com/BzSx2.gif)
以下是代码(感谢 @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)
或者这样:
![在此输入图片描述](https://istack.dev59.com/Bqrq5.gif)
使用以下代码:
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)