Home>

I want to read sequential images, convert them to ndarray format, and save them in a text file.

from PIL import Image
import numpy as np
import glob
file = glob.glob ('./ Desktop/Cat/*. jpg')
for f in file:
  image = np.array (Image.open (file) .convert ('RGB'))
np.savetxt ('./ Desktop/image.txt', image, fmt = '%. 1e')
Traceback (most recent call last):
  File "/home/hoge/.local/lib/python3.6/site-packages/PIL/Image.py", line 2613, in open
    fp.seek (0)
AttributeError: 'list' object has no attribute 'seek'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "./Desktop/image_data.py", line 7, in<module>
    image = np.array (Image.open (files) .convert ('RGB'))
  File "/home/yudai/.local/lib/python3.6/site-packages/PIL/Image.py", line 2615, in open
    fp = io.BytesIO (fp.read ())
AttributeError: 'list' object has no attribute 'read'


There was the following related question.
How to store multiple image data in Python

Environment

python 3.6.7
numpy 1.15.4
ubuntu 18.04

  • Answer # 1


    instead of

    image = np.array (Image.open (file). convert ('RGB'))) image = np.array (Image.open (f). convert ('RGB'))).

    Furthermore,imageis overwritten in the loop.
    To save multiple files as one text file, it is necessary to save the file after adding it to another array by the related question method.

  • Answer # 2

    Each error presented is an error on a variable named fp, but the code presented does not have fp

    What is the definition of fp?