I'm trying to open an image stored internally in Blender into the Image module from Pillow (https://pillow.readthedocs.io/en/3.1.x/reference/Image.html). This should be a simple operation but I'm having some problems with it. I don't want to use the path of the image, but I want to work with the pixels.
from bpy import data
from PIL import Image
import numpy as np
# Generate a random image
width = 80
height = 40
img = data.images.new('random_colors', width=width, height=height)
num_pixels = len(img.pixels)
from random import random as rnd
#Create random colors for each pixel
for px in range(0, num_pixels-5, 12):
r = rnd()
b = rnd()
g = rnd()
a = 1
cols = (r, g, b, a)
for i in range(4):
img.pixels[px+i] = cols[i]
pxs = bytearray(int(p*255) for p in img.pixels)
image = Image.fromarray(np.array(pxs), 'RGBA')
I'm getting : IndexError: tuple index out of range
Any hint?