DEV Community

Cover image for Image effects with python

Posted on

Image effects with python

The module PIL can be used to change images. You can load an image, resize it to any size. You can apply effects (like image blur) and more.

So how does that work?

First you need an image to change. Lets say this image

Save it as image.png

Resize image

Resize an image is very easy. Load the image, get the width and height. Then apply the thumbnail method. Finally you can change the image format (jpg)

import PIL.Image
im ='image.png')
w, h = im.size
im.thumbnail((w//2, h//2))'image_resize.png', 'jpeg')

Run it, you'll see the image changed

Blur image

To blur an image use the code below. This type of effect works by applying a filter (matrix) to the image. In the case it's applied using the method im.filter():

import PIL.Image, PIL.ImageFilter
im ='image.png')
im2 = im.filter(PIL.ImageFilter.BLUR)'image-blur.png', 'jpeg')

Run to see blur effect applied


Top comments (0)