下面的python脚本有一部分是我百度然后修改的,一部分是我自己写的,如果以后有什么新的我用的到的脚本或者这些脚本有修改我也会在这里更新,这些源码放在这里仅以备份为目的
sm.ms.py
# coding=utf-8
from time import sleep
import requests, os
url = "https://sm.ms/api/v2/upload"
headers = {
'Connection': 'close',
'Authorization': 'key' #此处填写sm.ms图床的token
}
files_path = r'C:\Users\Administrator\Desktop\pic'
ufile = r'C:\Users\Administrator\Desktop\url.txt'
hfile = r'C:\Users\Administrator\Desktop\hash.txt'
dfile = r'C:\Users\Administrator\Desktop\done.txt'
while True:
try:
pfiles = os.listdir(files_path) #遍历文件
for file in pfiles:
fpath = os.path.join(files_path, file)
print('开始上传' + fpath)
pic = open(fpath, 'rb')
files = {
'smfile': pic #读取文件
}
r = requests.post(url, headers=headers, files=files, timeout=60) #上传
print(r.status_code)
pic.close()
r.encoding = 'utf-8'
print(r.json())
t = fpath + "_inurl_" + r.json()['data']['url'] + "\n" #文件名+url
pichash = r.json()['data']['hash']
with open(ufile,"a") as f: #写入文档
f.write(t)
f.close()
print(t)
with open(hfile,"a") as f:
f.write(pichash + "\n")
f.close()
print("图片哈希为" + pichash)
with open(dfile,"a") as f:
f.write(file + "\n")
f.close()
print("图片信息保存完成" + "\n")
sleep(5)
except:
print("程序出错了,开始删除本地已上传的文件" + "\n")
with open(dfile, "r+") as f:
for fname in f:
fpath = os.path.join(files_path, fname)
fpath = fpath[:-1]
os.remove(fpath)
print("已删除" + fpath)
f.truncate(0)
f.close()
print("文件删除完成,900秒后重试上传" + "\n")
sleep(900)
picdl.py
# coding=utf-8
import requests
filepath = 'E:/acg/acg/' #图片存放的地址
i = 1
#将图片URL存放在 E:\acg\acgurl.txt 中
with open(r'E:\acg\acgurl.txt','r',encoding= 'UTF-8')as f:
for url in f:
r = requests.request('get',url) #get请求
print(r.status_code)
with open(filepath+str('acgpic')+str(i)+'.jpg','wb') as p: #打开写入到path路径里的二进制文件
p.write(r.content) #写入r对象的二进制文件
p.close()
i += 1
quchong.py
# coding=utf-8
import os
import hashlib
filedir = 'E:\\acg\\acg'
def filecount(DIR):
filecount = len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])
return (filecount)
def md5sum(filename):
f = open(filedir+'/'+filename, 'rb')
md5 = hashlib.md5()
while True:
fb = f.read(8096)
if not fb:
break
md5.update(fb)
f.close()
return (md5.hexdigest())
def delfile():
all_md5 = {}
dir =os.walk(filedir)
for i in dir:
for tlie in i[2]:
if md5sum(tlie) in all_md5.values():
os.remove(filedir+'/'+tlie)
print(tlie)
else:
all_md5[tlie] = md5sum(tlie)
if __name__ == '__main__':
oldf = filecount(filedir)
print('去重前有', oldf, '个文件\n请稍等正在删除重复文件...')
delfile()
print('\n\n去重后剩', filecount(filedir), '个文件')
print('\n\n一共删除了', oldf - filecount(filedir), '个文件\n\n')
size.py
# coding=utf-8
import os
import shutil
from PIL import Image
files_path = r'E:\acg\acg'
#定义一个分类函数,函数括号中为需要分类的图片文件夹路径
def photo_classify(files_path):
# 读取文件夹中所有文件的名称
files_list = os.listdir(files_path)
# 循环文件夹中的所有文件
for photo in files_list:
# 获取图片的绝对路径
photo_abspath = os.path.join(files_path, photo)
print(photo_abspath)
# 判断photo是不是文件夹,若是文件夹则跳过,若不是文件夹则继续执行
if os.path.isdir(photo_abspath):
continue
else:
print(photo_abspath)
# 读图片的尺寸
img = Image.open(photo_abspath)
weight = img.size[0]
high = img.size[1]
print(weight)
print(high)
# 判断新文件夹是否存在,若不存在,则新建文件夹,若存在,则将图片复制到文件夹中
# 先定义新文件的路径,名字是new_path
name_path = str(weight) + '-' + str(high)
print(name_path)
new_path = os.path.join(files_path, name_path)
print(new_path)
# 查询新文件路径下面是否是文件夹
if os.path.isdir(new_path):
# 将图片复制到新闻家当中
shutil.copyfile(photo_abspath, os.path.join(new_path, photo))
# shutil.copyfile(photo_abspath, new_path)
else:
os.mkdir(new_path)
shutil.copyfile(photo_abspath, os.path.join(new_path, photo))
# shutil.copyfile(photo_abspath, new_path)
print('分类成功!')
if __name__ == '__main__':
photo_classify(files_path)
size4.py
# coding=utf-8
import os
import shutil
from PIL import Image
files_path = r'E:\pic\1'
suffixlist = ['.Webp', '.BMP', '.JPEG', '.RAW', '.JPG', '.PNG', '.webp', '.bmp', '.jpeg', '.raw', '.jpg', '.png',]
#定义一个分类函数,函数括号中为需要分类的图片文件夹路径
def photo_classify(files_path):
# 读取文件夹中所有文件的名称
files_list = os.listdir(files_path)
# 循环文件夹中的所有文件
for photo in files_list:
# 获取图片的绝对路径
photo_abspath = os.path.join(files_path, photo)
# 判断photo是不是文件夹,若是文件夹则跳过,若不是文件夹则继续执行
if os.path.isdir(photo_abspath):
print(photo_abspath + '是文件夹')
continue
elif os.path.splitext(photo_abspath)[-1] not in suffixlist:
print(photo_abspath + "的文件后缀不是'.Webp', '.BMP', '.JPEG', '.RAW', '.JPG', '.PNG', '.bmp', '.jpeg', '.raw', '.jpg', '.png',中的一个")
else:
print('处理' + photo_abspath)
# 读图片的尺寸
img = Image.open(photo_abspath)
weight = img.size[0]
high = img.size[1]
ratio = float(weight) / float(high)
print('宽' + str(weight) + '高' + str(high) + '比例' + str(ratio))
if 0.45 <= ratio <= 0.72:
#if weight < high:
picKind = 'pe'
elif 1.50 <= ratio <= 2.30:
picKind = 'pc'
elif weight == high:
picKind = 'pp'
else:
picKind = 'other'
new_path = os.path.join(files_path, picKind)
print(new_path)
# 查询新文件路径下面是否是文件夹
if os.path.isdir(new_path):
# 将图片复制到新文件夹当中
shutil.copyfile(photo_abspath, os.path.join(new_path, photo))
# shutil.copyfile(photo_abspath, new_path)
else:
os.mkdir(new_path)
shutil.copyfile(photo_abspath, os.path.join(new_path, photo))
# shutil.copyfile(photo_abspath, new_path)
print('分类成功!')
if __name__ == '__main__':
photo_classify(files_path)
json_into_txt.py
# coding=utf-8
import requests, json
tfile = "E:/acg/acgurl.txt"
i = 1
url = 'https://www.loliapi.com/acg?type=json'
while i < 100:
r = requests.request('get',url) #获取网页
imgjson = r.text
text = json.loads(imgjson)
t = text['imgurl'] + "\n"
with open(tfile,"a") as f:
f.write(t)
f.close()
print(t)
i += 1
# coding=utf-8
import requests, json
from bs4 import BeautifulSoup
tfile = "E:/acg/picurl.txt"
url = ' '
#while True:
r = requests.request('get',url) #获取网页
soup = BeautifulSoup(r.text, "lxml")
for a in soup.find_all('a'):
if a.img:
src = a.img['src']
with open(tfile,"a") as f:
f.write(src)
f.close()
print(src)
rename.py
# coding=utf-8
import os
files_path = r'E:\acg\acg' #文件位置
num = 1
newname = "img"
files = os.listdir(files_path)
for filename in files:
portion = os.path.splitext(filename)
nname = newname + str(num) + portion[1]
os.chdir(files_path)
os.rename(filename,nname)
print("文件" + filename + "已修改为" + nname)
num = num + 1
resuffix.py
# coding=utf-8
import os
files_path = r'E:\acg\acg' #文件位置
originalsuffix = "all" #要修改的后缀名,如果要修改所有文件则填'all'
suffix = "png" #目标后缀名
files = os.listdir(files_path)
#files.sort(key=lambda x:int(x[:-4]))
for filename in files:
portion = os.path.splitext(filename)
if originalsuffix == "all" or portion[1] == originalsuffix:
# 重新组合文件名和后缀名
newname = portion[0] + "." + suffix
os.chdir(files_path)
os.rename(filename,newname)
print("文件" + filename + "已修改为" + newname)
pic2webp.py
# coding=utf-8
import os
from PIL import Image
files_path = r'E:\acg\acg' #文件位置
suffix = "webp" #目标格式(后缀名)
del_original_img = "true" #是否删除原图片
for dpath, dname, dfiles in os.walk(files_path, topdown=False):
for fname in dfiles:
imgpath = os.path.join(dpath, fname)
portion = os.path.splitext(fname)
suffixfname = os.path.join(dpath, portion[0] + "." + suffix)
im = Image.open(imgpath)
if suffix == "jpg" or suffix == "jpeg":
im = im.convert("RGB")
im.save(suffixfname)
print(imgpath + "成功转换为" + suffixfname)
if del_original_img == "true":
if imgpath == suffixfname:
print("目标文件" + imgpath + "已是" + suffix + "格式,为防止误删,跳过此文件")
else:
os.remove(imgpath)
print("已删除源文件" + imgpath)
腾云先锋(TDP,Tencent Cloud Developer Pioneer)是腾讯云 GTS 官方组建并运营的技术开发者群体。这里有最专业的开发者&客户,能与产品人员亲密接触,专有的问题&需求反馈渠道,有一群志同道合的兄弟姐妹。来加入属于我们开发者的社群吧 。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有