numpy 公开用于通过多维arrays 处理和操作图像,因为它们对于存储值非常有用 > 作为像素(rgb、rgba、greyscale 等...)
RGB:
>>> import numpy as np
>>> from PIL import Image
>>> array = np.zeros([100, 200, 3], dtype=np.uint8)
>>> array[:,:100] = [255, 128, 0] #Orange left side
>>> array[:,100:] = [0, 0, 255] #Blue right side
>>> img = Image.fromarray(array)
>>> array[:,:100] = [100, 128, 0]
>>> array[:,100:] = [0, 0, 200]
>>> img = Image.fromarray(array)
>>> img.save('img.png')
灰度:
>>> import numpy as np
>>> from PIL import Image
>>> array = np.zeros([100, 200], dtype=np.uint8)
>>> # Set grey value to black or white depending on x position
>>> for x in range(200):
>>> for y in range(100):
>>> if (x % 16) // 8 == (y % 16) // 8:
>>> array[y, x] = 0
>>> else:
>>> array[y, x] = 255
>>>
>>> img = Image.fromarray(array)
>>> img.save('img.png')