Codementor Events

Photos Enhancing With Python Programming Technique

Published Dec 17, 2020Last updated Aug 31, 2021
Photos Enhancing With Python Programming Technique

Python is the most reliable and renowned content management system for websites of any kind to create dynamically attractive web resources for their uses.

Python has got everything that developers can ask for to provide reliable user experience to end consumers and develop the business online.

For any website, maintaining the quality of the images becomes challenging because the high-quality image would result in the slow loading speed of the landing pages, which might result in poor user experience.

There are many tools available online that can compress the images and makes them uploadable on the website. For instance, one we can recommend is Zyro Image Upscaler. However, the resulted images would often lose all the visual appeal after they are compressed through an online tool.

Whenever anyone comes across a picture that has poor quality, the user would lose interest.

The excellent quality picture makes people stick to your page and keep interacting with your content.

That's why image enhancement activities play a vital role in getting those details back and make the images visually attractive to the end-users.

Images are edited in such a way to bring back the dynamic range of the given frame so that pictures look more vibrant and professional.

Image enhancement activity would not alter the frame by adding anything in the frame.

In this article, we are going to discuss two image enhancement methods with Python to improve the picture quality on landing pages.

Let's start with Power Law Transformation, and then we will discuss Image Inverse:

Power Law Transformation

Basically, I(i,j) refers to the value of the pixel located in the image. This value represents the intensity of that particular pixel on the photograph, and it falls in the range of 0 to 255 while the i and j relates to the row and column values respectively

For the Power Law Transformation technique, we have an operator called gamma correction that we are going to use in this method to enhance our image.

Below is the operator's equation at the pixel level.

O(i,j) = kI(i,j)^gamma

For most of the scenarios, the value of k is equal to 1 in this equation, so that we will be focusing only on the value of gamma.

So, you can write the equation as shown below.

p(i,j) = I(i,j)^gamma

Now, we will use some libraries to ensure effective performance.

NumPy and OpenCV are the two libraries we are going to use for this example.

Below is the python script that we would write here. Any Standard Python Web Development Agency would be using this script.

import cv2
import numpy as np
 
im = cv2.imread('boat.tiff')
im = im/255.0
im_power_law_transformation = cv2.pow(im,0.6)
cv2.imshow('Original Image',im)
cv2.imshow('Power Law Transformation',im_power_law_transformation)
cv2.waitKey(0)

Please note that we have put the gamma value as 0.6, and we have used a boat.tiff image, which is the sample image. You can set the name of your image in that place.

Also, we have used a black-and-white photograph to enhance its picture quality and understand how this method works.

So, after writing the code, you might get an image which is lighter compared to what you have in the original version.

Now, we will change the gamma value to 1.5 and observe that soon the image becomes darker.

In this particular example, the gamma value represents how we want to display the image digitally.

The human brain uses gamma correction to comprehend an image, and the gamma value is the best display of contrast in the pictures on digital screens.

This is the fundamental benefit of using the Power Law Transformation tool to get the best contrast ratio for images and improvise dynamic range to make it look more catchy.

In another example, we are going to use a color image and enhance it through a python code.

Let's explore another method known as Image Inverse:

Image Inverse
All the color images usually have three color channels, namely red, green, and blue. They are also known as RGB in the graphics world.

So in this example, to modify the color image, we are going to tweak their intensity by subtracting their current values from 255.

@O_R(i,j) = 255 - R(i,j)
O_G(i,j) = 255 - G(i,j)
O-B)i,j) = 255 - B(i,j)

For this particular example, to make it simple, we are going to use a grscale image and applied the operator.

In the first step, you need to extract each intensity value of channels. And to extract the value, we are going to use the Python Imaging Library. The Complete Guide Of Python would prove useful to explore the Python Imaging Library.

For example, you want to extract RGB value at the pixel location (325, 432).

You can do that process by writing below code snippet.

from PIL import Image

im = Image.open('baboon.png')
print im.getpixel((325,432))

As you might have guessed, the getpixel method returns the value of the pixel at a given position.

However, if you run about the script, you would get a random number that would not represent the RGB value of that particular pixel.

So, you need to understand in which mode the system is reading the image. To do that, write the below line of code.

print im.mode

If the output shows ‘P’, it means that the image is being read in the palette mode, so you will have to convert the image in RGB mode through the below code.

rgb_im = im.convert('RGB')

Once you write this, supposedly, you get something like (187, 168 138). This value shows three different channels like red, green, and blue with the values 180, 168, and 138, respectively.

So, in a nutshell, you will have to write code below to execute this task in a single go.

from PIL import Image

im = Image.open('baboon.png')
rgb_im = im.convert('RGB')
print rgb_im.getpixel((325,432))

That's how you can retrieve the RGB value of a particular pixel location in the given frame quickly.

Now, to print all the values of red, green, and blue channels of all pixels in the picture, you can read below Python code snippet.

from PIL import Image

im = Image.open('baboon.png')
rgb_im = im.convert('RGB')
width, height = im.size
 
for w in range(width):
    for h in range(height):
        print rgb_im.getpixel((w,h))

Now, we are going to apply an image inverse operator in our image to revert the colors.

Write the below code snippet to do that.

import scipy.misc
from scipy import misc
from scipy.misc.pilutil import Image
 
im = Image.open('boat.tiff')
im_array = scipy.misc.fromimage(im)
im_inverse = 255 - im_array
im_result = scipy.misc.toimage(im_inverse)
misc.imsave('result.tiff',im_result)

With the above code snippet, the photo would be converted. For knowing in detail about these things, you should contact and Hire Python App Developer. It will help you in the long run.

You will get an image that inverted. The white or bright pictures in the frame would have become darker and vice versa.

That’s how you can enhance the picture quality of your image using the Python programming language.

Conclusion

Do let me know what your thoughts about enhancing your photos with these techniques in Python programming language are.

I would like to know your feedback about this topic, so feel free to share your comments below.

Discover and read more posts from Harikrishna Kundariya
get started
post comments1Reply
Mia Lee
a year ago

Your project seems fantastic! I believe that the image’s originality is essential. While searching for pictures of winding roads recently, I came across great source depositphotos.com/stock-photography.html. I learned more about vector images and how to use them correctly in my projects