用户:
1354派派查看:0 回复:0 评论:0 创建时间:2022-06-11T22:53:49
输入图片名时要把图片格式也带着。要把代码文件与图片保存到同一个路径。
from PIL import Image
import matplotlib.pyplot as plt
import collections
def RGB(file):
pic = Image.open(file).convert("RGB")
width,height = pic.size
block = 10
#piece = pic.crop((width/2-10/2,height/2-10/2,width/2+10/2,height/2+10/2))
#piece.show()
board = Image.new("RGB",pic.size)
for y in range(0,height,block):
for x in range(0,width,block):
temp = pic.crop((x,y,x+block,y+block))
#temp.show()
temp_list = list(temp.getdata())
max_color = collections.Counter(temp_list).most_common()[0][0]
temp_past = Image.new("RGB",temp.size,max_color)
print(max_color)
board.paste(temp_past,(x,y))
# plt.imshow(board)
# plt.pause(0.01)
board.show()
file_1 = input("请输入图片名称:",)
#file = file_1 + ".png"
RGB(file_1)