taskManager / task /_1批量顺序上传原始图片到服务器manga.py
rogerxavier's picture
Upload 8 files
7d4961d verified
raw
history blame
1.86 kB
#批量上传
import os
import requests
# 补零函数,将数字部分补齐为指定长度,从而顺序上传
def zero_pad(s, length):
return s.zfill(length)
# ->void
def upload_manga_to_space(baseUrl:str,manga_path:str):
# 先删除原来的后上传
url = baseUrl +"/deleteFiles"
directory_clear_list = ["manga", "manga1", "manga12", "output", "mp3_out", "mp4_out", "cover", "cache"]
for directory in directory_clear_list:
response = requests.delete(url, params={"directory": directory})
if response.status_code == 200:
print(response.text)
else:
print("请求失败,状态码:", response.status_code)
print("请求失败,状态码:", response.text)
url = baseUrl+'/getOriginalMangaList'
# 获取当前目录的下的全部图片用于上传
img_path = manga_path
subdir_path = os.path.join(os.getcwd(), img_path)
image_files = []
for root, dirs, files in os.walk(subdir_path):
for file in files:
if file.endswith(".jpg") or file.endswith(".png"):
image_files.append(os.path.relpath(os.path.join(root, file)))
# 对对话框文件名中的数字部分进行补零操作-这样顺序会正常 #排序上传列表中的顺序
image_files.sort(
key=lambda x: zero_pad(''.join(filter(str.isdigit, os.path.splitext(os.path.basename(x))[0])), 3))
# 转换为上传格式并上传
upload_files = []
for image_path in image_files:
upload_files.append(("images", (image_path, open(image_path, "rb"), "image/jpeg")))
response = requests.post(url, files=upload_files)
print(response.text)
if __name__ == '__main__':
upload_manga_to_space(baseUrl='https://rogerxavier-moviepy-with-manga-test.hf.space',manga_path='manga')