Creating Instagram Filters With OpenCV and Python

Social and Photography apps such as Instagram, Canva, Snapchat, Picsart, etc. provide a bunch of filters to apply to our portraits and allow us to stylize them according to our choices and needs. An Image filter is a process of modifying an existing image by changing color, hue, sharpness, shade, and other characteristics according to our choice. In this article, we will see how we can create Instagram-like filters with OpenCV.

Import the Required Libraries

For this project, we will need OpenCV for Python and the Numpy package for some operations. So install them as follows:

pip install opencv-python numpy

Now we will be creating our filters.

Creating Instagram Filters With OpenCV

Below are some of the examples showing the creation of Instagram filters with OpenCV in Python:

Grayscale Filter Using OpenCV

For the grayscale filter, we use the cvtColor method of OpenCV to apply the color space as follows:

# greyscale filter
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Python
import cv2
import numpy as np

# choose the image file
image = cv2.imread('portrait.jpg')
# get image width and height
height, width = image.shape[:2]
image = cv2.resize(image, (int(width/3), int(height/3)),
                   interpolation=cv2.INTER_AREA)

# greyscale filter
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# place text at center


def place_text(text, image):
    text_size = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0]
    text_x = (image.shape[1] - text_size[0]) // 2
    text_y = (image.shape[0] + text_size[1]) // 2
    cv2.putText(image, text, (text_x, 50),
                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)


# place text on the image
place_text('Greyscale image', gray)
place_text('Original image', image)

# show the images together
cv2.imshow('Greyscale image', gray)
cv2.imshow('Original image', image)

# wait for a key press
cv2.waitKey(0)

Output:

Explanation:

  • First we import the opencv cv2 and numpy libraries.
  • Then we read the image using cv2.imread() method.
  • Then we apply scaling to image using cv2.resize() method.
  • For grayscale filter, we use the cv2.cvtColor() method where we pass the image and the colour code that is cv2.COLOR_BGR2GRAY.
  • Finally we create a method to place the text at center and then display the images.

Sepia Filter Using Python OpenCV

For sepia filter, we convert our original image by applying sepia tone using a 3×3 matrix and apply the transformation. It is done as follows:

# sepia filter
sepia = np.array([[0.272, 0.534, 0.131],
[0.349, 0.686, 0.168],
[0.393, 0.769, 0.189]])
sepia_image = cv2.transform(image, sepia)

The values are standard for sepia tone. The transform() method applies the tone to each of the pixels.

Python
import cv2
import numpy as np

# choose the image file
image = cv2.imread('portrait.jpg')
# get image width and height
height, width = image.shape[:2]
image = cv2.resize(image, (int(width/3), int(height/3)),
                   interpolation=cv2.INTER_AREA)


# sepia filter
sepia = np.array([[0.272, 0.534, 0.131],
                  [0.349, 0.686, 0.168],
                  [0.393, 0.769, 0.189]])
sepia_image = cv2.transform(image, sepia)


# place text at center
def place_text(text, image):
    text_size = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0]
    text_x = (image.shape[1] - text_size[0]) // 2
    text_y = (image.shape[0] + text_size[1]) // 2
    cv2.putText(image, text, (text_x, 50),
                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)


# place text on the image
place_text('Original image', image)
place_text('Sepia image', sepia_image)

# show the images together
cv2.imshow('Original image', image)
cv2.imshow('Sepia image', sepia_image)

# wait for a key press
cv2.waitKey(0)

Output:

Explanation

  • In this example, we use the sepia filters.
  • After importing and resizing the image, we create a 3×3 matrix that will be applied as a transformation pixel by pixel using cv2.transform() method.
  • The transform() method takes the image and the matrix.
  • Rest of the code is same as previous example.

Pencil and Sketch Filter Using Python OpenCV

Pencil and Sketch filter is achieved using the cv2.pencilSketch() method which takes the image and few parameters to adjust the sketch.

# pencil filter and sketch
pencil, sketch = cv2.pencilSketch(image, sigma_s=40, sigma_r=0.06, shade_factor=0.05)
Python
import cv2
import numpy as np

# choose the image file
image = cv2.imread('portrait.jpg')

# get image width and height
height, width = image.shape[:2]
image = cv2.resize(image, (int(width/3), int(height/3)),
                   interpolation=cv2.INTER_AREA)

# pencil filter and sketch
pencil, sketch = cv2.pencilSketch(
    image, sigma_s=40, sigma_r=0.06, shade_factor=0.05)

# place text at center

def place_text(text, image):
    text_size = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0]
    text_x = (image.shape[1] - text_size[0]) // 2
    text_y = (image.shape[0] + text_size[1]) // 2
    cv2.putText(image, text, (text_x, 50),
                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)


# place text on the image
place_text('Original image', image)
place_text('Pencil image', pencil)
place_text('Sketch image', sketch)

# show the images together
cv2.imshow('Original image', image)
cv2.imshow('Pencil image', pencil)
cv2.imshow('Sketch image', sketch)

# wait for a key press
cv2.waitKey(0)

Output

Explanation

  • Besides the filter, rest of the code is similar to previous examples.
  • The cv2.pencilSketch() method takes the following parameters:
    • image: This is the image to be edited.
    • sigma_s: This is spatial standard deviation for the Gaussian kernel which controls the detailing in image. Lesser value retains more data and vice versa.
    • sigma_r: This is range standard deviation for the bilateral filter which controls the noise in image. Lesser value retains more texture and noise and vice versa.
    • shade_factor: It controls the shading factor of image. Greater value gives darker shade.
  • Finally we display the images.

Enhanced Filter Using OpenCV

This filter is used to achieve detailed image while preserving the overall image. To apply enhanced or HDR filter, we use the detailEnhance() method which enhances the given image as follows:

# enhanced filter
enhanced = cv2.detailEnhance(image, sigma_s=10, sigma_r=0.15)

It takes the optional parameters sigma_s and sigma_r for the filter.

Python
import cv2
import numpy as np

# choose the image file
image = cv2.imread('portrait.jpg')

# get image width and height
height, width = image.shape[:2]
image = cv2.resize(image, (int(width/3), int(height/3)),
                   interpolation=cv2.INTER_AREA)

# enhanced filter
enhanced = cv2.detailEnhance(image, sigma_s=10, sigma_r=0.15)

# place text at center
def place_text(text, image):
    text_size = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0]
    text_x = (image.shape[1] - text_size[0]) // 2
    text_y = (image.shape[0] + text_size[1]) // 2
    cv2.putText(image, text, (text_x, 50),
                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)


# place text on the image
place_text('Enhanced image', enhanced)
place_text('Original image', image)


# show the images together
cv2.imshow('Enhanced image', enhanced)
cv2.imshow('Original image', image)

# wait for a key press
cv2.waitKey(0)

Output

Explanation:

  • Besides the filter, rest of the code is similar to previous examples.
  • cv2.detailEnhance(): It uses a bilateral filter to enhance the details of the image. It uses the following parameters:
    • sigma_s: Used to control the spatial details.
    • sigma_r: Used to control the range details.
  • Overall it increases the sharpness and at the same time improves the clarity as well.

Conclusion

In this tutorial, we learnt to implement various filters and using different kernels, we can create even more filters for our image. While some filters are provided by the OpenCV library as methods, we can create our own filters with the help of kernels or matrices as we did in sepia filter.



Contact Us