8-bit images are a way of storing image data in which each pixel carries 8 bits of information or you can also say each pixel carries 1 byte of data. Therefore the max number of colors that it can hold of 256 or 2^8. You might have played Nintendo in your childhood, those were 8-bit video games and they can only display 256 different colors. You can also convert images to 8-bit images using Python, sounds good right?

Currently, most of the computer graphics run on 64 bit which means they can display 2^64 different colors. But you can experience 8-bit color through remote desktops. To conserve bandwidth many remote desktop switches to 8-bit for a faster and smoother experience.

8bit might sound outdated as of now, but they’re still being used. For example in smartwatches with ambient displays, there are scanners that use 8bit as their standard, etc.

So, to bring your childhood back we have created this tutorial that can convert images to 8-bit images using Python.

What you’ll learn in this tutorial?

  • Learn about different libraries which you can use to cover images to 8 bit
  • How to convert images to 8-bit images using Python.

Python Modules for 8-bit Images

There are a lot of Python modules available for image manipulation but since our focus is only on 8-bit image conversion, we’ll focus only on few of them.

OpenCV

It is an open-source Python module that is aimed at real-time computer vision. The module is cross-platform which means it supports Java, Python, C++, etc. This is one of the most popular modules in Python which is used for implementing motion detection, video manipulation, image recognition, and even deep learning face recognition.

Some of the applications of OpenCV:

  • 2D and 3D feature toolkits.
  • Facial recognition systems.
  • Gesture recognition.
  • Motion recognition.
  • Object detection.
  • Segmentation and recognition.
  • Motion tracking

Pixelate

This is another image manipulation package for Python which can be used to pixelate or convert normal images to 8-bit images. The module is very handy and comes with a lot of other functionalities like image resize, grayscaling, etc.

Super Pyxelate

This is my personal favorite. Super Pyxelate is the improved and faster version of the original Pyxelate module. The module shares a lot of components with the Pyxelate algorithm and creates 8-bit images. The module also allows you to upscale images, change the dimensions, depth, and grayscaling of the image.

Method -1: Convert to 8-bit Images using Super Pyxelate

Now open your terminal and paste the below code to install the module:

pip3 install git+https://github.com/sedthh/pyxelate.git --upgrade 

This will install the required package i.e. Super Pyxelate. Once installed, now open your favorite editor, create a new file “8bit_pyx.py” and paste the below code:

from skimage import io
from pyxelate import Pyx, Pal

#load image with 'skimage.io.imread()'

image = io.imread("IMAGE_NAME.png")  
downsample_by = 14  # new image will be 1/14th of the original in size
palette = 7  # find 7 colors

#1) Instantiate Pyx transformer

pyx = Pyx(factor=downsample_by, palette=palette)

#2) fit an image, allow Pyxelate to learn the color palette

pyx.fit(image)

#3) transform image to pixel art using the learned color palette

new_image = pyx.transform(image)
save new image with 'skimage.io.imsave()'
io.imsave("pixel.png", new_image)

Code Explanation:

We’re importing our required modules then we’re reading our image and storing it in a variable called image.

After that, we have defined the palette size to be 7 and downsample_by 14. This means the converted image will be 1/14 of the original size. Then, we’re instantiating Pyx transformer.

  • pyx.fit(image): is basically trying to fit the image to the given color palette.
  • new_image = pyx.transform(image): is transforming the image to pixel art using the learned color palette and store it in a variable “new_image“.
  • io.imsave("pixel.png", new_image): now the image is stored in a file called “pixel.png”.

Code Execution:

Now to run the code, I am passing an image called “test.png”. Run the below command to execute the code:

python3 8bit_pyx.py

Input Image:

convert-to-8-bit-images-using-python

Output:

convert-to-8-bit-images-using-python

Method -2: Convert to 8-bit Images using Pixelate

In this method, we’re going to use another package called Pixelate. So in your terminal paste the below code to install the dependency module:

pip3 install pixelate

Once installed successfully, you can create a new file called “pix.py” and paste the below code:

from PIL import Image

def pixelate(input_file_path, pixel_size):
    image = Image.open(input_file_path)
    image = image.resize(
        (image.size[0] // pixel_size, image.size[1] // pixel_size),
        Image.NEAREST
    )
    image = image.resize(
        (image.size[0] * pixel_size, image.size[1] * pixel_size),
        Image.NEAREST
    )

    image.save("pixelate.jpg")

pixelate("test.jpg",8)

Code Explanation:

We have defined a function pixelate() that accepts two parameters: input_file_path and pixel_size. Inside the function, you can see, we’re reading the image and storing it in a variable called “image”.

After that, we’re resizing the image and perform the calculations to convert it into an 8-bit image.

At last, image.save("pixelate.jpg") is saving the image.

Code Execution:

To execute the code use:

python3 pix.py

Input:

convert-to-8-bit-images-using-python

Output:

convert-to-8-bit-images-using-python

Method -3: Convert to 8-bit Images using OpenCV

Now we first have to install OpenCV which s our main player for image manipulation. So, in your terminal paste the below code:

pip3 install opencv-python

Once installed, create a new file “openc.py” and paste the below code:

import cv2

#Input image

input = cv2.imread('test.jpg')
 
#Get input size
 
height, width = input.shape[:2]

#Desired "pixelated" size
 
w, h = (256, 256)
 
#Resize input to "pixelated" size
 
temp = cv2.resize(input, (w, h), interpolation=cv2.INTER_LINEAR)

#Initialize output image
 
output = cv2.resize(temp, (width, height), interpolation=cv2.INTER_NEAREST)
cv2.imwrite("pixelate.jpg", output)

Code Explanation:

We’re first reading the image and storing it in a variable “input“.

After that, we’re fetching the height and width of the image. We also have defined the size of the processed image, for now, it’s 256×256.

The image is now being resized using cv2.resize(input, (w, h), interpolation=cv2.INTER_LINEAR).

At the end, we’re saving the image under the name “pixelate.jpg”

Code Execution:

To execute the code use paste the below code in your terminal:

python3 openc.py

Input:

Output:

Final Words

So, in this article, we have covered three methods to convert any image to 8-bit pixel art. There are other methods and modules too which can be used to convert to 8-bit images using Python.

Here are some useful tutorials that you can read: