The method iteratively approximates a pixel art by sampling values based on the orientation of edges in the input image. Then it applies unsupervised machine learning to generate the basis for an 8-bit color palette.
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("blade_runner.jpg")
# generate pixel art that is 1/14 the size
height, width, _ = img.shape
factor = 14
colors = 6
dither = True
p = Pyxelate(height // factor, width // factor, colors, dither)
img_small = p.convert(img) # convert an image with these settings
_, 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:
Once the class is created, call convert(image) by passing a NumPy array representation of the image.
NOTE: the process is pretty time consuming, generating large pixel arts can take quite a while!
The method applies a few computer vision functions for preprocessing. Then simple convolutions are applied on the images. The downsampled areas are calculated based on their gradients' magnitudes and orientation.
The function was inspired by the Histogram of Oriented Gradients method.
Once it's done, a Bayesian Gaussian Mixture model is fitted (instead of conventional K-means) to find a reduced palette.
Using the centroids of the overlapping gaussians as "mean" colors is an empirically better choice,
as cluster centroids for rare colors would have less effect on the rest of the palette due to
their smaller covariances (allowing flatter gaussians to eventually take over).
Since it also predicts probabilities, iteratively polling from the first and second best prediction over a threshold allows simple dithering.
The dirichlet distributions will put less weight on unnecessary clusters as well.
pyx.py
is the command line interface for the Pyxelate class and accepts a bunch of arguments to process multiple files one after another.
usage: pyx.py [-h] [-i folder of input images] [-o folder of output images]
[-f scale down input image by factor] [-s scale up output image by factor] [-c colors] [-d dither]
[-a alpha] [-r regenerate_palette] [-t random_state]
If no --output was defined, a pyxelated/ folder will be created for output images.
There is also a basic GUI that runs the CLI from a Tkinter window.
The Pyxelate class requires Python 3.7+ and relies on the following libraries to run:
The source code is available under the MIT license.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。