I know this can be done with matplotlib or PIL, but I need it to be done in blender, without installing any other package.

I searched and tried following code in blender, but only get a black image as output.

import bpy
import numpy as np

image_path = "src.jpg"
output_image_path = "output.jpg"
output_image_name = "output.jpg"

image = bpy.data.images.load(image_path)
np_array = np.asarray(image.pixels)

# create output image
output_image = bpy.data.images.new(output_image_name, width=image.size[0], height=image.size[1])
output_image.file_format = 'JPEG'
output_image.filepath = output_image_path

# convert np array back to image
output_image.pixels = np_array.ravel()


So, how to make this work? Thanks.

  • $\begingroup$ Hi! I tried the same above and it worked without any problems... Maybe check your paths and filenames carefully, make sure you are looking at the right output file ? $\endgroup$
    – ydvisual
    Feb 19 at 4:28

1 Answer 1


Well, After I re-open my blender and change source image to another path, everything works. No change on the code.

So I leave this question here for anyone who need this code.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.