当前位置: 代码网 > it编程>前端脚本>Python > 基于Python编写图片去水印小工具

基于Python编写图片去水印小工具

2025年05月12日 Python 我要评论
你有没有注意到,某鱼上有不少提供去水印的服务,收取1块钱每张图。最搞笑的是,很多人都没意识到,去水印的技术其实非常简单,尤其是现在ai盛行的时代,好多平台会提供的去水印接口便宜又好用,一张图片只需要几

你有没有注意到,某鱼上有不少提供去水印的服务,收取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'
        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
            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_images, 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.path.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图片去水印内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com