מור*_*ניק 5 python image matplotlib pillow
我需要找到一种方法,使用python(最好是使用matplotlib和枕头库)将正方形图像减少到256个大像素。
有任何想法吗?
Mar*_*ell 10
九个月过去了,现在我可以按照您最初关于如何使用Python和PIL / Pillow对图像进行像素化的要求,拼凑一些Python。
#!/usr/local/bin/python3
from PIL import Image
# Open Paddington
img = Image.open("paddington.png")
# Resize smoothly down to 16x16 pixels
imgSmall = img.resize((16,16),resample=Image.BILINEAR)
# Scale back up using NEAREST to original size
result = imgSmall.resize(img.size,Image.NEAREST)
# Save
result.save('result.png')
Run Code Online (Sandbox Code Playgroud)
原始图片
结果
帕丁顿是如此可爱-他只需要像素化即可!
如果将他缩小到32x32像素(而不是16x16),然后重新调整尺寸,您将获得:
关键字:
像素化,像素化,像素化,像素化,帕丁顿,Python,枕头,PIL,图像,图像处理,最近邻插值。
另一种选择是使用PyPXL
在 Lab 色彩空间中使用 K-Means 聚类对图像和视频进行像素化的 Python 脚本。视频像素化支持多处理以实现更好的性能。
使用 Paddington 图像作为源,您可以运行:
python pypxl_image.py -s 16 16 paddington.png paddington_pixelated.png
Run Code Online (Sandbox Code Playgroud)
这给出了这个结果
当然,如果您希望它具有 256 x 256 像素而不是 256 个大像素,您可以运行
python pypxl_image.py -s 256 256 paddington.png paddington_pixelated.png
Run Code Online (Sandbox Code Playgroud)
这给出了这个结果
与其他解决方案相比,这两个结果都具有更复古的 8 位外观,这可能适合您的需求。
这是我使用滑动窗口的解决方案
import numpy as np
import matplotlib.pyplot as plt
def pixelate_rgb(img, window):
n, m, _ = img.shape
n, m = n - n % window, m - m % window
img1 = np.zeros((n, m, 3))
for x in range(0, n, window):
for y in range(0, m, window):
img1[x:x+window,y:y+window] = img[x:x+window,y:y+window].mean(axis=(0,1))
return img1
img = plt.imread('test.png')
fig, ax = plt.subplots(1, 4, figsize=(20,10))
ax[0].imshow(pixelate_rgb(img, 5))
ax[1].imshow(pixelate_rgb(img, 10))
ax[2].imshow(pixelate_rgb(img, 20))
ax[3].imshow(pixelate_rgb(img, 30))
# remove frames
[a.set_axis_off() for a in ax.flatten()]
plt.subplots_adjust(wspace=0.03, hspace=0)
Run Code Online (Sandbox Code Playgroud)
主要思想是在图像中滑动一定大小的窗口并计算该区域的平均颜色。然后用该颜色替换该区域中的原始像素。
另一个想法是处理灰度图像。在这里,我们计算一个区域的灰度图像的平均颜色,但现在我们根据平均值是否超过阈值用白色或黑色替换原始像素:
def pixelate_bin(img, window, threshold):
n, m = img.shape
n, m = n - n % window, m - m % window
img1 = np.zeros((n,m))
for x in range(0, n, window):
for y in range(0, m, window):
if img[x:x+window,y:y+window].mean() > threshold:
img1[x:x+window,y:y+window] = 1
return img1
# convert image to grayscale
img = np.dot(plt.imread('test.png'), [0.299 , 0.587, 0.114])
fig, ax = plt.subplots(1, 3, figsize=(15,10))
plt.tight_layout()
ax[0].imshow(pixelate_bin(img, 5, .2), cmap='gray')
ax[1].imshow(pixelate_bin(img, 5, .3), cmap='gray')
ax[2].imshow(pixelate_bin(img, 5, .45), cmap='gray')
# remove frames
[a.set_axis_off() for a in ax.flatten()]
plt.subplots_adjust(wspace=0.03, hspace=0)
Run Code Online (Sandbox Code Playgroud)
请记住: png值介于 0 和 1 之间,而jpg介于 0 和 255 之间
抱歉,我无法为您提供 Python 解决方案,但我可以向您展示技术和结果,只需在命令行中使用ImageMagick :
从这个开始:
首先,使用正常三次插值或双线性插值将图像大小调整为 16x16 像素,然后使用“最近邻”插值将图像缩放回原始大小:
magick artistic-swirl.jpg -resize 16x16 -scale 500x500 result.png
Run Code Online (Sandbox Code Playgroud)
关键词:
像素化,像素化,像素化,像素化,ImageMagick,命令行,命令行,图像,图像处理,最近邻插值。
| 归档时间: |
|
| 查看次数: |
2798 次 |
| 最近记录: |