Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to pixelate a square image to 256 big pixels with Python Matplotlib?
To pixelate a square image to 256 big pixels with Python, we can use PIL (Python Imaging Library) to resize the image in two steps: first shrink it to create the pixelated effect, then scale it back up. This technique creates the classic "big pixel" or mosaic appearance.
Understanding the Process
The pixelation process works by:
- Resizing the original image to a very small size (16x16 = 256 pixels)
- Using bilinear resampling to reduce detail and create pixel blocks
- Scaling back up to the original size using nearest neighbor to preserve the pixelated look
Example
Here's how to pixelate an image to 256 big pixels ?
from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
# Set figure parameters for better display
plt.rcParams["figure.figsize"] = [10, 5]
plt.rcParams["figure.autolayout"] = True
# Create a sample image for demonstration
# In practice, replace this with: img = Image.open("your_image.png")
sample_array = np.random.randint(0, 256, (400, 400, 3), dtype=np.uint8)
img = Image.fromarray(sample_array)
# Step 1: Resize to 16x16 (256 pixels total) using bilinear sampling
imgSmall = img.resize((16, 16), resample=Image.BILINEAR)
# Step 2: Scale back up using nearest neighbor to preserve pixelated effect
result = imgSmall.resize(img.size, Image.NEAREST)
# Display the results
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
ax1.imshow(img)
ax1.set_title("Original Image")
ax1.axis('off')
ax2.imshow(result)
ax2.set_title("Pixelated Image (256 pixels)")
ax2.axis('off')
plt.tight_layout()
plt.show()
# Save the result
result.save('pixelated_image.png')
print("Pixelated image saved as 'pixelated_image.png'")
Key Parameters
| Parameter | Purpose | Effect |
|---|---|---|
Image.BILINEAR |
Smooth downsampling | Reduces noise and creates clean pixel blocks |
Image.NEAREST |
Sharp upsampling | Preserves pixelated edges without smoothing |
(16, 16) |
Target size | Creates exactly 256 pixels (16×16) |
Working with Real Images
To use this technique with your own images, simply replace the sample array creation with ?
from PIL import Image
# Load your image
img = Image.open("/path/to/your/image.jpg")
# Ensure it's square by cropping if needed
width, height = img.size
size = min(width, height)
img = img.crop(((width - size) // 2, (height - size) // 2,
(width + size) // 2, (height + size) // 2))
# Apply pixelation
imgSmall = img.resize((16, 16), resample=Image.BILINEAR)
result = imgSmall.resize((size, size), Image.NEAREST)
result.save('pixelated_result.png')
Conclusion
Pixelating images involves a two-step resize process: shrinking to 16×16 pixels with bilinear sampling, then scaling back up with nearest neighbor resampling. This technique creates the classic retro pixelated effect perfect for artistic or stylistic purposes.
---