Search code examples
pythonpython-imaging-librarygoogle-colaboratoryattributeerror

why'_io.BufferedRandom' object has no attribute 'resize'


I'm using PIL, and I've got this error message:

AttributeError: '_io.BufferedRandom' object has no attribute 'resize'

My code:

def phash(img):
    img = img.resize((8, 8), Image.ANTIALIAS).convert('L')
    avg = reduce(lambda x, y: x + y, img.getdata()) / 64.
    return reduce(
        lambda x, y, z: x | (z << y),
        enumerate(map(lambda i: 0 if i < avg else 1, img.getdata())),
        0
    )


Solution

  • The code you shared only includes the definition of the function, and I think that the problem is in your main code.
    The error message that you got shows you that the object that you are trying to resize doesn't have this functionality, and because of that I think that you have accidentally loaded the image with the command open('path/to/image.png'), when you need to load it as an 'Image' object with the command Image.open('path/to/image.png').

    Try writing something like this:

    from PIL import Image
    from functools import reduce
    
    
    def phash(img):
        pass # your function here
    
    path = 'path/to/image.png'
    image = Image.open(path)
    phash = phash(image)