# ref: https://blender.stackexchange.com/questions/270924/how-to-open-blenders-image-using-pillow # ref: https://stackoverflow.com/questions/21233946/pil-image-save-function-fails-in-blender-python import bpy import os import subprocess import sys import io import struct import numpy as np try: from PIL import Image except: python_exe = os.path.join(sys.prefix, 'bin', 'python.exe') subprocess.call([python_exe, '-m', 'ensurepip']) subprocess.call([python_exe, '-m', 'pip', 'install', '--upgrade', 'pip']) subprocess.call([python_exe, '-m', 'pip', 'install', '--upgrade', 'pillow']) def pil_to_image(pil_image, name='NewImage'): width, height = pil_image.width, pil_image.height normalized = 1.0 / 255.0 bpy_image = bpy.data.images.new(name, width=width, height=height) bpy_image.pixels[:] = (np.asarray(pil_image.convert('RGBA'),dtype=np.float32) * normalized).ravel() return bpy_image def image_to_pil(bpy_image): img = bpy_image pixels = [int(px * 255) for px in bpy_image.pixels[:]] bytes = struct.pack("%sB" % len(pixels), *pixels) pil_image = Image.frombytes('RGBA', (bpy_image.size[0], bpy_image.size[1]), bytes) return pil_image im = image_to_pil(bpy.data.images["test"]) im = im.transpose(Image.FLIP_LEFT_RIGHT).rotate(45) pil_to_image(im)