code: def guessImageMetadataFromData(img_data):
      
      format, width, height = None, None, None
      img_stream = io.BytesIO(img_data)
      try:
        img = PIL.Image.open(img_stream)
      except IOError:
        format = imghdr.what(None, h=img_data)
        format = SUPPORTED_IMG_FORMATS.get(format, None)
      else:
        format = img.format.lower()
        format = SUPPORTED_IMG_FORMATS.get(format, None)
        width, height = img.size
      return format, width, height
docs: Identify an image format and size from its first bytes.