SoFunction
Updated on 2024-12-13

Python implementation of multiple images to synthesize a mosaic image

preamble

Recently, I had a private message from a user asking how to synthesize multiple images into a mosaic image look

She said that there are about hundreds of photos of her daughter since she was born, so she wanted to use them to synthesize one photo as a birthday gift.

Then we'll use the pictures from the last time we climbed the emoji pack to make a mosaic picture today, 20,000 of them synthesized into one, it's exciting to think about it!

picture material

4K High Definition Original Image

development environment (computer)

Python 3.6

Pycharm

Implementation Code

Import the required modules first

import cv2
import glob
import argparse
import numpy as np
from tqdm import tqdm  # Progress bars
from itertools import product  # Iterator

Reading image files

def parsArgs():
    parser = ('Patchwork mosaic pictures')
    parser.add_argument('--targetpath', type=str, default='examples/', help='Target image path')
    parser.add_argument('--outputpath', type=str, default='', help='Path to the output image')
    parser.add_argument('--sourcepath', type=str, default='sourceimages', help='Paths to all source image files used to stitch the images')
    parser.add_argument('--blocksize', type=int, default=15, help='The size of the mosaic fast')

    args = parser.parse_args()

    return args

Reads all source images and calculates the average of the corresponding colors

def readSourceImages(sourcepath,blocksize):
    print('Start reading image')

List of legal images

Set up a list to hold the color images that match the requirements

sourceimages = []

Average color list

avgcolors = []

(math.) ergodic

The progress bar moves once per traversal.

for path in tqdm(("{}/*.jpg".format(sourcepath))):
    image = (path, cv2.IMREAD_COLOR)
    if [-1] != 3:
        continue
    # Zoom Size
    image = (image, (blocksize, blocksize))
    # Image color averages
    avgcolor = ((image, axis=0), axis=0) / (blocksize * blocksize)
    (image)
    (avgcolor)
print('End reading')
return sourceimages,(avgcolors)

main function

def main(args):
    targetimage = ()
    outputimage = (,np.uint8)  # int8 int16 int32 int64
    sourceimages,avgcolors = readSourceImages(,)
    print('Start production')
    for i, j in tqdm(product(range(int([1]/)), range(int([0]/)))):
        block = targetimage[j * : (j + 1) * , i * : (i + 1) * ,:]
        avgcolor = ((block, axis=0), axis=0) / ( * )
        distances = (avgcolor - avgcolors, axis=1)
        idx = (distances)
        outputimage[j * : (j + 1) * , i * : (i + 1) * , :] = \
        sourceimages[idx]
    (, outputimage)
    ('result', outputimage)
    print('Production complete')

module call execution

if __name__ == '__main__':
    # run
    main(parseArgs())

Full effect

to this article on Python to achieve multiple images to synthesize a mosaic picture of the article is introduced to this, more related Python multiple images to synthesize a mosaic picture content please search my previous posts or continue to browse the following related articles I hope you will support me in the future more!