基于Python编写图片去水印小工具
目录
- 把它做成一个GUI工具
- 1. 安装必要的库
- 2. 编写GUI代码
- 3. 编写GUI代码
- 使用教程
- 总结
你有没有注意到,某鱼上有不少提供去水印的服务,收取1块钱每张图。最搞笑的是,很多人都没意识到,去水印的技术其实非常简单,尤其是现在AI盛行的时代,好多平台会提供的去水印接口便宜又好用,一张图片只需要几分钱就能搞定,比某鱼的1块钱便宜不知道倍!
为什么说是暴利呢?因为花姐发现https://www.textin.com/
上面有个图像水印去除的接口,调用1次只需要0.025!!!妥妥的只赚不亏呀!
我们在看看去水印的效果,还是很厉害的,反正让我PS绝对没人家做的这么好,而且图片也没压缩:
官方还贴心的给出了接口调用示例:
接下来我们只需要套个GUI外壳,方便我们批量处理图片即可。
把它做成一个GUI工具
官网虽然给出了调用示例,但是为了提高操作便捷性,我们可以做一个图形化界面(GUI)。别担心,GUI不难,我这就来带你做一个简单的Windows桌面应用,用Tkinter这个python内建的库。
1. 安装必要的库
pip install requests pillow
2. 编写GUI代码
import os import tkinter as tk from tkinter import filedialog, messagebox import json import requests import base64 from PIL import Image from io import BytesIO def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() # 之前的水印去除API调用部分 class CommonOcr(object): def __init__(self, img_path=None, is_url=False): # 图像水印去除 self._url = 'https://api.textin.com/ai/service/v1/image/watermark_remove' # 请登录后前往 “工作台-账号设置-开发者信息” 查看 x-ti-app-id # 示例代码中 x-ti-app-id 非真实数据 self._app_id = '73b************************d269' # 请登录后前往 “工作台-账号设置-开发者信息” 查看 x-ti-secret-code # 示例代码中 x-ti-secret-code 非真实数据 self._secret_code = 'de1ac7*******************48993131' javascript self._img_path = img_path self._is_url = is_url def recognize(self): head = {} try: head['x-ti-app-id'] = self._app_id head['x-ti-secret-code'] = self._secret_code if self._is_url: head['Content-Type'] = 'text/plain' body = self._img_path www.devze.com else: image = get_file_content(self._img_path) head['Content-Type'] = 'application/octet-stream' body = image result = requests.post(self._url, data=body, headers=head) return result.text except Exception as e: return e def down_img(base64str,output_folder,img_name): try: img_data = base64.b64decode(base64str) img = Image.open(BytesIO(img_data)) file_name = os.path.join(output_folder,img_name) img.save(file_name) # with open(output_folder, 'wb') as f: # f.write(image_data) print(f"去水印图片已经保存到 {file_name}") except Exception as e: print(f"图片去水印失败: {e}") # GUI界面部分 class WatermarkRemoverApp: def __init__(self, root): self.root = root self.root.title("去水印工具") self.root.geometry("400x200") self.select_button = tk.Button(root, text="选择文件夹", command=self.select_folder) self.select_button.pack(pady=20) self.process_button = tk.Button(root, text编程客栈="开始去水印", command=self.process_javascriptimages, state=tk.DISABLED) self.process_button.pack(pady=20) def select_folder(self): self.folder_path = filedialog.askdirectory() if self.folder_path: self.process_button.config(state=tk.NORMAL) def process_images(self): output_folder = os.path.join(self.folder_path, "output") os.makedirs(output_folder, exist_ok=True) for file_name in os.listdir(self.folder_path): if file_name.endswith((".jpg",".jpeg", ".png",".bmp")): file_path = os.p编程ath.join(self.folder_path, file_name) response = CommonOcr(img_path=file_path).recognize() # 解析JSON字符串为Python字典 data = json.loads(response) if data["code"] ==200: # 假设接口返回包含"success"表示成功 down_img(data["result"]["image"],output_folder,file_name) else: messagebox.showerror("错误", f"图片 {file_name} 去水印失败!{data['msg']}") messagebox.showinfo("完成", "所有图片已处理完毕!") if __name__ == "__main__": root = tk.Tk() app = WatermarkRemoverApp(root) root.mainloop()
3. 编写GUI代码
运行起来是这个样子的
使用教程
- 选择文件夹:点击“选择文件夹”按钮,选择你要处理的图片文件夹。
- 开始去水印:点击“开始去水印”,程序会自动遍历文件夹中的所有图片(支持JPG、PNG等格式),然后通过调用textin接口去除水印。
- 输出文件:去水印后的图片会保存在当前文件夹中的
output
目录下。
如何赚钱?
现在你可以用这个工具来给图片去水印了,接下来就是去某鱼发布去水印的服务。比如,你可以定个价格:每张图片1元,批量去水印,随便做个广告推送,搞定!很多人会觉得手动去水印很麻烦,尤其是做电商的商家,他们更愿意支付小额费用来节省时间。
总结
这篇教程给大家展示了如何利用Python简单地实现一个去水印工具,并且利用textin的接口去水印服务赚取差价。通过GUI界面,你甚至可以让这个工具变得更方便易用,适合没有编程基础的人群。你可以将这款工具用作副业,快速投入市场,甚至可以做得越来越大,开始接更多客户,走上暴富之路(咳咳,开玩笑的)。
到此这篇关于基于Python编写图片去水印小工具的文章就介绍到这了,更多相关Python图片去水印内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论