As an intermediate step for further evolution I am trying to edit the pixels of an image via Numpy, like this:
for img in bpy.data.images:
print(img.name, img.size[1], img.size[0], img.channels, img.type, img.colorspace_settings)
img_arr = (np.array(img.pixels[:]) * 64).reshape((img.size[1], img.size[0], img.channels))
print('begin write to pixels')
# tried different things here
img.pixels = img_arr.flatten()
img.update()
print('image updated')
if img_arr.shape[0] and img_arr.shape[1]:
# output image via OpenCV
img_arr = cv2.cvtColor(np.float32(img_arr), cv2.COLOR_RGB2BGR)
cv2.imwrite('out_cv2_' + str(i) + '.png', img_arr)
# direct save of image
img.filepath = 'out_direct_' + str(i) + '.png'
img.file_format = 'PNG'
img.save()
i = i + 1
My expectation is that the image in Blender would be darkened, and more transparent. However that is not happening. The images I save via OpenCV for comparison are darker and semi-transparent.
I've tried different ways of writing to the pixels
- divide the numpy array by 255
- transofrm the numpy array into a tuple with
np.asarray
What am I missing?