用Python写的四等分切割图片的代码教程,一张图片对等切成四份源码

代码是用GPT写的,写的还是很不错的,非常简单。

以下是一个简单的Python脚本,使用PIL库完成你的需求。首先,请确保已经安装了PIL库,如果没有,可以通过以下命令安装:

pip install pillow

要将Python脚本转换为可执行的exe文件,你可以使用pyinstaller库。首先,确保你已经安装了pyinstaller。如果没有,请在命令行中运行以下命令进行安装:

pip install pyinstaller

然后,你可以使用以下代码:

import os
import webbrowser
from tkinter import messagebox, Tk
from PIL import Image

def main():
    # 弹窗提示
    root = Tk()
    root.withdraw()
    messagebox.showinfo("提示", "该资源来源于怪木副业网")

    # 处理图片
    input_folder = os.getcwd()
    output_folder = "怪木副业网"
    supported_formats = (".jpg", ".webp", ".png")

    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    for file in os.listdir(input_folder):
        if file.lower().endswith(supported_formats):
            image = Image.open(file)
            width, height = image.size
            half_width = width // 2
            half_height = height // 2

            image.crop((0, 0, half_width, half_height)).save(os.path.join(output_folder, f"{file}_1.jpg"))
            image.crop((half_width, 0, width, half_height)).save(os.path.join(output_folder, f"{file}_2.jpg"))
            image.crop((0, half_height, half_width, height)).save(os.path.join(output_folder, f"{file}_3.jpg"))
            image.crop((half_width, half_height, width, height)).save(os.path.join(output_folder, f"{file}_4.jpg"))

    # 提示图片切割完成
    messagebox.showinfo("提示", "图片切割完成")

    # 打开网站并退出
    webbrowser.open("http://www.guaimu.com")
    root.destroy()

if __name__ == "__main__":
    main()

关于运行脚本时出现命令窗口的问题,这是因为我们使用了Tkinter库。当使用pyinstaller将脚本转换为exe文件时,你可以使用--noconsole选项来隐藏命令窗口。以下是创建无命令窗口exe文件的命令(请将your_script.py替换为你的Python脚本文件名):

pyinstaller --onefile --noconsole your_script.py

这将在dist文件夹中生成一个无命令窗口的exe文件。希望这些修改能满足你的需求。如有其他问题,请随时提问。

运行这个脚本,它将在启动时弹出一个提示框,然后处理当前目录下的JPG、WebP和PNG格式的图片,将它们二等分切割后保存到名为"怪木副业网"的文件夹中。最后,它将在默认浏览器中打开www.guaimu.com网站。

最终效果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔚蓝de笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值