hyunsu jeong
hyunsu jeong

Reputation: 51

How to convert ndarray into IMAGE type

load_image function doesn't accept ndarray Type. The function only accept IMAGE type.

Is it possible to convert ndarray type into IMAGE type or _ImageCrop type into IMAGE type?

enter image description here

Upvotes: 0

Views: 200

Answers (2)

Dmitry Mottl
Dmitry Mottl

Reputation: 862

import numpy as np
from PIL import Image

# monochrome:
X1 = np.random.randint(0, 255, size=(256,256), dtype=np.uint8)
image1 = Image.fromarray(X1, "L")
image1.show()

# color:
X2 = np.random.randint(0, 255, size=(256,256,3), dtype=np.uint8)
image2 = Image.fromarray(X2, "RGB")
image2.show()

Check out different modes:
https://pillow.readthedocs.io/en/5.2.x/handbook/concepts.html#concept-modes

Upvotes: 0

Krishna Choudhary
Krishna Choudhary

Reputation: 625

As per my understanding of the problem, you want to convert numpy array into image format (PIL). This can be done by following code:

import numpy
import PIL

#Convert nparray to PIL image
img = PIL.Image.fromarray(arr)    #arr is numpy array

Upvotes: 1

Related Questions