Python+opencv module to achieve emoticon package into mosaic pictures, image processing road

Keywords: Python OpenCV Pycharm AI image processing

Preface

Today, using the expression packs that you downloaded yesterday, you can use them to put together a wave of mosaic pictures. That's good. Don't talk too much. Let's start happily!

development tool

Python version: 3.6.4

Related modules:

opencv-python module;

numpy module;

tqdm module;

argparse module;

And some of the modules that come with python.

Environment Setup

Install Python and add it to the environment variable so that the relevant modules required for pip installation are available.

Introduction to Principles

The principle is really simple. Read all downloaded emoticon pack pictures first

And adjust the image size to the specified mosaic block size, and finally calculate the color mean of the adjusted picture (RGB separated): Finally, if you are not in a tight time and want to improve python quickly, the most important thing is not to be afraid of hardship, I suggest you can price @762459510, which is really good, many people progress very fast, you need not be afraid of hardship! You can add it and have a look at it~

'''Read all source pictures and calculate the corresponding color average'''
def readSourceImages(sourcepath, blocksize):
    print('Start to read source images')
    sourceimages = []
    avgcolors = []
    for path in tqdm(glob.glob("{}/*.jpg".format(sourcepath))):
        image = cv2.imread(path, cv2.IMREAD_COLOR)
        if image.shape[-1] != 3:
            continue
        image = cv2.resize(image, (blocksize, blocksize))
        avgcolor = np.sum(np.sum(image, axis=0), axis=0) / (blocksize * blocksize)
        sourceimages.append(image)
        avgcolors.append(avgcolor)
    print('Finish reading source images')
    return sourceimages, np.array(avgcolors)

Next, the image is traversed from left to right, from top to bottom (each time the mosaic block size is intercepted), in a similar way:

Then, for each mosaic block, find a picture of the expression pack that is closest to its color and paste it in the corresponding location. The code is as follows:

'''Principal function'''
def main(args):
    targetimage = cv2.imread(args.targetpath)
    outputimage = np.zeros(targetimage.shape, np.uint8)
    sourceimages, avgcolors = readSourceImages(args.sourcepath, args.blocksize)
    print('Start to make photomosaic')
    for i, j in tqdm(product(range(int(targetimage.shape[1]/args.blocksize)), range(int(targetimage.shape[0]/args.blocksize)))):
        block = targetimage[j*args.blocksize: (j+1)*args.blocksize, i*args.blocksize: (i+1)*args.blocksize, :]
        avgcolor = np.sum(np.sum(block, axis=0), axis=0) / (args.blocksize * args.blocksize)
        distances = np.linalg.norm(avgcolor - avgcolors, axis=1)
        idx = np.argmin(distances)
        outputimage[j*args.blocksize: (j+1)*args.blocksize, i*args.blocksize: (i+1)*args.blocksize, :] = sourceimages[idx]
    cv2.imwrite(args.outputpath, outputimage)
    cv2.imshow('result', outputimage)
    print('Finish making photomosaic, result saved in %s' % args.outputpath)

To thank your readers, I would like to share some of my recent collection of programming dries with you, to give back to every reader, and to help you. Finally, if you are not very nervous about time and want to improve python quickly, the most important thing is not to be afraid of hardship. I suggest you can price @762459510, which is really good. Many people progress very fast and need you not be afraid of hardship! You can add it and have a look at it~

The main dry goods are:

Over 2000 Python e-books (both mainstream and classic books should be available)

(2) Python Standard Library (most Chinese version)

3. Project Source (Forty or fifty interesting and classic hands-on projects and sources)

(4) Videos about Python basic introduction, crawling, web development, big data analysis (suitable for white learning)

_Python learning roadmap (say goodbye to immersive learning)

All done~See the introduction of your personal home page for full source code.

Posted by sogno on Thu, 21 Oct 2021 11:26:10 -0700