In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
In order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way to solve this problem. How to splice Lyons with QQ friends' avatars under Python picture production.
In this blog, we will implement two functions:
Merge all avatars into a larger image
Synthesize a large image of all the avatars with a certain template
Again, give all the running effect pictures first:
Code implementation
1. Libraries required by the code
Import requests,codecs,re,urllib,os,random,mathfrom PIL import Imageimport numpy as npimport cv2 as cv2, code explanation
2.1. Merge a small avatar into a larger image
For this, just paste each small avatar on the big picture, which can be solved by using the paste function of Image. For the order of posting, you can directly follow the following illustration to paste one by one:
So, just give the code:
Def simple_split (filepackage,size,littlesize): # simple stitching. The parameter is the file name of the picture, and the size of each row and column. The size of the small avatar picture row = size [0] col = size [1] bigimg = Image.new ('RGBA', (littlesize*row,littlesize*col)) # result figure number = 0 for i in range (row): # row for j in range (col): # column randpic = random.randint (1) img = Image.open (filepackage+str (randpic) +' .png'). Convert ('RGBA') img = img.resize ((littlesize) Littlesize)) loc = (iLife1) * littlesize, (juni1) * littlesize) print (loc,number) number+=1 bigimg.paste (img,loc) bigimg.save (resultSavePath)
Because we don't have many friends, we randomly choose a friend avatar to post it every time, so if your density is high, the last avatar has a lot of repetitive avatars.
Let me show you my last picture:
2.2. Stitching a picture with a picture as a template
I didn't know if there was a third-party library that could be made directly, so I built a small wheel myself.
Train of thought:
Divide the template into small pictures of A x B, describe its position as pic [I] [j], then get the average RGB value of each small image, compare the average RGB value of pic [I] [j] with the RGB value of friends' avatars, find out the closest avatar, and then insert the avatar in the pic [I] [j] of the image.
The train of thought is relatively simple, isn't it?
Then there's the implementation: the code is commented in many places, so I won't say much about it, but I'll just give you the code:
Import requests,codecs,re,urllib,os,random Mathfrom PIL import Imageimport numpy as npimport cv2 as cvtxtpath = 'CGV _ peg' # the file that you pasted from QQ Mail savepath = 'CJV _ UsersUnix _ Number of friends all_mean_rgbs = [] # Storage calculated average RGB value def meanrbg (img): # calculate the average picture rgb rgb = np.array (img) r = int (round (np.mean (rgb [: :, 0])) g = int (round (np.mean (rgb [:,:, 1])) b = int (round (np.mean (rgb [:,:, 2])) return (def gettouxiang (txtpath): # enter your txt file storage location file = codecs.open (txtpath,'rb') 'utf-8') s = file.read () pattern = re.compile (r'\ droomqq.com') all_mail = pattern.findall (s) # regular expressions match all qq numbers all_link = [] # used to store links that need to be accessed url =' http://qlogo.store.qq.com/qzone/' for mail in all_mail: qq = mail.replace ('@ qq.com' '') l = url + qq+'/'+ qq+'/100' all_link.append (l) I = 1 for link in all_link: # traversal link Download avatar saveurl = savepath+str (I) + '.png' savaImg (link,saveurl) I + = 1 print ('downloaded', I) friends_count = len (all_link) # get the number of friends avatars return Truedef savaImg (picurl,saveurl): # store the picture function, and picurl is the URL of the picture Saveurl is the local storage location try: bytes = urllib.request.urlopen (picurl) file = open (saveurl,'wb') file.write (bytes.read ()) file.flush () file.close () return True except: print ('worry') savaImg (picurl,saveurl) def simple_split (filepackage,size,littlesize): # simple stitching, parameter is image file name, size of each line and column The size of the small avatar row = size [0] col = size [1] bigimg = Image.new ('RGBA', (littlesize*row,littlesize*col)) number = 0 for i in range (row): for j in range (col): randpic = random.randint (1) img = Image.open (filepackage+str (randpic) +' .png'). Convert ('RGBA') img = img.resize (littlesize,littlesize) loc = (i*littlesize) J*littlesize, (iTun1) * littlesize, (littlesize) print (loc,number) number+=1 bigimg.paste (img,loc) bigimg.save (resultSavePath) def mode_split (filepackage,modepath,bigsize,littlesize): # storing avatars in templates row = bigsize [0] # how many small avatars per row col = bigsize [1] # each column suitSize = (littlesize*row Littlesize*col) # the final pixel of the large image size bigImg = Image.open (modepath) bigImg = bigImg.resize (suitSize) resultImg = Image.new ('RGBA',suitSize) for i in range (row): for j in range (col): cutbox = (Jacu1) * littlesize) # template cut for comparison cutImg = bigImg.crop (cutbox) # copy to cutImg tmprgb = meanrbg (cutImg) suitOne = mostSuitImg (tmprgb) + 1 # compare the most appropriate avatar img = Image.open (filepackage + str (suitOne) + '.png'). Convert ('RGBA') img = img.resize ((littlesize,littlesize)) resultImg.paste (img) Cutbox) print ('pasted', cutbox) resultImg.save (resultSavePath) # Storage def mostSuitImg (tmprgb): # for comparison Find the most suitable avatar global all_mean_rgbs minRange = 200000 id = 0 for rgb in all_mean_rgbs: tmp = (rgb [1] [0]-tmprgb [2]) * * 2+ (rgb [1] [1]-tmprgb [1]) * * 2+ (rgb [1] [2]-tmprgb [1]) * * 2 if tmp
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.