paint-brush
Pixelated Images Using Pythonby@h3avren
6,158 reads
6,158 reads

Pixelated Images Using Python

by Ajay Singh RanaJanuary 18th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Use PIL the following way to create a pixelated Image: from PIL import Image image = Image.open(image_name) image_small = image.resize((width,height)) pixelated = image_small.resize(image.size,Image.NEAREST) pixelated.save(save_as)

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Pixelated Images Using Python
Ajay Singh Rana HackerNoon profile picture


What pushed me to learn to pixelate an Image?

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.

This article will proceed as:

  • Requirements : we will install the requirements of this tutorial
  • An overview of the Pillow library
  • Trying out some basic functions on an Image
  • And finally we’ll pixelate an Image

Requirements

Pillow a fork of the Python Imageing Library (PIL).

To install just run:

pip install Pillow

or

python -m pip install Pillow

Let’s have an overview of the Pillow library

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.

The 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 jpeg to png
  • 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.

Let’s try some basic functions:

We’ll use the following image throughout this tutorial:

tutorial image


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:

rotate left

resized image

cropped image


Always use the image.show() method before saving any image using image.save(), just to make sure you have processed the image properly.

Let’s pixelate the image now

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.

Conclusion

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.