Из вашего описания не ясно, является ли camera1[2]
простым списком последовательных значений R, G, B, A или списком RGBA-кортежей.Итак, я покажу вам, как читать оба варианта.;)
Ваша главная проблема в том, что ваши данные не содержат информации о ширине и высоте, поэтому нам нужно как-то предоставить эту информацию.Один из способов сделать это - прочитать данные в массив Numpy 3D правильной формы.Но мы также можем сделать это напрямую в PIL, используя соответствующие методы Image.
Для моих демонстраций я использую циклы Python для создания простых данных RGBA.
Этот скрипт создает список кортежей RGBA.
from PIL import Image
maxval = 255
width, height = 400, 300
# Display size info
size = width * height
fmt = 'Width: {}, Height: {}, Pixels: {}, Bytes: {}'
print(fmt.format(width, height, size, size * 4))
# Make a 2D gradient that starts at black in the top left corner,
# with red & green increasing horizontally, blue increasing vertically.
# This would be much faster using Numpy instead of Python loops.
pixels = []
# Make all pixels fully opaque
alpha = maxval
for y in range(height):
blu = maxval * y // height
for x in range(width):
red = gre = maxval * x // width
# Make a single RGBA pixel as a tuple
pix = red, gre, blu, alpha
# And save it
pixels.append(pix)
# Show that the size of `pixels` is correct and show the first few pixels
print('Size:', len(pixels))
print(pixels[:8])
# Make a new image object. All pixels are set to black.
img = Image.new('RGBA', (width, height))
# Copy the pixel data to the Image
img.putdata(pixels)
img.show()
img.save('test1.png')
выход
Width: 400, Height: 300, Pixels: 120000, Bytes: 480000
Size: 120000
[(0, 0, 0, 255), (0, 0, 0, 255), (1, 1, 0, 255), (1, 1, 0, 255), (2, 2, 0, 255), (3, 3, 0, 255), (3, 3, 0, 255), (4, 4, 0, 255)]
test1.png
Этот скрипт создает плоский список значений R, G, B, A.Он использует объект Python 3 bytes
, поэтому он не будет работать должным образом на Python 2.
from PIL import Image
maxval = 255
width, height = 400, 300
# Display size info
size = width * height
fmt = 'Width: {}, Height: {}, Pixels: {}, Bytes: {}'
print(fmt.format(width, height, size, size * 4))
# Make a 2D gradient that starts at black in the top left corner,
# with red & green increasing horizontally, blue increasing vertically.
# This would be much faster using Numpy instead of Python loops.
rgba = []
# Make all pixels fully opaque
alpha = maxval
for y in range(height):
blu = maxval * y // height
for x in range(width):
red = gre = maxval * x // width
# Make a single RGBA pixel as a tuple
pix = red, gre, blu, alpha
# And save each of red, gre, blu, alpha to rgba.
# By using `.extend` we create a flat list
rgba.extend(pix)
# Show that the size of `rgba` is correct and show the first few values.
print('Size:', len(rgba))
print(rgba[:32])
# Convert the rgba list to bytes.
rgba = bytes(rgba)
# Make a new image object from the bytes
img = Image.frombytes('RGBA', (width, height), rgba)
img.show()
img.save('test2.png')
output
Width: 400, Height: 300, Pixels: 120000, Bytes: 480000
Size: 480000
[0, 0, 0, 255, 0, 0, 0, 255, 1, 1, 0, 255, 1, 1, 0, 255, 2, 2, 0, 255, 3, 3, 0, 255, 3, 3, 0, 255, 4, 4, 0, 255]
file 'test2.png 'идентичен' test1.png '.