Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function for Python which like getimagesize in PHP?

I have search for a while, and there is a function call get_image_dimensions(), however, as to my understanding, it works for the images which are downloaded or say local. So, any functions or solution like getimagesize in PHP, that we can just get the dimension of an image via URL, instead of path to local?

like image 398
Yann Avatar asked May 16 '11 06:05

Yann


People also ask

How can I get image resolution in PHP?

PHP | imageresolution() Function. The imageresolution() function is an inbuilt function in PHP which is used to set and return the resolution of an image in DPI (dots per inch). If none of the optional parameters is given, the current resolution is returned as an indexed array.

How can I get image width and height in PHP?

The getimagesize() function will determine the size of any supported given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondent HTTP content type.

What is image function PHP?

Image create ( ) function is another inbuilt PHP function mainly used to create a new image. The function returns the given image in a specific size. We need to define the width and height of the required image.


2 Answers

Using the python image library (PIL)

from PIL import Image
im = Image.open("fileName.jpg")
im.size

If you have an url, open it via urlopen and pass the file object to Image.open

import urllib2 as urllib
fd = urllib.urlopen("http://a/b/c")
im = Image.open(fd)
im.size
like image 162
Riccardo Galli Avatar answered Oct 07 '22 17:10

Riccardo Galli


PHP can open a URL as it does a file. This could be a boon (as in your case), or a bane (as in remote file inclusion vulnerability).

Python opts to be explicit in that a file is a file, and a remote resource (URL, for example), is a remote one.

If you need some utility function to get image size from a remote resource, you probably need to write a wrapper to the local one. Usually you only need to read about 4096 bytes to determine the image size.

A little more work, yes, but there's no magic like in PHP.

like image 40
Nam Nguyen Avatar answered Oct 07 '22 15:10

Nam Nguyen