from PIL import Image
import numpy as np
# Test: set 2 pixels in a 4x3 image.
N = 2
nx, ny = 4, 3
# Specify the colours of pixels at these (x,y) coordinates
pos = np.array([[0,0], [1,1]])
col = np.array([[0,255,255], [255,0,0]])
# Set the alpha channel of the coloured pixels to 255: opaque
cola = np.ones((N,4)) * 255
cola[:,:3] = col
# The image RGBA array: initially set all pixels to transparent (alpha=0)
img = np.zeros((nx, ny, 4), dtype=np.uint8)
# Set the pixels' colours.
img[pos[:,0],pos[:,1]] = cola
# Turn the array into a PIL Image and save it.
im = Image.fromarray(img)
im.save("image.png")
1条答案
按热度按时间cczfrluj1#
你不会说最终图像的大小是什么,但是如果它的尺寸是
(nx, ny)
,你可以用NumPy和PIL来做到这一点: