import os
# 设置上传的图片路径
image_path = 'C:/Users/username/Desktop/images/'
# 设置图片上传的URL
url = 'https://imgse.com/api/upload/multi'
# 定义上传图片的函数
def upload_image(image_file):
# 打开图片文件
with open(image_file, 'rb') as f:
# 构建POST请求参数
files = {'file': ('image.jpg', f, 'image/jpeg')}
# 发送POST请求
response = requests.post(url, files=files)
# 获取返回的JSON数据
data = response.json()
# 获取图片链接
image_url = data['data'][0]['url']
# 返回图片链接
return image_url
# 遍历指定路径下的所有图片文件
for filename in os.listdir(image_path):
if filename.endswith('.jpg') or filename.endswith('.png') or filename.endswith('.jpeg'):
# 构建图片文件的完整路径
image_file = os.path.join(image_path, filename)
# 调用上传图片的函数
image_url = upload_image(image_file)
# 输出图片链接
print(image_url)
有GUI版
import tkinter as tk
import requests
import os
class ImageUploader:
def __init__(self):
self.window = tk.Tk()
self.window.title('Image Uploader')
# 设置上传的图片路径
self.image_path = tk.StringVar()
# 设置图片上传的URL
self.url = 'https://imgse.com/api/upload/multi'
# 创建窗口控件
self.create_widgets()
def create_widgets(self):
# 创建选择图片路径的Label和Entry
tk.Label(self.window, text='Image Path:').grid(row=0, column=0)
tk.Entry(self.window, textvariable=self.image_path).grid(row=0, column=1)
tk.Button(self.window, text='Browse', command=self.browse_image).grid(row=0, column=2)
# 创建上传图片的Button和输出结果的Text
tk.Button(self.window, text='Upload', command=self.upload_image).grid(row=1, column=0)
self.result_text = tk.Text(self.window, height=10, width=50)
self.result_text.grid(row=2, column=0, columnspan=3)
def browse_image(self):
# 弹出文件选择对话框
filename = tk.filedialog.askopenfilename()
# 将选择的文件路径设置到Entry中
self.image_path.set(filename)
def upload_image(self):
# 获取图片路径
image_path = self.image_path.get()
# 判断是否选择了图片文件
if not image_path:
tk.messagebox.showerror('Error', 'Please select an image file.')
return
# 判断所选择的文件是否为图片文件
if not image_path.endswith('.jpg') and not image_path.endswith('.png') and not image_path.endswith('.jpeg'):
tk.messagebox.showerror('Error', 'Please select a valid image file.')
return
# 判断图片文件是否存在
if not os.path.exists(image_path):
tk.messagebox.showerror('Error', 'The selected image file does not exist.')
return
# 打开图片文件
with open(image_path, 'rb') as f:
# 构建POST请求参数
files = {'file': ('image.jpg', f, 'image/jpeg')}
# 发送POST请求
response = requests.post(self.url, files=files)
# 获取返回的JSON数据
data = response.json()
# 获取图片链接
image_url = data['data'][0]['url']
# 在输出结果的Text中显示图片链接
self.result_text.insert('end', image_url + '\n')
def run(self):
self.window.mainloop()
if __name__ == '__main__':
uploader = ImageUploader()
uploader.run()