0

Program to download an image from a given URL

import requests

url = f"https://c4.wallpaperflare.com/wallpaper/296/372/981/wall-e-pixar-disney-movies-wallpaper-preview.jpg"

response : requests.Response = requests.get(url)
print(response.headers)

I want to understand how to find out the dimensions(width x height) of image.

Content-Length which is in headers of the response gives how many bytes(including red, green & blue channel) that image contains but not the dimensions.

response.content gives the content of image which are bytes but how to find the dimension of the image?

Using PIL.Image.size we can also get the size of image but I want to understand how to get that from response which we get.

I have refferred this question of StackOverflow but the answer by ElJeffe I cannot understand.

Udesh
  • 2,415
  • 2
  • 22
  • 32
  • You could parse the JPEG yourself. Why you would want to do this when you could use PIL is beyond me. – Sören Dec 03 '22 at 11:30
  • If response.content contains the image data you will need to determine the image file format (e.g. png, jpg, gif,...), then extract the bytes that determine image dimensions for that particular format (different for every format, and possibly quite complicated). The other answer you refer to downloads just a little bit of the file image file -- the bit that is likely to contain image dimensions -- and feeds it to PIL to decode. – Tony Dec 03 '22 at 11:36
  • See last part of this answer to get a PIL Image from a requests response https://stackoverflow.com/a/59586273/2836621 – Mark Setchell Dec 06 '22 at 23:11

0 Answers0