热搜: | 激活| 时序| mos|
快捷导航 发布入口
扫描二维码关注官方微信号

一款由AI制作的微信朋友圈九宫格图软件

[复制链接]
查看: 74|回复: 0

630

主题

198

回帖

1715

积分

管理员

积分
1715
QQ
发表于 昨天 17:46 | 显示全部楼层 |阅读模式 来自 湖南省 移动
作者声明:该内容包含AI创作,请谨慎辨别。

一个把一张图分割成9张图的软件
1.使用方法很简单,直接把图片放到这个软件文件夹里面,双击运行软件
文件夹会多出个九宫格文件夹,直接把1-9发朋友圈

代码如下:
  1. import tkinter as tk
  2. from tkinter import filedialog, messagebox
  3. from PIL import Image
  4. import os
  5. import sys

  6. def resource_path(relative_path):
  7.     try:
  8.         base_path = sys._MEIPASS
  9.     except Exception:
  10.         base_path = os.path.abspath(".")
  11.     return os.path.join(base_path, relative_path)

  12. def cut_image():
  13.     path = filedialog.askopenfilename(
  14.         filetypes=[("图片", "*.jpg;*.jpeg;*.png;*.bmp")]
  15.     )
  16.     if not path:
  17.         return

  18.     try:
  19.         img = Image.open(path).convert("RGB")
  20.         w, h = img.size

  21.         margin_w = int(w * 0.12)
  22.         margin_h = int(h * 0.12)
  23.         new_w = w + 2 * margin_w
  24.         new_h = h + 2 * margin_h
  25.         bg = Image.new("RGB", (new_w, new_h), "white")
  26.         bg.paste(img, (margin_w, margin_h))

  27.         pw = new_w // 3
  28.         ph = new_h // 3

  29.         out_dir = os.path.join(os.path.dirname(path), "九宫格_成品")
  30.         os.makedirs(out_dir, exist_ok=True)

  31.         num = 1
  32.         for row in range(3):
  33.             for col in range(3):
  34.                 x1 = col * pw
  35.                 y1 = row * ph
  36.                 x2 = x1 + pw
  37.                 y2 = y1 + ph
  38.                 part = bg.crop((x1, y1, x2, y2))
  39.                 part.save(os.path.join(out_dir, f"{num}.jpg"), quality=95)
  40.                 num += 1

  41.         messagebox.showinfo("完成", f"已生成九宫格!\n保存在:\n{out_dir}")
  42.     except Exception as e:
  43.         messagebox.showerror("错误", str(e))

  44. # GUI 主程序 + 窗口图标
  45. root = tk.Tk()
  46. root.title("朋友圈九宫格切图")
  47. root.geometry("400x220")
  48. root.resizable(False, False)

  49. # 加载 favicon.ico
  50. try:
  51.     icon_path = resource_path("favicon.ico")
  52.     root.iconbitmap(icon_path)
  53. except:
  54.     pass

  55. tk.Label(root, text="九宫格切图工具", font=("微软雅黑", 16)).pack(pady=20)
  56. tk.Label(root, text="自动留白 + 不裁切 + 1~9顺序", fg="gray").pack()
  57. tk.Button(root, text="选择图片并生成", font=("微软雅黑", 12), bg="#409eff", fg="white", width=20, height=2, command=cut_image).pack(pady=30)

  58. root.mainloop()
复制代码

源代码:

附件下载地址:
   





  • 以下附件需要回复 (后刷新页面才能下载) 1 次可见,已回复 0
  • 1、grid.py 2KB 已下载 4 次
温馨提示:本站无需登入,即可回复帖子,发帖和回复请勿涉及违法等行为!网罗天下电脑(wltxdn.cn)
回复

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

精彩推荐

网罗天下让分享更简单

  • 反馈建议:admin@wltxdn.com
  • 工作时间:周一到周日 09:00-21:00
185-7316-8656

关注我们

Copyright   ©2018-2026  wltxdn Inc.  Powered by©Wltxdn  技术支持:网罗天下电脑    ( 湘ICP备2021015364号 ) 劰载中...