One blessing becomes more blessing Alipay Jifu network picture capture basic image operation

Image capture and processing

Overview

The annual Wufu gathering activity has started again. Even if it costs only a few dollars at the end, it cannot stop everyone's interest in gathering the five good luck. To collect the five blessings, you need to use AR to scan things with blessing characters. I remember that I used to download all kinds of blessing pictures from the Internet to scan, and it was quite difficult to download, so this year I directly crawled Baidu pictures with "福" , "Fuqi", "fu" as keywords in the picture. Later, I thought that when I was studying images, there were a lot of basic image processing operations, so I did a little manipulation on the image by hand, making one blessing character into multiple blessing characters. The backstage replied "福" to get more than 170 pictures of "Fu". The creation is not easy, just click "I'm watching".

Image capture

Image capture does not use any framework to capture, it is very simple URL request data, analysis data, data persistence. I won't go into details here. If you want to know, you can see dozens of lines of code in my previous tweets to download HD wallpapers in batches.

The implementation code is as follows


# -*- coding:utf-8 -*-
import requests
import os
import re
import time
import random

def getManyPages(keyword, pages):
    params = []
    for i in range(30, 30 * pages + 30, 30):
        params.append({
        })
    url = 'https://image.baidu.com/search/acjson'
    urls = []
    for i in params:
        urls.append(requests.get(url, params=i).json().get('data'))
    return urls
def getpage(key, page):
    new_url = []
    for i in range(0, page * 30 + 30, 30):
        new_url.append({
        })
    url = 'https://image.baidu.com/search/acjson'
    result = []
    for i in new_url:
        randnumber1 = random.randint(0, 3)  # 生成随机数
        time.sleep(randnumber1)  # 按随机数延时
        print(i)
        try:
            result.append(requests.get(url, params=i).json().get('data'))
            print(result)
        except:  # 如果延时之后还是被拒绝
            # print('error\n')
            randnumber2 = random.randint(5, 10)  # 延迟随机时间
            time.sleep(randnumber2)
    return result
def getImg(dataList, localPath, keyword):
    i = 1
    x = 0
    for list in dataList:
        for each in list:
            try:
                if each.get('thumbURL') != None:
                    print('downloading:%s' % each.get('thumbURL'))
                    pic = requests.get(each.get('thumbURL'))
            except requests.exceptions.ConnectionError:
                print('error: This photo cannot be downloaded')
                continue
            dir = 'image/' + keyword + '_' + str(i) + '.jpg'
            fp = open(dir, 'wb')
            fp.write(pic.content)
            fp.close()
            i += 1
def dowmloadPic(html, keyword):
    pic_url = re.findall('"objURL":"(.*?)",', html, re.S)
    i = 1
    print('Can not find key word:' + keyword + 'is downloading...')
    for each in pic_url:
        print('No ' + str(i) + '.jpg is downloading ,URL:' + str(each))
        try:
            pic = requests.get(each, timeout=10)
        except requests.exceptions.ConnectionError:
            print('error: This photo cannot be downloaded')
            continue

        dir = 'image/' + keyword + '_' + str(i) + '.jpg'
        fp = open(dir, 'wb')
        fp.write(pic.content)
        fp.close()
        i += 1
if __name__ == '__main__':
    keyword = '福气'  # 改变keyword就可以得到你要搜索的图片
    dataList = getpage(keyword, 2)  # key word and number of page
    getImg(dataList, '/images', keyword)  # path

Picture processing

In this part, I did the three simplest image processing (actually just called the function), basically completed in one or two sentences. "Gray processing", "pixel inversion", and "picture inversion" were done to the image respectively. code show as below.


import cv2
from matplotlib import pyplot as plt
import numpy as np

imgOgr = cv2.imread('D://python_pycharm//MyPy//image//fu_7.jpg')
# 转换颜色模式,显示原图
img = cv2.cvtColor(imgOgr, cv2.COLOR_BGR2RGB)
# 灰度图
img_gray = cv2.cvtColor(imgOgr, cv2.COLOR_RGB2GRAY)
# 反色
img_array = np.asarray(img)
img_re = 255 - img_array
# 倒
img_r = cv2.flip(img, -1)

plt.subplot(2, 2, 1), plt.imshow(img)
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 2, 2), plt.imshow(img_gray, cmap="gray")
plt.title('Gray'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 2, 3), plt.imshow(img_re)
plt.title('re'), plt.xticks([]), plt.yticks([])
plt.subplot(2, 2, 4), plt.imshow(img_r)
plt.title('r'), plt.xticks([]), plt.yticks([])
plt.show()

Show results

1. Image capture effect

One blessing becomes more blessing Alipay Jifu network picture capture basic image operation

One blessing becomes more blessing Alipay Jifu network picture capture basic image operation

2. Image processing results

One blessing becomes more blessing Alipay Jifu network picture capture basic image operation

Guess you like

Origin blog.51cto.com/15069472/2577343