|
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import tkinter as tk
- from tkinter import filedialog
- import requests
- from urllib3 import encode_multipart_formdata
- from os.path import basename
- from PIL import Image
- def getmidstring(html, start_str, end):
- start = html.find(start_str)
- if start >= 0:
- start += len(start_str)
- end = html.find(end, start)
- if end >= 0:
- return html[start:end].strip()
- def getTextLeft(html, end):
- start = html.find(end)
- if start >= 0:
- return html[0:start].strip()
- root = tk.Tk()
- root.withdraw()
- filetypes = [(
- '图片文件(*.jpg;*.jpeg;*.gif;*.png;*.bmp)', '*.jpg;*.jpeg;*.gif;*.png;*.bmp')]
- file_paths = filedialog.askopenfilenames(filetypes=filetypes)
- for file_path in file_paths:
- print(file_path)
- im = Image.open(file_path)
- print("image/" + im.format.lower())
- with open(file_path, mode="rb") as f:
- file = {
- "flag": "1",
- "pic_path": (basename(file_path), f.read()),
- }
- encode_data = encode_multipart_formdata(file)
- file_data = encode_data[0]
- print(encode_data[1])
- url = "http://pic.sogou.com/ris_upload"
- headers_from_data = {
- "Content-Type": encode_data[1]
- }
- response = requests.post(
- url=url,
- headers=headers_from_data,
- data=file_data,
- allow_redirects=False)
- response_text = response.text
- start = response_text.find("<a href="")
- start = getmidstring(response_text, "<a href="", "">")
- print("相似图片:" + start)
- start = getTextLeft(start, "&oname=")
- start = start + "&flag=1&reqType=ajax&st=12&reqFrom=result"
- start = requests.get(start).json()
- print("上传的图片:" + start['query'])
复制代码
|
|