gpt4 book ai didi

python - 如何用python将方形图像像素化为256个大像素?

转载 作者:太空狗 更新时间:2023-10-30 01:50:08 27 4
gpt4 key购买 nike

我需要找到一种使用 python 将正方形图像缩小到 256 个大像素的方法,最好使用 matplotlib 和 pillow 库。

有什么想法吗?

最佳答案

九个月过去了,我现在可以拼凑一些 Python - 根据您最初关于如何使用 Python 和 PIL/Pillow 对图像进行像素化的请求。

#!/usr/local/bin/python3
from PIL import Image

# Open image
img = Image.open("artistic-swirl.jpg")

# Resize smoothly down to 16x16 pixels
imgSmall = img.resize((16,16), resample=Image.Resampling.BILINEAR)

# Scale back up using NEAREST to original size
result = imgSmall.resize(img.size, Image.Resampling.NEAREST)

# Save
result.save('result.png')

原始图片

enter image description here

结果

enter image description here


如果将其缩小到 32x32 像素(而不是 16x16),然后重新调整大小,您会得到:

enter image description here

关于python - 如何用python将方形图像像素化为256个大像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47143332/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com