The content of this article is about the code examples of Python to implement image pixelation. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Cause
I saw pixel pictures on the Internet and found them quite interesting, so I planned to write one using some PIL libraries in python.
Implementation idea
Divide a picture into multiple blocks, and the color of each block is equal to this color The color with the most colors in the block, as shown below.
This picture takes 2×2 pixels as the block size, stores the color in the block and the number of occurrences of each color in the dictionary, takes the largest color, and fills it The whole block.
Specific implementation
from PIL import Image def init(): # 设置每个像素区块的大小 block_size = 75 img = Image.open("a.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'D:\python\pixel_image\awesome_copy.png') 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()
Effect comparison
##Summary
Open source addresshttps://github.com/MasakiOvO/...
The above is the detailed content of Python code example to implement image pixelation. For more information, please follow other related articles on the PHP Chinese website!