python图片像素化_如何使用python将正方形图像像素化为256个大像素?

本文介绍了一种使用Python和PIL/Pillow库将图像像素化的方法。通过先将图像缩小到较低分辨率(例如16x16像素),然后放大回原始大小来实现像素效果。

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 ?

解决方案

Nine months pass and I can now cobble together some Python - per your original request on how to pixellate an image with Python and PIL/Pillow.

#!/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')

Original Image

Result

Paddington is so cute - he just needed to be pixellated!

If you take him down to 32x32 pixels (instead of 16x16) and then resize back up, you get:

Keywords:

Pixelate, pixellate, pixelated, pixellated, Paddington, Python, Pillow, PIL, image, image processing, nearest neighbour interpolation.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值