Python implementation of thousand image imaging: from image crawling to image synthesis

Thousand picture imaging: use N pictures to make a picture.

Implementation principle: first, the image to be imaged is transformed into mosaic image, and then the corresponding color block is replaced by the corresponding color image from the gallery.

Image processing in the Gallery: mark the mixed color of each picture in the gallery to replace the target color block, and record the characteristics of each picture for imaging, so as to increase the imaging quality.

origin

Delaware

Picture parts

Hero League - Weibo

I saw this one a long time ago, and was shocked by him. The picture is composed of nearly a thousand skin pictures of LOL (it's made of ps or a piece of collage, which should not be possible). Just yesterday, I suddenly thought of this thing, and decided to do one, and even started to take action.

I found this article, looked at the composition of the next picture, and decided to get all the skin pictures first! Then we started crawling again!

Operating environment:

Python3.6.5 , pycharm-2018-1-2 , win10

Reptilian thinking

First of all, I went to the official website to find the source of skin pictures. I thought of the skin monopoly area of daoju City, where there are all the pictures we need.

Get the URL of the first picture by locking the picture with F12( https://game.gtimg.cn/images/daoju/app/lol/medium/2-122015-9.jpg)

By analogy, you can get multiple skin URL s. It is found that only the red box in the figure is different

Try to change the number in the red box. When changing the last three digits (122015 -- > 122001), you get the skin of another Nuo hand. You can basically determine the last three digits as the skin number, the first digit as the hero number, and the skin number must be three digits.

See here you may have a question, why not get the URL of the required image directly? Why should we spend a lot of time looking for the law?

Because this flipping website is special, the URL will not change when flipping, so you can't get all skin through common methods.

It's estimated that someone will propose to use selenium library to simulate people's use of browser to get all the pictures, but this will cause the speed of crawling pictures to be greatly reduced, which can only be used as a next step (in this process, I learned about octopus, and found that its principle is similar to selenium, which is to simulate people's control of browser, and the speed is not allowed to be looked at directly, although it can climb nearly 98% of the website). Bloggers can Under the condition of limited force, I chose to find out the rules!

If you have a good way to solve this problem, you can put it forward in the comments, thank you very much!

Next, when we know the rules, how to get the number of each different hero? Under the guidance of other bloggers, it is found that there are all heroes' avatars in the LoL database. Through the slow search of F12, we found this js file!

View its preview, you can get all hero numbers, and test found available! For example, Ashe's first skin number should be 22001, so the URL is https://game.gtimg.cn/images/daoju/app/lol/medium/2-22001-9.jpg,

The test found that it did succeed!

Well, that's the end of web page analysis. Finally, you can write code.

Code framework

1. Get the hero number and skin number (Note: the skin number does not find the number of each hero skin, so it is set to find all 001 to 015 pictures, of course, you can also find more 020)

2. Import number into picture URL( https://game.gtimg.cn/images/daoju/app/lol/medium/2-****-9.jpg )In, generate Url_list.

3. Download the corresponding image according to the URL and save it locally.

Full code

import requests
import re
import os
# # # # # # # # # # # # # #
# title:obtain LOL Hero skin image  #
# author:A brief book Wayne_Dream  #
# date:2018-7-5           #
# # # # # # # # # # # # # #

def getHero_data():
    try:
        headers = {
            'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
        }
        url = 'http://lol.qq.com/biz/hero/champion.js'
        r = requests.get(url, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        text = r.text
        hero_id = re.findall(r'"id":"(.*?)","key"', text)
        hero_num = re.findall(r'"key":"(.*?)"', text)
        return hero_id, hero_num
    except:
        return 'Lying groove, failed to get hero code!'

def getUrl(hero_num):
    part1 = 'https://game.gtimg.cn/images/daoju/app/lol/medium/2-'
    part3 = '-9.jpg'
    skin_num = []
    Url_list = []
    for i in range(1, 21):
        i = str(i)
        if len(i) == 1:
            i = '00'+i
        elif len(i) == 2:
            i = '0'+i
        else:
            continue
        skin_num.append(i)
    for hn in hero_num:
        for sn in skin_num:
            part2 = hn + sn
            url = part1 + part2 + part3
            Url_list.append(url)
    print('picture URL Success')
    return Url_list

def PicName(hero_id, path):
    pic_name_list = []
    for id in hero_id:
        for i in range(1, 21):
            pic_name = path + id + str(i) + '.jpg'
            pic_name_list.append(pic_name)
    return pic_name_list

def DownloadPic(pic_name_list, Url_list):
    count = 0
    n = len(Url_list)
    try:
        for i in range(n):
            headers = {
                'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
            }
            res = requests.get(Url_list[i], headers=headers).content
            if len(res) < 100:
                count += 1
                print('\r Current progress:{:.2f}%'.format(100*(count/n)), end='')
            else:
                with open(pic_name_list[i], "wb") as f:
                    f.write(res)
                    count += 1
                    print('\r Current progress:{:.2f}%'.format(100*(count/n)), end='')
    except:
        return 'Horizontal groove, failed to get the picture!'

if __name__ == '__main__':
    print('author:A brief book Wayne_Dream:')
    print('https://www.jianshu.com/u/6dd4484b4741')
    input('Please enter any character to start crawler:')
    if os.path.exists('D:\LOLimg_wayne\\') == False:
        path = r'D:\LOLimg_wayne\\'
        os.mkdir(path)
        hero_id, hero_num = getHero_data()
        Url_list = getUrl(hero_num)
        pic_name_list = PicName(hero_id, path)
        print('Downloading picture, please wait...')
        print('stay' + path + 'View below...')
        DownloadPic(pic_name_list, Url_list)
        print('Image download completed')
    else:
        path = r'D:\LOLimg_wayne\\'
        hero_id, hero_num = getHero_data()
        Url_list = getUrl(hero_num)
        pic_name_list = PicName(hero_id, path)
        print('Downloading picture, please wait...')
        print('stay' + path + 'View below...')
        DownloadPic(pic_name_list, Url_list)
        print('Image download completed')

Well, here we have completed the acquisition of the whole skin of LOL, and then we will take the most interesting step, thousand map imaging!

In the initial stage, we first use a foreign synthetic software

Software download address:

https://fmedda.com/en/download

If you can't open it, search "foto mosaic Edda" to download it!

After opening, the interface is like this.

First step

Step 2 create photo mosaic

design sketch

local

If you want to have a direct try at the Qiantu imaging, you can download the skin atlas I crawled to on Baidu online: https://pan.baidu.com/s/19PywbOV1dBwr7r7bzsMhmw Password: 5arb

Original link: https://www.jianshu.com/p/c963370cd8df

Source network, for learning purposes only, invasion and deletion.

Don't panic. I have a set of learning materials, including 40 + E-books, 800 + teaching videos, involving Python foundation, reptile, framework, data analysis, machine learning, etc. I'm not afraid you won't learn! https://shimo.im/docs/JWCghr8prjCVCxxK/ Python learning materials

Pay attention to the official account [Python circle].

file

Tags: Programming Python Selenium Windows Pycharm

Posted on Tue, 19 May 2020 06:11:24 -0400 by fimbria