The family bought tiny blocks for the kids to play with, and since the instructions that came with them only had a few pictures, they were finished after a few spellings, so I planned to use Python to make a pixel painting picture tool.
The overall logic is very simple.
- Read the image using the Pillow package
- Split the image into a square block of pixels
- Get the color that appears most in the pixel block and use it as the color of the pixel block.
- Re-splicing the image.
The specific code is as follows.
from PIL import Image
pic = Image.open("image.png").convert("RGB")
width, height = pic.size
block = 10 # Set block size
board = Image.new("RGB", (width // block * block, height // block * block))
for y in range(0, height, block):
for x in range(0, width, block):
temp = pic.crop((x, y, x + block, y + block))
temp_list = list(temp.getdata())
max_color = collections.Counter(temp_list).most_common()
# temp_past = Image.new("RGB", temp.size, max_color) Borderless
temp_past = Image.new("RGB", (temp.size - 1, temp.size - 1), max_color)
board.paste(temp_past, (x, y))
The specific effect is as follows.