TypeError: int() argument must be a string, a bytes-like object or a number, not 'Image'

created at 07-15-2021 views: 24

description

In process of PIL.Image convert to numpy array

When using PIL.Image to read the image, if you use numpy.array() directly to convert, an error will occur:

lst = list()
for file_name in os.listdir(dir_image):
    image = PIL.Image.open(file_name)
    lst.append(image)
arr = numpy.array(lst)

At this point, an error will occur in the execution of the last line above:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Image'

solution

The solution is as follows:

lst = list()
for file_name in os.listdir(dir_image):
    image = PIL.Image.open(file_name)
    lst.append(np.array(image))
arr = numpy.array(lst)

That is, the elements in the list have been converted to numpy.array instead of direct Image objects.

created at:07-15-2021
edited at: 07-15-2021: