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

一款专门解决Windows打印没有6张8张布局脚本(Windows 自带打印图片布局里面)

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

635

主题

199

回帖

1721

积分

管理员

积分
1721
QQ
发表于 8 小时前 | 显示全部楼层 |阅读模式 来自 湖南省 移动
作者声明:该内容包含AI创作,请谨慎辨别。
一款专门解决Windows打印没有6张8张布局脚本(Windows 自带打印图片布局里面)
Windows 自带的图片打印界面没有内置 6 张(2×3 或 3×2)的布局选项,
只提供 1、2、4、9 张等预设。这是系统设计限制
所以需要本脚本
使用教程:直接把需要打印排版的图片,放到桌面“”新建文件夹“(没有可以自己新建一个)”
然后双击脚本或者exe软件
弹出如下,自己可以选着需要的布局;直接回车,也可以直接按回车默认6张布局。


代码如下:
  1. import os
  2. from PIL import Image

  3. # ===================== 基础打印配置 =====================
  4. # A4 300DPI 标准尺寸
  5. A4_W = 2480
  6. A4_H = 3508
  7. DPI = 300
  8. CM2PX = DPI / 2.54
  9. MM2PX = CM2PX / 10

  10. # 四周统一固定 2.5mm 小白边(永久生效)
  11. SMALL_MM = 2.5
  12. EDGE_MARGIN = int(SMALL_MM * MM2PX)

  13. # 3厘米大留白
  14. MARGIN_3CM = int(3 * CM2PX)

  15. # 图片间隙
  16. GAP = 10
  17. BG_COLOR = (255, 255, 255)

  18. # 路径配置
  19. desktop = os.path.join(os.path.expanduser("~"), "Desktop")
  20. SRC_FOLDER = os.path.join(desktop, "新建文件夹")
  21. OUT_FOLDER = os.path.join(desktop, "A4批量排版输出")

  22. # 全部排版规格
  23. LAYOUT_INFO = {
  24.     2:  (1, 2),
  25.     4:  (2, 2),
  26.     6:  (2, 3),
  27.     8:  (2, 4)
  28. }
  29. # ========================================================

  30. def select_need_layout():
  31.     """自选排版,直接回车默认6张"""
  32.     print("=" * 55)
  33.     print("请选择需要生成的排版(默认直接回车 = 只生成6张)")
  34.     print("1 = 每页 2 张")
  35.     print("2 = 每页 4 张")
  36.     print("3 = 每页 6 张【默认】")
  37.     print("4 = 每页 8 张")
  38.     print("多选用逗号隔开,例如:1,3")
  39.     print("=" * 55)
  40.     inp = input("请输入选择:").strip()
  41.     # 直接回车 默认6张
  42.     if not inp:
  43.         return [6]
  44.     selected = []
  45.     for s in inp.split(","):
  46.         s = s.strip()
  47.         if s == "1":
  48.             selected.append(2)
  49.         elif s == "2":
  50.             selected.append(4)
  51.         elif s == "3":
  52.             selected.append(6)
  53.         elif s == "4":
  54.             selected.append(8)
  55.     if not selected:
  56.         return [6]
  57.     return selected

  58. def choose_margin_mode():
  59.     """留白选择,直接回车默认四周2.5mm无大留白"""
  60.     print("-" * 55)
  61.     print("请选择额外大留白(直接回车 = 默认四周2.5mm小边距)")
  62.     print("1 → 额外上方留白 3cm")
  63.     print("2 → 额外下方留白 3cm")
  64.     print("3 → 仅四周2.5mm小边距【默认】")
  65.     print("-" * 55)
  66.     m = input("输入序号 1/2/3:").strip()
  67.     if m == "1":
  68.         return 1
  69.     elif m == "3" or not m:
  70.         return 3
  71.     else:
  72.         return 2

  73. def get_image_files():
  74.     """读取桌面 新建文件夹 内所有图片"""
  75.     suffix = (".jpg", ".jpeg", ".png", ".bmp")
  76.     if not os.path.exists(SRC_FOLDER):
  77.         print("❌ 错误:桌面未找到【新建文件夹】,请先创建!")
  78.         return []
  79.     img_list = []
  80.     for f in os.listdir(SRC_FOLDER):
  81.         if f.lower().endswith(suffix):
  82.             img_list.append(os.path.join(SRC_FOLDER, f))
  83.     if not img_list:
  84.         print("❌ 新建文件夹内没有图片!")
  85.     return img_list

  86. def resize_fit(img, tar_w, tar_h):
  87.     """图片等比例缩放+居中,不变形"""
  88.     ow, oh = img.size
  89.     scale = min(tar_w / ow, tar_h / oh)
  90.     nw = int(ow * scale)
  91.     nh = int(oh * scale)
  92.     img = img.resize((nw, nh), Image.Resampling.LANCZOS)
  93.     new_img = Image.new("RGB", (tar_w, tar_h), BG_COLOR)
  94.     new_img.paste(img, ((tar_w - nw) // 2, (tar_h - nh) // 2))
  95.     return new_img

  96. def start_export(img_list, select_list, margin_mode):
  97.     os.makedirs(OUT_FOLDER, exist_ok=True)

  98.     # 基础四周2.5mm固定边距
  99.     base_top = EDGE_MARGIN
  100.     base_bot = EDGE_MARGIN
  101.     base_left = EDGE_MARGIN
  102.     base_right = EDGE_MARGIN

  103.     # 叠加额外大留白
  104.     if margin_mode == 1:
  105.         top_big = MARGIN_3CM
  106.         bot_big = 0
  107.         mode_name = "上额外3cm留白"
  108.     elif margin_mode == 2:
  109.         top_big = 0
  110.         bot_big = MARGIN_3CM
  111.         mode_name = "下额外3cm留白"
  112.     else:
  113.         top_big = 0
  114.         bot_big = 0
  115.         mode_name = "仅四周2.5mm边距"

  116.     final_top = base_top + top_big
  117.     final_bot = base_bot + bot_big

  118.     for per_page in select_list:
  119.         rows, cols = LAYOUT_INFO[per_page]
  120.         sub_dir = os.path.join(OUT_FOLDER, f"{per_page}张_{mode_name}")
  121.         os.makedirs(sub_dir, exist_ok=True)

  122.         use_w = A4_W - base_left - base_right
  123.         use_h = A4_H - final_top - final_bot

  124.         cell_w = int((use_w - (cols - 1) * GAP) / cols)
  125.         cell_h = int((use_h - (rows - 1) * GAP) / rows)

  126.         page_idx = 0
  127.         for i in range(0, len(img_list), per_page):
  128.             batch = img_list[i:i+per_page]
  129.             a4 = Image.new("RGB", (A4_W, A4_H), BG_COLOR)

  130.             for idx, path in enumerate(batch):
  131.                 try:
  132.                     im = Image.open(path).convert("RGB")
  133.                     im = resize_fit(im, cell_w, cell_h)
  134.                     r = idx // cols
  135.                     c = idx % cols
  136.                     x = base_left + c * (cell_w + GAP)
  137.                     y = final_top + r * (cell_h + GAP)
  138.                     a4.paste(im, (x, y))
  139.                 except Exception:
  140.                     print(f"⚠️ 跳过损坏图片: {os.path.basename(path)}")

  141.             page_idx += 1
  142.             save_name = f"{per_page}张_第{page_idx}页.jpg"
  143.             save_path = os.path.join(sub_dir, save_name)
  144.             a4.save(save_path, quality=100)
  145.         print(f"✅ 【{per_page}张排版】生成完成 → {sub_dir}")

  146. if __name__ == "__main__":
  147.     pics = get_image_files()
  148.     if not pics:
  149.         input("\n按回车退出...")
  150.         exit()

  151.     print(f"📸 共读取图片:{len(pics)} 张\n")
  152.     select_layout = select_need_layout()
  153.     mar_mode = choose_margin_mode()

  154.     print("\n⏳ 开始生成选中排版,请稍候...\n")
  155.     start_export(pics, select_layout, mar_mode)

  156.     print("\n🎉 全部任务完成!")
  157.     print(f"📂 文件统一输出至桌面:【A4批量排版输出】")
  158.     input("\n按回车键关闭窗口...")
复制代码
代码附件如下:

嫌py麻烦的可以直接下载exe软件双击直接运行
exe附件下载地址:

   

  • 以下附件需要回复 (后刷新页面才能下载) 1 次可见,已回复 0
  • 1、一键6图.py 6KB 已下载 3 次
温馨提示:本站无需登入,即可回复帖子,发帖和回复请勿涉及违法等行为!网罗天下电脑(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号 ) 劰载中...