今天刷到一个很酷的壁纸,准备动手,准备动手
废话不多说上代码
首先
pip install pillow
如果下载速度太慢换阿里源(百试不爽
pip install pillow -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
from PIL import Image, ImageDraw
# 把原图跟本代码放在同一目录下
image = Image.open('原图') # 括号里填图片名称+图片格式
radius = 10 # 波点半径
offset = 3 # 波点间距
output_image = Image.new("RGB", image.size)
draw = ImageDraw.Draw(output_image)
# 绘制
for x in range(0, image.width, radius * 2 + offset):
for y in range(0, image.height, radius * 2 + offset):
box = (x, y, x + radius * 2, y + radius * 2)
region = image.crop(box)
stat = ImageStat.Stat(region)
average_color = tuple(int(c) for c in stat.median)
draw.ellipse(box, fill=average_color)
# 保存图片&图片格式类型可更改,这里为png
output_image.save('pixelate_output.png')
其中
radius = 10 # 波点半径
offset = 3 # 波点间距
可自行调整波点间距和半径,所选图片分辨率越高越好
完事儿~上图