代码拉取完成,页面将自动刷新
import json
import time
import cv2
import numpy as np
import requests
from lxml import etree
from PIL import ImageFont, ImageDraw, Image
from PyQt5.QtGui import QImage
from urllib.parse import quote
def getnetPic(url):
res = requests.get(url)
img = QImage.fromData(res.content) #从一块内存中按照指定的格式来构造一个image对象
return img
def getwallheavencc(keyword,page):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
url="https://wallhaven.cc/search?q="+str(keyword)+"&page="+str(page)
imgsection=requests.get(url,headers)
imghtml=etree.HTML(imgsection.text)
imglist=imghtml.xpath("//section[@class='thumb-listing-page']/ul/li/figure/img/@data-src")
smallist=[]
largelist=[]
for imgurl in imglist:
strimgurl=str(imgurl)
smallist.append(strimgurl)
urllist=strimgurl.split('/')
urllist[2]="w.wallhaven.cc"
urllist[3]="full"
urllist[5]="wallhaven-"+urllist[5]
urlstr='/'.join(urllist)
largelist.append(urlstr)
imgdict={"small":smallist,"large":largelist}
return imgdict
def download_img(img_url,fileplace):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
r = requests.get(img_url, headers=headers, stream=True)
if r.status_code == 200:
# 截取图片文件名
img_name = img_url.split('/').pop()
img_name=fileplace+"/"+img_name
with open(img_name, 'wb') as f:
f.write(r.content)
return img_name
def addshuiyin(inputpath,outpath,shuiyin,left,top,R,G,B):
bkimg= cv2.imdecode(np.fromfile(inputpath, dtype=np.uint8), -1)#从文本或二进制文件中的数据构造一个数组。从指定的内存缓存中读取数据,并把数据转换(解码)成图像格式
name=str(inputpath).split("/")[-1]
outpath=outpath+"/"+str(int(time.mktime(time.localtime(time.time()))))+"."+str(name).split(".")[-1]
# cv2.putText(bkimg, shuiyin, (100, 300), cv2.FONT_HERSHEY_SIMPLEX,
# 0.7, (255, 255, 0), 1, cv2.LINE_AA)
if (isinstance(bkimg, np.ndarray)):
# 转化成PIL类型
bkimg = Image.fromarray(cv2.cvtColor(bkimg, cv2.COLOR_BGR2RGB))#颜色空间转换 array转为image
draw = ImageDraw.Draw(bkimg)#imagedraw用来绘图
# 字体的格式
textSize=30
if bkimg.size[0] > bkimg.size[1]:
textSize = int(bkimg.size[1] / 100 * 3)
else:
textSize= int(bkimg.size[0] / 100 * 3)
fontStyle = ImageFont.truetype(
"font/simsun.ttc", textSize, encoding="utf-8")
textColor = (R, G, B)
draw.text((left, top),shuiyin, textColor, font=fontStyle)#绘制文本
# 转换回OpenCV类型
img2 = cv2.cvtColor(np.asarray(bkimg), cv2.COLOR_RGB2BGR)
# 保存图片
cv2.imwrite(outpath, img2)
return outpath
def getpicurllist(theme,page):
# https://www.logosc.cn/api/so/get?page=1&pageSize=20&keywords=%E9%A3%8E%E6%99%AF&category=pixabay&isNeedTranslate=false
url="https://www.logosc.cn/api/so/get?page="+str(page)+"&pageSize=20&keywords="+quote(theme,"utf-8")+"&category=pixabay&isNeedTranslate=false"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
html=requests.get(url,headers=headers)
data1=json.loads(html.text)
imgdata=data1['data']
return imgdata
def Filter_Fudiao(src_img):
# filter=np.array([[-1,0,0],[0,0,0],[0,0,1]])
filter = np.array([[-1, 0], [0, 1]])
row=src_img.shape[0]
col=src_img.shape[1]
new_img=np.zeros([row,col],dtype=np.uint8)
for i in range(row-1):
for j in range(col-1):
new_value = np.sum(src_img[i:i + 2, j:j + 2] * filter) + 128 # point multiply
if new_value > 255:
new_value = 255
elif new_value < 0:
new_value = 0
else:
pass
new_img[i, j]=new_value
return new_img
#返回(高,宽,像素值)
def getimgheight(path):
img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), -1)
return img.shape
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。