Search code examples
pythonpython-imaging-library

'_io.BufferedRandom' object has no attribute 'getvalue'


I am using PIL to check some attributes from an image uploaded to my Django backend. However I am getting the following error:

<class 'AttributeError'> 347
'_io.BufferedRandom' object has no attribute 'getvalue'

With the following code:

try:
  if 'image' in request.FILES['image'].content_type:
    image = request.FILES['image']

    image_name = profile["user"] + "." + image.name.split(".")[1]

    image_value = image.file.getvalue()

    with Image.open(image) as im:
      if (getattr(im, "is_animated", False)):
        if profile.premium:
          print("premium image posted")

          updated_profile['pfp'] = image_name 

          functions.upload_image("pfp", image_value, image_name)  
        else:
          print("premium error")

          messages.error(request, "You need premium for animated posts!")
      else:
        print("image posted")
        
        updated_profile['pfp'] = image_name

        functions.upload_image("pfp", image_value, image_name)
except Exception as e:
  exc_type, exc_obj, exc_tb = sys.exc_info()
  print(exc_type, exc_tb.tb_lineno)
  print(e)

Line 347 (the line that raised the error):

image_value = image.file.getvalue()

Solution

  • It was fixed by changing line 347:

    image_value = image.file.getvalue()
    

    to the following:

    image_value = image.read()
    

    as suggested by mrhd in a comment.