How can I find the resolution of an image with any extension without using any module that is not part of Python's standard library (except perhaps PIL)?
Asked
Active
Viewed 703 times
1
-
1`Image.open("filename").size` ? `Image` can be imported from `PIL`, they support a [ton of file formats](https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html) – GammaGames Jan 05 '22 at 16:29
-
@GammaGames Great! Thanks a lot, that worked out well. Can you add that as an answer? – Robo Jan 05 '22 at 16:34
-
Does this answer your question? [setting image resolution in Python pillow](https://stackoverflow.com/questions/40829577/setting-image-resolution-in-python-pillow) – pippo1980 Jan 05 '22 at 16:38
-
Er... no it doesn't... I want to read the image resolution, not set it. And anyway, @GammaGames's comment's already helped me out – Robo Jan 05 '22 at 16:41
-
https://stackoverflow.com/questions/6444548/how-do-i-get-the-picture-size-with-pil resolution and dimension is not the same – pippo1980 Jan 05 '22 at 16:48
-
Hmm... a mistake in my understanding, perhaps, but GammaGames's answer was still what I was looking for – Robo Jan 05 '22 at 16:48
-
das Gut ........ – pippo1980 Jan 05 '22 at 16:49
-
Zero-dependency on non-default libraries as requested here... https://stackoverflow.com/a/19035508/2836621 – Mark Setchell Jan 05 '22 at 18:34
1 Answers
3
You can get an image's resolution with the Pillow package:
from PIL import Image
with Image.open("filename") as image:
width, height = image.size
You can find more in the documentation
As pippo1980 pointed out, this is for the dimensions of the image and not for the image's resolution. For completeness, here's how to get an image's resolution:
from PIL import Image
with Image.open("filename") as image:
xres, yres = image.info['dpi']

GammaGames
- 1,617
- 1
- 17
- 32
-
1
-
2@pippo1980 You're right, I tend to use the terms interchangeably but just in case others come across the issue I've updated the answer – GammaGames Jan 05 '22 at 18:10
-
Actually I didnt know about xres and yres !!! Thanks see here too: https://stackoverflow.com/questions/54517814/using-pillow-to-find-dpi never thought about non-square pixels – pippo1980 Jan 05 '22 at 20:33