Here in this article, we have provided two different python source codes which can find the resolution of a jpeg image.
Prerequisite Python Program to Find the Size (Resolution) of Image
- Python Function
- Python File handling.
- Python PIL library
- Python user-defined function
- Python file I/O
Python Program to Find the Size (Resolution) of Image
Find the Size (Resolution) of a JPEG Image Without any External Library
def image_resolution(filename):
# open image in binary mode
with open(filename,'rb') as img:
# height of image (in 2 bytes) is at 164th position
img.seek(163)
# read the 2 bytes
x = img.read(2)
# calculate height
height = (x[0] << 8) +x[1]
# next 2 bytes is width
x = img.read(2)
# calculate width
width = (x[0] << 8) + x[1]
print("Width X Height: ", width,"X",height)
image_resolution("image.jpg")
Output:
Width X Height: 57601 X 255
Find the Size (Resolution) of Image With PIL Library
Use the pip install PIL command to install the PIL library:
Code:
from PIL import Image
image = "Image.jpg"
img = Image.open(image)
width, height = img.size
print("The image resolution is", width, "x", height)
Output:
The image resolution is 1333 x 1000
People are also reading:
- Python Program to Find Sum of Natural Numbers Using Recursion
- Python Program to Merge Mails
- WAP to find quotient and remainder of two numbers
- WAP to calculate sum and average of three numbers
- Python Program to Display Calendar
- Python Program to Find HCF or GCD
- How to Check if a Python String Contains Another String?
- WAP in C++ and python to find the root of quadratic equation
- C Program to Extract a Portion of String
- WAP in C++ and Python to calculate 3X3 Matrix multiplication
Leave a Comment on this Post