存个py
天上一颗蛋
·
·
个人记录
from PIL import Image
import os
file_dir = 'C:/Users/10767/Desktop/GetBackGround/Pick'
image_filenames = [os.path.join(file_dir, x) for x in os.listdir(file_dir)] #get all url
lenth = len(image_filenames)
imagine = Image.open(image_filenames[0])
width, height = imagine.size
#print("done")
listR = [[0] * width * height for i in range(lenth)] #create list for data
listG = [[0] * width * height for i in range(lenth)]
listB = [[0] * width * height for i in range(lenth)]
R = [0] * width * height #ans
B = [0] * width * height
G = [0] * width * height
for i in range(lenth):
imagine = Image.open(image_filenames[i])
for x in range(width):
for y in range(height):
r, g, b = imagine.getpixel((x, y))
listR[i][x * height + y] = r
listB[i][x * height + y] = b
listG[i][x * height + y] = g
for x in range(width):
for y in range(height):
backetR = [0] * 256 #backet sort
backetG = [0] * 256
backetB = [0] * 256
for i in range(lenth):
backetR[listR[i][x * height + y]] += 1
backetG[listG[i][x * height + y]] += 1
backetB[listB[i][x * height + y]] += 1
#print(backetR[listR[i][x * height + y]])
maxNumR = -1
maxNumG = -1
maxNumB = -1
for i in range(256):
#print(i, backetR[i])
if backetR[i] > maxNumR:
R[x * height + y] = i
maxNumR = backetR[i]
#print(backetR[i], maxNumR)
if backetG[i] > maxNumG:
G[x * height + y] = i
maxNumG= backetG[i]
if backetB[i] > maxNumB:
B[x * height + y] = i
maxNumB = backetB[i]
#print(R[x * height + y], G[x * height + y], B[x * height + y])
newImagine = Image.new("RGB", (width, height))
for x in range(width):
for y in range(height):
#print(R[x * height + y], G[x * height + y], B[x * height + y])
newImagine.putpixel((x, y), (R[x * height + y], G[x * height + y], B[x * height + y]))
#newImagine.show()
saveURL = 'C:/Users/10767/Desktop/GetBackGround/output/output.jpg'
newImagine.save(saveURL)