TOPIC: Python Mini Project 02 - Finding Resolution of an Image
Difference between Dimension and Resolution of an Image:
Dimension | Resolution |
---|---|
Definition: Image dimensions refer to the physical size of the image, typically measured in terms of width and height. It indicates the number of pixels along the width and height of the image. |
Definition: Image resolution refers to the level of detail or quality in an image. It is measured in pixels per inch (PPI) or dots per inch (DPI) and signifies the density of pixels within a specific area. |
Representation: Dimensions are expressed in pixels (px). For instance, an image with dimensions 1920x1080 means it has a width of 1920 pixels and a height of 1080 pixels. |
Quality and Clarity: Higher resolution generally means better quality and sharper images. A higher PPI/DPI results in more pixels packed into a given area, providing greater detail and clarity. |
Aspect Ratio: The relationship between the width and height of an image is known as the aspect ratio. It describes how these dimensions relate to each other, such as 4:3, 16:9, etc. |
Print/Screen Res: Resolution requirements differ for print and digital media. Print typically requires higher resolutions (300 DPI or more) to ensure clear and crisp prints, while screen resolution (72-150 PPI) works well for digital display. |
Visual Appearance: Changing the dimensions directly affects the visual appearance of an image. Altering the width or height can stretch or shrink the image, potentially distorting the content. |
Resizing Impact: Changing the resolution without adjusting the dimensions can affect image quality. Increasing resolution without additional pixels can make the image appear pixelated, while decreasing it might result in loss of detail. |
Importance in Layouts: Image dimensions are crucial in web design, print media, and digital graphics. They dictate how images fit into layouts, determining how they appear on different devices or media. |
File Size and Storage: Higher resolution images generally have larger file sizes as they contain more data. Storing or sharing high-resolution images may require more storage space and bandwidth compared to lower resolution versions. |
As we have learnt the difference between Dimension and Resolution of an Image, we can go on to the next step for finding the Resolution of an Image using Python Program. With the help of python program, we can easily find the resolution of the image for any type like the JPEG, PNG, Bitmap, etc. So, in order to find the resolution, we need to know where the specific byte position of the image file is. For that, we must look at the following image: After understanding this image, we can look at the explanation of the program.
Explanation of the program:
We declare a d function 'fRes' which takes the file_name as input, which represents the image file
(JPEG - .jpg) for which the resolution needs to be determined. Then, we use the open() function in
binary mode ('rb') to open the image file for reading binary data. After that, the function
navigates to a specific byte position in the image file (image.seek(163)) known to contain the
resolution data in the case of JPEG files. It then reads 2 bytes (image.read(2)) each for height
(h_var) and width (w_var) information from the image file. The code then computes the resolution
values for height (resHeight) and width (resWidth) by performing bitwise operations and bit shifting
to combine the two bytes into a single value. Finally, the function prints the extracted resolution
values in the format "Width X Height" to the console. The byte position (163) used to extract
resolution information might vary for different image formats. For JPEG files, it's common, but
other formats may have different positions for storing such metadata.
NOTE:
This method of extracting resolution information by seeking specific
byte positions may not work universally for all image formats or versions. Different image
formats
may store resolution information differently or at various byte offsets within the file
Watch the video on YouTube to understand more about finding the Resolution of an Image
Code as discussed in the video:
# Finding the resolution of an Image
def fRes(file_name):
with open(file_name, 'rb') as image:
# image.seek(22) # for png
image.seek(163) # for jpg
h_var = image.read(2)
resHeight = (h_var[0] << 8) + h_var[1]
w_var = image.read(2)
resWidth = (w_var[0] << 8) + w_var[1]
print("The resolution of the image is: ", resWidth, "X", resHeight)
# fRes("file.png")
fRes("file.jpg")
You can copy and run this code