Pyxelate is a Python class that converts images into tiny pixel arts with limited color palettes.
pip3 install git+https://github.com/sedthh/pyxelate.git
from pyxelate import Pyxelate
from skimage import io
import matplotlib.pyplot as plt
img = io.imread("cat.jpg")
# generate pixel art that is 1/12 the size
height, width, _ = img.shape
colors = 8
p = Pyxelate(height // 12, width // 12, colors)
img_small = p.convert(img) # convert img
_, axes = plt.subplots(1, 2, figsize=(16, 16))
axes[0].imshow(img)
axes[1].imshow(img_small)
plt.show()
The Pyxelate() class accepts the following init parameters:
- height: the height of the result image (height was chosen to be first parameter to mirror the array representation)
- width: the width of the result image
- color: the number of colors (default is 8)
- regenerate_palette: if set to False, then the palette will only be generated once, and all future images will be generated using this original palette. This is useful for generating a sequence of images with the same palette (the default value is True, all images will have their own palettes).
- random_state: the random state for the Bayesian Gaussian Mixture model (default is 0)
Once the class is created, call convert(image) by passing a NumPy array representation of the image.
The method applies a few computer vision functions and simple convolutions on images and selects pixels based on the calculated gradient's magnitude. This was inspired by the Histogram of Oriented Gradients method. Then a Gaussian Mixture model is fitted (instead of conventional K-means) to find a reduced palette based on its components.
The Pyxelate class requires Python 3.7+ and relies on the following libraries to run:
Limitations: the method does not support dithering, as it would most likely add noise to areas with color gradients, making edges less clear.
The source code is available under the MIT license.