# How to Convert Image to numpy array, then convert it back to image by script

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"

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()

output_image.save()


So, how to make this work? Thanks.

• 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 ? Feb 19 at 4:28