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 the 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 which can convert images to 8-bit images using Python.
- 1 What you’ll learn in this tutorial?
- 2 Python Modules for 8-bit Images
- 3 Method -1: Convert to 8-bit Images using Super Pyxelate
- 4 Method -2: Convert to 8-bit Images using Pixelate
- 5 Method -3: Convert to 8-bit Images using OpenCV
- 6 Final Words
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.
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 application of OpenCV:
- 2D and 3D feature toolkits.
- Facial recognition systems.
- Gesture recognition.
- Motion recognition.
- Object detection.
- Segmentation and recognition.
- Motion tracking
This is another image manipulation package for Python which can be used to pixelate or convert normal image to 8-bit images. The module is very handy and comes with a lot of other functionalities like image resize, grayscaling, etc.
This is my personal favourite. Super Pyxelate is the improved and faster version of 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 image, change 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)
We’re importing our required modules then we’re reading our image and store it in a variable called image.
After that, we have defined the palette size to be 7 and downsample_by 14. Which 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 “
io.imsave("pixel.png", new_image): now the image is stored in a file called “pixel.png”.
Now to run the code, I am passing an image called “test.png”. Run the below command to execute the code:
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 succesfully, 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 // pixel_size, image.size // pixel_size), Image.NEAREST ) image = image.resize( (image.size * pixel_size, image.size * pixel_size), Image.NEAREST ) image.save("pixelate.jpg") pixelate("test.jpg",8)
We have defined a function
pixelate() that accepts two parameters:
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 performing the calculations to convert it into 8-bit image.
image.save("pixelate.jpg") is saving the image.
To execute the code use:
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)
We’re first reading the image and storing it in a variable “
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”
To execute the code use paste the below code in your tetminal:
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: