Getting the size of an image with the Python Image Library (PIL) returns its width and height in pixels.
USE Image.Image.size TO RETURN THE SIZE OF AN IMAGE
Call Image.open(fp) to open an image with the filename fp. Use PIL.Image.Image.size with this opened image to return a tuple containing the width and height in pixels.
Python Code:
1 2 3 4 5 6 7 | import Image image = Image.open("sample.png") #image to open width, height = image.size #extract width and height from output tuple print(width, height) #print width and height |
Python Code(Alternative):
1 2 3 4 5 6 | from PIL import Image with Image.open("sample.png") as image: width, height = image.size print(width, height) #print width and height |
Output:
1 2 3 | 1280 720 |
To show image use show() method
1 2 3 4 5 | from PIL import Image with Image.open("sample.png") as im: im.show() |