松哥分享了两种生成像素风格图像的方法,包括像素风证件照。方法一用Python的PIL库,将图片分成块,以块中最多颜色填充,效果一般。方法二用Tiler工具,可生成像素画,支持自定义像素块,如用PaddlePaddle的logo,生成时间较长,结果保存为out.png。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

from PIL import Imagedef init():
# 设置每个像素区块的大小
block_size = 75
img = Image.open("bao5.jpg") # 获取图片的宽高
width, height = img.size # 获取像素点对应RGB颜色值,可以改变img_array中的值来改变颜色值
img_array = img.load() # 为了处理最后的区块,加了一次循环
max_width = width + block_size
max_height = height + block_size for x in range(block_size - 1, max_width, block_size): for y in range(block_size - 1, max_height, block_size): # 如果是最后一次循环,则x坐标等于width - 1
if x == max_width - max_width % block_size - 1:
x = width - 1
# 如果是最后一次循环,则x坐标等于height - 1
if y == max_height - max_height % block_size - 1:
y = height - 1
# 改变每个区块的颜色值
change_block(x, y, block_size, img_array)
y += block_size
x += block_size
img.save(r'result_3.jpg')
img.show()"""
:param x坐标 x:
:param y坐标 y:
:param 区块大小 black_size:
:param 可操作图片数组 img_array:
"""def change_block(x, y, black_size, img_array):
color_dist = {}
block_pos_list = [] for pos_x in range(-black_size + 1, 1): for pos_y in range(-black_size + 1, 1): # todo print(x + pos_x,y + pos_y)
block_pos_list.append([x + pos_x, y + pos_y]) for pixel in block_pos_list: if not str(img_array[pixel[0], pixel[1]]) in color_dist.keys():
color_dist[str(img_array[pixel[0], pixel[1]])] = 1
else:
color_dist[str(img_array[pixel[0], pixel[1]])] += 1
# key-->value => value-->key
new_dict = {v: k for k, v in color_dist.items()}
max_color = new_dict[max(color_dist.values())] # 将区块内所有的颜色值设置为颜色最多的颜色
for a in block_pos_list:
img_array[a[0], a[1]] = tuple(list(map(int, max_color[1:len(max_color) - 1].split(","))))def get_key(dict, value):
return [k for k, v in dict.items() if v == value]if __name__ == "__main__":
init()!git clone https://gitee.com/jingsongliujing/tiler.git#!git clone https://github.com/nuno-faria/tiler.git
Cloning into 'tiler'... remote: Enumerating objects: 2082, done. remote: Counting objects: 100% (2082/2082), done. remote: Compressing objects: 100% (1973/1973), done. remote: Total 2082 (delta 106), reused 2082 (delta 106), pack-reused 0 Receiving objects: 100% (2082/2082), 13.34 MiB | 617.00 KiB/s, done. Resolving deltas: 100% (106/106), done. Checking connectivity... done.
!pip install -r tiler/requirements.txt
Looking in indexes: https://mirror.baidu.com/pypi/simple/ Requirement already satisfied: opencv-python in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r tiler/requirements.txt (line 1)) (4.1.1.26) Requirement already satisfied: numpy in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r tiler/requirements.txt (line 2)) (1.16.4) Requirement already satisfied: tqdm in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from -r tiler/requirements.txt (line 3)) (4.36.1)
!python /home/aistudio/tiler/tiler.py /home/aistudio/tiler/images/bao3.jpg /home/aistudio/tiler/tiles/at/gen_at
Loading tiles 100%|█████████████████████████████████████████| 125/125 [00:01<00:00, 68.66it/s] Gettting and processing boxes 100%|█████████████████████████████████████████████| 5/5 [01:43<00:00, 20.76s/it] Creating tiled image 100%|██████████████████████████████████| 50000/50000 [00:01<00:00, 29059.27it/s]
drop└── drop.png
!python /home/aistudio/tiler/gen_tiles.py /home/aistudio/tiler/tiles/drop/drop.jpg
100%|█████████████████████████████████████████████| 5/5 [00:30<00:00, 6.14s/it]
!python /home/aistudio/tiler/gen_tiles.py /home/aistudio/tiler/tiles/paddle/paddle.jpg
100%|████████████████████████████████████████████| 5/5 [00:00<00:00, 152.48it/s]
!python /home/aistudio/tiler/tiler.py /home/aistudio/tiler/images/gai1.jpg /home/aistudio/tiler/tiles/paddle/gen_paddle
Loading tiles 100%|████████████████████████████████████████| 125/125 [00:00<00:00, 160.80it/s] Gettting and processing boxes 100%|████████████████████████████████████████████| 5/5 [09:29<00:00, 113.89s/it] Creating tiled image 100%|██████████████████████████████| 1112395/1112395 [00:33<00:00, 33386.27it/s]
以上就是松哥带你生成像素风格图像(像素风证件照也是没问题滴~)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号