What is? Kilogram imaging
Use python Process thousands of pictures , Put it The final composition of a picture , New picture enlarged , You can see every small picture .
#!/usr/bin/env python # encoding: utf-8 import os from PIL import Image import numpy as np class thousandMapImaging: def __init__(self): self.picture_path = 'assets/picture.jpeg' self.thousand_picture_path = 'assets/images/' def compute_mean(self, imgPath): ''' Get the average color value of the image :param imgPath: Thumbnail path :return: (r,g,b) Of the entire thumbnail rgb Average ''' im = Image.open(imgPath) im = im.convert('RGB') # To rgb Pattern # Convert image data into data sequence . Behavior unit , Each row stores the color of each pixel ''' Such as : [[ 60 33 24] [ 58 34 24] ... [188 152 136] [ 99 96 113]] [[ 60 33 24] [ 58 34 24] ... [188 152 136] [ 99 96 113]] ''' imArray = np.array(im) # mean() The functionality : Find the mean value of the specified data R = np.mean(imArray[:, :, 0]) # Get all R The average of the values G = np.mean(imArray[:, :, 1]) B = np.mean(imArray[:, :, 2]) return (R, G, B) def getImgList(self): """ Get the path and average color of thumbnails :return: list, Stored image path 、 Average color value . """ imgList = [] for pic in os.listdir(self.thousand_picture_path): imgPath = self.thousand_picture_path + pic imgRGB = self.compute_mean(imgPath) imgList.append({ "imgPath": imgPath, "imgRGB": imgRGB }) return imgList def computeDis(self, color1, color2): ''' Calculate the color difference between the two pictures , The of computer is color space distance . dis = (R**2 + G**2 + B**2)**0.5 Parameters :color1,color2 It's color data (r,g,b) ''' dis = 0 for i in range(len(color1)): dis += (color1[i] - color2[i]) ** 2 dis = dis ** 0.5 return dis def create_image(self, bgImg, imgDir, N=10, M=50): ''' According to the background picture , Fill in the new picture with the avatar bgImg: Background picture address imgDir: Avatar catalog N: The magnification of the background image M: The size of the avatar (MxM) ''' # Get a list of pictures imgList = self.getImgList() # Read the picture bg = Image.open(bgImg) # bg = bg.resize((bg.size[0] // N, bg.size[1] // N)) # The zoom . It is recommended to zoom the original image , The picture is too big and the calculation time is very long . bgArray = np.array(bg) width = bg.size[0] * M # The width of the newly generated picture . Magnification per pixel M times height = bg.size[1] * M # The height of the newly generated picture # Create a new blank diagram newImg = Image.new('RGB', (width, height)) # Cyclic filling diagram for x in range(bgArray.shape[0]): # x, Row data , The original width can be used to replace for y in range(bgArray.shape[1]): # y, Column data ,, You can use the height of the original drawing to replace # Find the picture with the smallest distance minDis = 10000 index = 0 for img in imgList: dis = self.computeDis(img['imgRGB'], bgArray[x][y]) if dis < minDis: index = img['imgPath'] minDis = dis # Cycle is completed ,index Is to store the image path with the closest color # minDis The color difference is stored # fill tempImg = Image.open(index) # Open the picture with the smallest color difference distance # Resize the picture , No adjustment is allowed here , Because I adjusted it when I downloaded the picture tempImg = tempImg.resize((M, M)) # Paste the small picture onto the new picture . Be careful x,y , Don't confuse the ranks . apart M Paste a sheet . newImg.paste(tempImg, (y * M, x * M)) print('(%d, %d)' % (x, y)) # Print progress . Format output x,y # Save the picture newImg.save('final.jpg') # Finally save the picture def hello(self): ''' This is a welcome speech :return: self ''' print('*' * 50) print(' ' * 20 + ' Kilogram imaging ') print(' ' * 5 + 'Author: autofelix Date: 2022-01-14 13:14') print('*' * 50) return self def run(self): ''' The program entry ''' self.create_image(self.picture_path, self.thousand_picture_path) if __name__ == '__main__': thousandMapImaging().hello().run()
stay python The interactive co
For a summary, see uploaded re