程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Thousand image imaging of Python special effects

編輯:Python

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 .

One 、 Special effects preview

After processing
After the detail is enlarged

Two 、 Principle of procedure

  • Cut the original image into small pieces one by one
  • Find the most similar piece in thousands of galleries , Replace it with
  • The difficulty is how to find the most similar photos , Of course, the most common is based on color , Find the photo with the most similar color
  • The disadvantage is that there are few pictures in the gallery , When the number of blocks cut from the original drawing is large , The pictures are compared repeatedly more times , It will take a lot of time to increase cpu The load of
  • We can zoom all the photos in the gallery , Then record the information about scaling the photo , And use this information to name the photos , This will reduce the number of repeated comparisons
Do you understand?

3、 ... and 、 Program source code

#!/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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved