Reprint AI Studio project link https://aistudio.baidu.com/aistudio/projectdetail/3499631?forkThirdPart=1
Compare your heart with photos: on Valentine's day, spell out a love show with stock photos and blow up a circle of friends
Links: [Valentine's day limit] dog food artifact in the circle of friends
The annual Valentine's Day is coming. People with objects must have a lot of commemorative photos in their mobile phones. For a more innovative Sha, Ren and le, simply issuing a Jiugong grid can no longer meet our demands. Don't panic. You can quickly rearrange the photos of dog food in your mobile phone through the following contents, so as to spread dog food in a fancy circle of friends.
Sample display
On the left is the template image (from Baidu image search), and in the middle is 27 × 27 search images of Xinyuan's clothes are similar images, and on the right is the Jiugong grid image divided by similar images. In this way, you can happily show yourself and your objects in the circle of friends~
usage method
Select template pictures (such as gesture of heart comparison), select a batch of pictures (the total number of pictures is required to be greater than the specified number of pictures for fitting, if you want to use 27 × 27 pictures fit the target picture, preferably more than 27 × 27 pictures to get better fitting results), modify the following parameters to their desired values, and then run to get the final results (saved in 9_save folder).
pic_base_num Number of lines in the picture pic_size Size of each small picture templet_dir Template picture imgs_dir Picture folder for fitting output_dir Output picture path name
It doesn't matter if you don't have a girlfriend. The following code contains a crawler function that can automatically obtain a large number of pictures from Baidu pictures according to keywords
If you really don't have so many pictures, you can copy the pictures several times or remove py_ files. del command in myfunctions code
Code introduction
- py_ files. search_ on_ Get Baidu keyword search results according to Baidu
- py_files.myfunctions contains three functions, which have the following functions: centrrop the picture (cut it into a square picture), obtain the difference value of two three channel pictures, and fit the stock picture to the template picture
- py_files.make_9_blocks divides the picture into nine squares
The above code refers to:
https://blog.csdn.net/weixin_42845306/article/details/112789419
https://blog.csdn.net/qq_37124237/article/details/80931894
https://aistudio.baidu.com/aistudio/projectdetail/3472196
Code example
# Get 1000 pictures of Xinyuan clothes, and centrrop all the pictures from py_files.search_on_baidu import * from py_files.myfunctions import * import os # Randomly obtain the required picture and process it into a square search_pic_on_baidu('Xinyuan dressing', 1000, 'pics') for img in os.listdir('pics'): make_center_square('pics/'+img)
Baidu pictures found about 117 related pictures,000 Zhang Error, the current picture cannot be downloaded It takes a long time to complete the processing:525.2224822044373 second
# Get the 'distance' of two pictures from py_files.search_on_baidu import * from py_files.myfunctions import * img_dir1='pics/0.jpg' img_dir2='pics/1.jpg' img1=cv2.imread(img_dir1) img2=cv2.imread(img_dir2) pic_size=64 get_distance_of_2_pics(img1,img2,pic_size=pic_size)
64.51257657604776
# Fitting template images is very time-consuming and almost requires pic_base_num*pic_base_num sec from py_files.myfunctions import * import cv2 import os pic_base_num=9*3 pic_size=64 templet_dir='templet.jpg' imgs_dir='pics' output_dir='result.jpg' imgs2img(pic_base_num,pic_size,templet_dir,imgs_dir,output_dir)
# Divide the picture into 9 pieces for sending a circle of friends~ ! rm -rf 9_save ! mkdir 9_save from py_files.make_9_blocks import * output_dir='result.jpg' image = Image.open(output_dir) image = fill_image(image) image_list = cut_image(image) save_images(image_list,'9_save')