Python Script for Merging Avatar Images into a Composite Photo Using PIL and OpenCV
This article demonstrates how to download QQ friend avatars, compute their average RGB values, and programmatically combine them into a large collage or a template‑based composite image using Python's Pillow and OpenCV libraries, with complete source code and explanations.
The article presents a Python solution for two image‑processing tasks: (1) merging multiple avatar pictures into a single large image, and (2) arranging avatars according to a template image by matching average RGB colors.
Required libraries are imported at the beginning, including requests, codecs, re, urllib, os, random, math, PIL.Image, numpy (as np), and cv2 (as cv).
Simple collage function ( simple_split) creates a new RGBA image sized by the number of rows and columns, randomly selects avatar files, resizes each to a specified small size, and pastes them onto the canvas using Image.paste. The result is saved to resultSavePath.
def simple_split(filepackage,size,littlesize):
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,friends_count)
img = Image.open(filepackage+str(randpic)+'.png').convert('RGBA')
img = img.resize((littlesize,littlesize))
loc = (i*littlesize,j*littlesize,(i+1)*littlesize,(j+1)*littlesize)
print(loc,number)
number+=1
bigimg.paste(img,loc)
bigimg.save(resultSavePath)Template‑based collage works by dividing a template image into an A×B grid, computing the average RGB of each cell, and selecting the avatar whose average RGB is closest to that cell. Functions meanrbg, gettouxiang, savaImg, mostSuitImg, and mode_split implement these steps.
def meanrbg(img):
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 (r,g,b)The script also includes utilities for downloading avatars from QQ URLs, handling download errors, and storing images with numeric filenames for easy reference.
In the __main__ block, the code optionally calls gettouxiang to fetch avatars, runs simple_split for a basic collage, computes average RGBs for all avatars, and finally executes mode_split to produce the template‑based result, saving the final composite image.
Several screenshots illustrate the intermediate and final results, confirming that the generated collages resemble the intended templates, and the author notes that increasing the number of avatars or the template size improves visual fidelity.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
