Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pixelate a square image to 256 big pixels with python?

I need to find an way to reduce a square image to 256 big pixels with python, preferably with the matplotlib and pillow libraries.

Got any ideas ?

like image 461
Moran Reznik Avatar asked Nov 06 '17 18:11

Moran Reznik


People also ask

How many pixels is my image in Python?

Use PIL to load the image. The total number of pixels will be its width multiplied by its height.


2 Answers

Sorry, I can't give you a Python solution, but I can show you the technique and the result, just using ImageMagick at the command-line:

Starting with this:

enter image description here

First, resize the image down to 16x16 pixels using normal cubic, or bilinear interpolation, then scale the image back up to its original size using "nearest neighbour" interpolation:

magick artistic-swirl.jpg -resize 16x16 -scale 500x500 result.png

enter image description here

Keywords:

Pixelate, pixellate, pixelated, pixellated, ImageMagick, command-line, commandline, image, image processing, nearest neighbour interpolation.

like image 132
Mark Setchell Avatar answered Oct 05 '22 20:10

Mark Setchell


Another option is to use PyPXL

Python script to pixelate images and video using K-Means clustering in the Lab colorspace. Video pixelating support multi-processing to achieve better performance.

Using the Paddington image as a source you can run:

python pypxl_image.py -s 16 16 paddington.png paddington_pixelated.png

Which gives this result

enter image description here

Of course if you wanted it to have 256 x 256 pixels rather than just 256 big pixels you could run

python pypxl_image.py -s 256 256 paddington.png paddington_pixelated.png

Which gives this result

enter image description here

Both results have a more retro 8-bit look to them compared to the other solutions, which might suit your needs.

like image 22
hellocatfood Avatar answered Oct 05 '22 20:10

hellocatfood