我的64*64
数组由0
和1
组成。如何将此数组转换为图像并保存?
from PIL import Image
img = Image.fromarray(data , 'RGB') //
img.save('my.png')
img.show()
'删除RGB并没有给我预期的结果所有1的数组也给了我黑'
上面的代码给了我错误,我无法转换它,如果我想降低分辨率图像如何转换它,即16*16
答案 0 :(得分:3)
from PIL import Image
import random
data = [random.randint(0, 1) for i in range(64 * 64)]
img = Image.new('1', (64, 64))
img.putdata(data)
img.save('my.png')
img.show()
答案 1 :(得分:1)
from PIL import Image
import numpy as np
from random import randint
# create random array
def create_arr(width, height):
bin_array = np.zeros((width, height), 'uint8')
for x in xrange(0, width):
for y in xrange(0, height):
bin_array[x, y] = randint(0, 1)
return bin_array
# write array to img
def create_img(width, height, bin_array):
rgb_array = np.zeros((width, height, 3), 'uint8')
for x in xrange(0, width):
for y in xrange(0, height):
rgb_array[x, y, 0] = bin_array[x, y] * 255 #R
rgb_array[x, y, 1] = bin_array[x, y] * 255 #G
rgb_array[x, y, 2] = bin_array[x, y] * 255 #B
img = Image.fromarray(rgb_array)
img.save('img.jpeg')
# create array
bin_array = create_arr(64, 64)
# write bin_array to img
create_img(64, 64, bin_array)