如何使用 python pil 创建半透明图案?

How to create semi transparent pattern with python pil?

我在这方面找到了一些示例 site。我想创建示例 6。你能帮忙吗?

将餐巾纸的图像创建为 numpy 数组。正方形的大小为 10×10。您可以使用命令 numpy tile。将生成的图像保存到文件中。

标准灰度图,黑色像素为0,灰色像素为128,白色像素为255:

import numpy as np
import matplotlib.pyplot as plt

# first create one 20 x 20 tile
a1 = np.zeros((20,20), dtype=int)
a1[10:20,0:10] = a1[0:10,10:20] = 128
a1[10:20,10:20] = 255

# fill the whole 100 x 100 area with the tiles
a = np.tile(a1, (5,5))

# plot and save
plt.imshow(a, 'Greys_r')
plt.savefig('pattern.png')

你可以这样做:

from PIL import Image
import numpy as np

# Make grey 2x2 image
TwoByTwo = np.full((2,2), 128, np.uint8)

# Change top-left to black, bottom-right to white
TwoByTwo[0,0] = 0
TwoByTwo[1,1] = 255

# Tile it
tiled = np.tile(TwoByTwo, (5,5))

# Make into PIL Image, rescale in size and save
Image.fromarray(tiled).resize((100,100), Image.NEAREST).save('result.png')