1

I have a text file that contains the paths to jpeg images I want to import into my script. I am using an example code provided by a Udemy course: "Deep Learning with Python - Novice to Pro!" that detects smiles in images. The function I am having trouble with is converting images into matrixes/two-dimensional arrays:

def img2array(f, detection=False, ii_size=(64, 64)):
"""
Convert images into matrixes/two-dimensional arrays.

detection - if True we will resize an image to fit the
            shape of a data that our first convolutional
            layer is accepting which is 32x32 array,
            used only on detection.

ii_size - this is the size that our input images have.
"""
rf=None
if detection:
    rf=f.rsplit('.')
    rf=rf[0]+'-resampled.'+rf[1]
    im = Image.open(f)
    # Create a smaller scalled down thumbnail
    # of our image.
    im.thumbnail(ii_size)
    # Our thumbnail might not be of a perfect
    # dimensions, so we need to create a new
    # image and paste the thumbnail in.
    newi = Image.new('L', ii_size)
    newi.paste(im, (0,0))
    newi.save(rf, "JPEG")
    f=rf
# Turn images into an array.
data=imread(f, as_gray=True)
# Downsample it from 64x64 to 32x32
# (that's what we need to feed into our first convolutional layer).
data=block_reduce(data, block_size=(2, 2), func=np.mean)
if rf:
    remove(rf)
return data

The function is called in another script:

    img_data=prep_array(img2array(filename, detection=True), detection=True)

I am not sure what to name 'filename' in order for this code to run correctly. When I give it the text file path I get an error that says:

UnidentifiedImageError: cannot identify image file 'filepath\imagelist.txt

I am brand new to Python, and I need help importing the correct 'filename' variable to make this function work.

1 Answers1

1

From the looks of the error message, you're passing in the file path of the textfile (containing paths to images) as filename

Parse the text file for file paths to images and pass it into your function.

with open("path/to/imagelist.txt", "r") as fp:
    filepaths = fp.read().splitlines()
    for filename in filepaths:
        img_data=prep_array(img2array(filename, detection=True), detection=True)

snnguyen
  • 334
  • 1
  • 12
  • This gives me an error of file not found because it added a "\n" at the end of the jpeg filename automatically. How do I fix this? Thank you! – Alyson Light Jan 31 '20 at 20:01
  • @AlysonLight: Insert a `filename = filename.rstrip()` – stovfl Jan 31 '20 at 20:03
  • You can strip the newline by hand or use `splitlines()` https://stackoverflow.com/a/12330535/2662958 – snnguyen Jan 31 '20 at 20:07
  • the rstrip did not solve the "\n" issue. I am getting the same error (no such file) – Alyson Light Jan 31 '20 at 20:11
  • @AlysonLight ***no such file**: Verify if the printed path is correct. Do you have a absoute or a relative path in your `.txt`? – stovfl Jan 31 '20 at 20:12
  • It has now read the files correctly and separated the jpeg files into a list variable, however, in the function above, once it reaches im = Image.open(f), I get an error: AttributeError: 'list' object has no attribute 'read' – Alyson Light Jan 31 '20 at 20:36
  • @AlysonLight Are you passing in the filepath as `f`? – snnguyen Jan 31 '20 at 20:45
  • Yes, that was my issue, had to pass filename. Thank you all so much for your patience! – Alyson Light Feb 01 '20 at 13:45