3 Star 1 Fork 0

luotian2020/晨博图片探索助手

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
picget.py 4.23 KB
一键复制 编辑 原始数据 按行查看 历史
luotian2020 提交于 2022-05-29 13:14 . finish
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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/luotian2020/chenbopic.git
git@gitee.com:luotian2020/chenbopic.git
luotian2020
chenbopic
晨博图片探索助手
master

搜索帮助