Back to home

Oil Paint Effect With OpenCV Python

Published: Aug 12, 2021

Last updated: Aug 12, 2021

This is Day 25 of the #100DaysOfPython challenge.

This post will use the OpenCV Python library to apply an oil painting effect to an image.

Prerequisites

  1. Familiarity with Pipenv. See here for my post on Pipenv.
  2. An image to use with the OpenCV library.

Getting started

Let's create the oil-paint-effect-with-open-cv-python directory and install Pillow.

# Make the `oil-paint-effect-with-open-cv-python` directory $ mkdir oil-paint-effect-with-open-cv-python $ cd oil-paint-effect-with-open-cv-python $ touch main.py # Init the virtual environment $ pipenv --three $ pipenv install opencv-python opencv-contrib-python # if you have issues with a hanging lockfile, try add the --skip-lock option

At this stage, you will need to add an image to the root of your directory. In my case, I will add base_img.jpg to the directory (which will be an image from Unsplash).

We are now ready to start coding!

Applying the oil painting effect

This section simply loads the image in var base_img (assuming you are following the directory structure where the notebook is in the docs folder).

import cv2 img = cv2.imread('./base_img.jpg')

Once that is complete, we can apply the oil paiting effect with one liner of code:

res = cv2.xphoto.oilPainting(img, 7, 1)

We can now compare by displaying the images:

cv2.imshow("original", img) cv2.imshow("res", res) cv2.waitKey(0) cv2.destroyAllWindows()

This will display the images in a window.

The original image:

Original

Original

After applying the effect:

After effect

After effect

When you are finished with viewing, hit escape to exit.

Summary

Today's post demonstrated how to use the OpenCV package to programmatically apply an oil painting effect to an image.

Resources and further reading

Photo credit: dancristianp

Personal image

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on Visibuild.

1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.

Oil Paint Effect With OpenCV Python

Introduction

Share this post