# Oil Paint Effect With OpenCV Python

> Apply an oil paint effect to images in Python with OpenCV.

- Canonical: https://dennisokeeffe.com/blog/2021-08-12-oil-paint-effect-with-open-cv-python
- Published: 2021-08-12
- Updated: 2021-08-12
- Tags: beginner, python, 100daysofpython

> This is Day 25 of the #100DaysOfPython challenge.

This post will use the [OpenCV Python library](https://docs.opencv.org/4.5.2/d6/d00/tutorial_py_root.html) to apply an oil painting effect to an image.

## Prerequisites

1. Familiarity with [Pipenv](https://pipenv.pypa.io/en/latest/). See [here](https://blog.dennisokeeffe.com/blog/2020-07-04-abcs-of-pipenv) 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.

```s
# 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](https://unsplash.com/@pawel_czerwinski)).

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).

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

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

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

We can now compare by displaying the images:

```py
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](https://cdn.dennisokeeffe.com/assets/2021-08-12-oil-paint-effect-with-open-cv-python/1-original.png)

After applying the effect:

![After effect](https://cdn.dennisokeeffe.com/assets/2021-08-12-oil-paint-effect-with-open-cv-python/2-oil-painting.png)

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

1. [The ABCs of Pipenv](https://blog.dennisokeeffe.com/blog/2020-07-04-abcs-of-pipenv)
2. [OpenCV Python library](https://docs.opencv.org/4.5.2/d6/d00/tutorial_py_root.html)
3. [Pipenv](https://pipenv.pypa.io/en/latest/)

_Photo credit: [`dancristianp`](https://unsplash.com/@dancristianp)_
