I am new to HackerNoon and I see a lot of images used here are pixelated images specially the one’s used as user flairs. I was keen to get myself one too but I’ve successfully dropped the idea now 🤪. I thought I would go and find some online tool to pixelate my picture but then struck me the idea of using Python to do so and I started digging in about doing so using PIL (cause in my teens I had heard of this library and tried a few resizing and rotating operations).
So, now that I know how to pixelate an Image in Python let’s learn to do so.
I learnt to pixelate an image from this article.
The image used in this article has been taken from Pexels.
Pillow/PIL (Python Imaging Library) are the terms that aren’t exactly the same but fork and hence will be used interchangeably within the article.
Pillow a fork of the
Python Imageing Library (PIL).
To install just run:
pip install Pillow
python -m pip install Pillow
Pillow library provides powerful image processing capabilities and also supports extensive file formats.
The most important class in the Python Imaging Library is the
Image class it represents an Image object which can either be a loaded image,processed image or a newly created from scratch image.
Image object has various methods to process the image a few of which are:
resize: resizes an image based on the arguments provided
rotate: rotates an image by the angle provided as an argument
thumbnail: creates a thumbnail out of images for archival purpose
save: used to save an image as well as to convert the image format for example from
crop: crops an specified area from the image
These were some basic utilities from the Pillow library, the library has got a lot more to offer. Visit the official documentation to learn more about this awesome Python library.
We’ll use the following image throughout this tutorial:
from PIL import Image # the Image class is used for creating an image object image = Image.open('face.jpeg') # opening an image file left = image.roatate(90) # rotates the image to the left print(image.size) # prints out (110,100) resized = image.resize((50,50)) # the first argument is a size tuple (width,height) cropped = image.crop((15,30,100,90)) # takes a tuple (start_x,start_y,end_x,end_y) image.save('face.png') # just converts the image from jpeg to png file format
Here are the result images:
Always use the image.show() method before saving any image using image.save(), just to make sure you have processed the image properly.
from PIL import Image image = Image.open('image.jpeg') image_tiny = image.resize((10,10)) # resize it to a relatively tiny size # pixeliztion is resizing a smaller image into a larger one with some resampling pixelated = image_tiny.resize(image.size,Image.NEAREST) # resizing the smaller image to the original size # Image.NEARESEST is the resampling function predefined in the Image class
Here’s our pixelated image:
You can always change the output image by playing around with the resizing factor i.e. by width and height when resizing down the image.
Read more about resizing algorithms in this stackoverflow question.
Well, we learnt some pretty basic operations that can be performed on images with the PIL library and also learnt how to pixelate an image using Python. I am happy to be able to share the different resources I came across while learning on my own. See you later, with another article.