Is it possible to determine the size of an image by a URL without downloading the entire image?

Given the HTML page of the news article, I am trying to find the appropriate images from the article. To do this, I look at the sizes of the images (if they are too small, they are probably navigational elements), but I do not want to upload each image.

Is there a way to get the width and height of the image without loading the full image?

+4
source share
2 answers

I don’t know if it will help you speed up your application, but it can be done. Make out these two articles:

http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/ for JPEG

http://www.herrodius.com/blog/265 for PNG

They are both intended for ActionScript, but this principle also applies to other languages.

I made a sample using C #. This is not the most beautiful code, and it works only for JPEG, but it can also be easily expanded to PNG:

var request = (HttpWebRequest) WebRequest.Create("http://unawe.org/joomla/images/materials/posters/galaxy/galaxy_poster2_very_large.jpg"); using (WebResponse response = request.GetResponse()) using (Stream responseStream = response.GetResponseStream()) { int r; bool found = false; while (!found && (r = responseStream.ReadByte()) != -1) { if (r != 255) continue; int marker = responseStream.ReadByte(); // App specific if (marker >= 224 && marker <= 239) { int payloadLengthHi = responseStream.ReadByte(); int payloadLengthLo = responseStream.ReadByte(); int payloadLength = (payloadLengthHi << 8) + payloadLengthLo; for (int i = 0; i < payloadLength - 2; i++) responseStream.ReadByte(); } // SOF0 else if (marker == 192) { // Length of payload - don't care responseStream.ReadByte(); responseStream.ReadByte(); // Bit depth - don't care responseStream.ReadByte(); int widthHi = responseStream.ReadByte(); int widthLo = responseStream.ReadByte(); int width = (widthHi << 8) + widthLo; int heightHi = responseStream.ReadByte(); int heightLo = responseStream.ReadByte(); int height = (heightHi << 8) + heightLo; Console.WriteLine(width + "x" + height); found = true; } } } 

EDIT: I'm not a Python expert, but this article seems to describe a Python lib that does just that (the latest sample): http://effbot.org/zone/pil-image-size.htm

+2
source

No, It is Immpossible. But you can get information from img tags, but not from backgrounds.

+1
source

Source: https://habr.com/ru/post/1339534/


All Articles