9951 explained code solutions for 126 technologies


python-numpySave Numpy array to image


import numpy as np
from PIL import Image

image2 = Image.fromarray(data)
image2.save('/tmp/image.png')ctrl + c
import numpy as np

load Numpy module for Python

from PIL import Image

load Pillow module to work with images

Image.fromarray

load image data from given Numpy array

data

Numpy array to save to image ( (e.g. loaded from another image))

.save(

save current image object into specified file


Usage example

import numpy as np
from PIL import Image

image = Image.open('/var/www/examples/heroine.png')
data = np.asarray(image)

image2 = Image.fromarray(data)
image2.save('/tmp/image.png')

print(type(image2))
output
<class 'PIL.Image.Image'>