Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate a mosaic in Python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to generate a mosaic painting in Python. The quality of the article is high, so Xiaobian shares it with you for reference. I hope you have a certain understanding of relevant knowledge after reading this article.

import re

import os

import cv2

import numpy as np

from tqdm import tqdm

IMG_DIR = "images"

def load_all_images(tile_row, tile_col):

img_dir = IMG_DIR

filenames = os.listdir(img_dir)

result = []

print(len(filenames))

for filename in tqdm(filenames):

if not re.search(".jpg", filename, re.I):

continue

try:

filepath = os.path.join(img_dir, filename)

im = cv2.imread(filepath)

row = im.shape[0]

col = im.shape[1]

im = resize(im, tile_row, tile_col)

result.append(np.array(im))

except Exception as e:

msg = "error with {} - {}".format(filepath, str(e))

print(msg)

return np.array(result, dtype=np.uint8)

Here, the parameter of load_all_images function is unified size, tile_row and tile_col correspond to height and width respectively.

The following code divides the image to be converted

img = cv2.imread(infile)

tile_row, tile_col = get_tile_row_col(img.shape)

for row in range(0, img_shape[0], tile_row):

for col in range(0, img_shape[1], tile_col):

roi = img[row:row+tile_row,col:col+tile_col,:]

We divide the image we want to convert into squares, tile_row and tile_col are the height and width of the squares, roi access the image data in the squares.

Here is a function that calculates the similarity between two images

from scipy.spatial.distance import euclidean

def img_distance(im1, im2):

if im1.shape != im2.shape:

msg = "shapes are different {} {}".format(im1.shape, im2.shape)

raise Exception(msg)

array1 = im1.flatten()

array2 = im2.flatten()

dist = euclidean(array1, array2)

return dist

im1 and im2 are the data of two pictures. The picture data is a three-dimensional numpy array. Here, we convert the three-dimensional array into a one-dimensional array and compare the Euclidean distance between the two. Then to find the most similar picture, simply traverse all the pictures in the collection, find the picture with the shortest distance, and replace the small square in the original picture.

About how to generate a mosaic painting in Python to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report