Convert Photo into Pixel Art using Python | by Abhijith Chandradas | Towards Data Science

Withe the rise in popularity of NFTs and crypto art, there is a rise in demand for programmers who can perform image manipulation and generate procedural art. Pixel art has also seen a resurgence in popularity of late, especially among the connoisseurs of digital art. In this article I will explain how to perform image manipulation in python to convert any photo into pixel art.
I will use a cropped version(800px X 800px ) of the above image for the purpose of demonstration.
We will use Pillow Library for image manipulation and Matplotlib library for displaying and saving images.
#Import Librariesfrom PIL import Imageimport matplotlib.pyplot as plt
Image can be read by providing the path to the image as an argument to the open function of Image module of Pillow library. Since our image is in the same folder as the notebook, just the name of the image is sufficient.
Once the image is read, it can be displayed using imshow function of matplotlib.
#Read imageimg=Image.open('mario.jpg')#show imageplt.imshow(img)plt.show()
The first step in creating a pixelated image is conversion of the input image image into a small image using Bilinear interpolation resampling technique.
You can visit the below page to know more about different image resampling techniques.
www.cambridgeincolour.com
This can be achieved by using the resize method by providing the size and resampling technique as arguments.We can convert the 800px X 800px image into 8px by 8px image as below:
small_img=img.resize((8,8),Image.BILINEAR)
The small image is then resized to the desired output size, again by using the resize function.We will use Nearest Neighbour resampling technique which is essentially enlarging each pixel.The default resampling technique is bicubic interpolation, which will sharpen the image.The below code will convert the small image (8px X 8px) into an image which has dimensions 1000 px X 1000px.
#resizeo_size=(1000,1000) #output sizeres=small_img.resize(o_size,Image.NEAREST)#save imageres.save('mario_8x8.png')#display imageplt.imshow(res)plt.show()
Note that we have used save function to save the image locally with filename mario_8x8.png.
The above image contains only 0.01% of the data as that in the original image. Hence it is not surprising if you are not able to recognize the original image from this image.A good pixel art must contain enough data from the original image so that it can be recognized. However, the the fine features will be lost in the process.If the original figure has a lot of fine features which need to be conserved, we should use a much larger size for the initial resizing.The appropriate small image size can be obtained by trial and error method.
The next step is streamlining the processed discussed above by creating a function which generates pixel art from any image. The function must incorporate the following functionalities:i. Read the photo ii. Convert it to pixel art taking the small image size and the output image size as argumentsiii. Save the generated imageiv. Display the original image and the pixel art side by side for comparison
This function can be used to try different small image sizes for the mario image to find an appropriate size to obtain the most visually pleasing pixel art.
We will convert the image to 32px X 32px size to pixelate the image. img.size can be used to get the size of the original image. By providing img.size as the output size, we can generate a pixel art which is of the same size as that of the original image.
photo2pixelart(image='mario.jpg',i_size=(32,32),                    o_size=img.size)
We can observe that the new pixel art shows much more detailing compared to initial one. This figure contains 16 times more data compared to the the 8px version. However it contains only 0.16% data from the original image.
We will scale down the length and breadth of the image by 10%, i.e, 80px X 80px for the next image. This figure would contain 1% data of the original image.
#Function Callphoto2pixelart(image='mario.jpg',i_size=(80,80),                    o_size=img.size)
This image is smoother than the previous image with the features of the image more recognizable.
The code for this tutorial and all the images used and generated are available in my GitHub Repo.
I hope you like the article, I would highly recommend signing up for Medium Membership to read more articles by me or stories by thousands of other authors on variety of topics. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.