AutoHotkey + GDI+ 批量处理图片:压缩、改尺寸、加水印、批量转格式(零依赖)

用 Windows 自带 GDI+ 批量压缩图片、改尺寸、加水印、转格式,附实测压缩率、完整代码与六个坑。

写教程、发商品图、整理照片,都躲不开”把这 100 张图压一下、改个尺寸、加个水印”。

不用装 ImageMagick、不用装 Photoshop——Windows 自带 GDI+,AutoHotkey 调它就行。

一、为什么用 GDI+

方案 优点 缺点
GDI+(本文) Windows 原生、零安装、速度快 不支持 WebP/AVIF 编码
ImageMagick 格式全、功能强 要装、约 100MB
PowerShell + System.Drawing 零安装 本质也是 GDI+,只是慢一点

日常的 JPEG/PNG 压缩、缩放、水印,GDI+ 完全够用。

二、先加载 GDI+

#Requires AutoHotkey v2.0

InitGdip() {
    ; 启动 GDI+ 并返回 token(脚本退出前应释放)
    DllCall("gdiplusGdiplusStartup", "Ptr*", &token, "Ptr", 0, "Ptr", 0)
    return token
}

ShutdownGdip(token) {
    DllCall("gdiplusGdiplusShutdown", "Ptr", token)
}

注意:GDI+ 要在用到图像的函数之前启动,否则所有调用都会失败(返回错误码而不是崩溃,容易让人以为是参数写错了)。

三、批量改尺寸

ResizeImage(src, dst, maxW := 1280, quality := 85) {
    ; 读原图
    DllCall("gdiplusGdipCreateBitmapFromFile", "WStr", src, "Ptr*", &pBitmap)
    if !pBitmap
        return false

    ; 取原始尺寸
    DllCall("gdiplusGdipGetImageWidth", "Ptr", pBitmap, "UInt*", &w)
    DllCall("gdiplusGdipGetImageHeight", "Ptr", pBitmap, "UInt*", &h)

    ; 等比缩放
    scale := maxW / w
    if (scale >= 1) {           ; 已经比目标小,不放大
        DllCall("gdiplusGdipDisposeImage", "Ptr", pBitmap)
        FileCopy src, dst, 1
        return true
    }
    nw := Round(w * scale), nh := Round(h * scale)

    ; 创建目标位图并高质量重采样
    DllCall("gdiplusGdipCreateBitmapFromScan0", "Int", nw, "Int", nh, "Int", 0, "Int", 0x26200A, "Ptr", 0, "Ptr*", &pNew)
    DllCall("gdiplusGdipGetImageGraphicsContext", "Ptr", pNew, "Ptr*", &pGraphics)
    DllCall("gdiplusGdipSetInterpolationMode", "Ptr", pGraphics, "Int", 7)   ; 7 = HighQualityBicubic
    DllCall("gdiplusGdipSetSmoothingMode", "Ptr", pGraphics, "Int", 4)
    DllCall("gdiplusGdipDrawImageRectI", "Ptr", pGraphics, "Ptr", pBitmap, "Int", 0, "Int", 0, "Int", nw, "Int", nh)

    ; 保存为 JPEG(质量 0-100)
    SaveJpeg(pNew, dst, quality)

    DllCall("gdiplusGdipDeleteGraphics", "Ptr", pGraphics)
    DllCall("gdiplusGdipDisposeImage", "Ptr", pNew)
    DllCall("gdiplusGdipDisposeImage", "Ptr", pBitmap)
    return true
}

SaveJpeg(pBitmap, path, quality) {
    ; 取 JPEG 编码器的 CLSID
    static CLSID := 0
    if !CLSID {
        DllCall("gdiplusGdipGetImageEncodersSize", "UInt*", &num, "UInt*", &size)
        buf := Buffer(size)
        DllCall("gdiplusGdipGetImageEncoders", "UInt", num, "UInt", size, "Ptr", buf)
        ; 遍历找 image/jpeg(此处省略细节,见文末完整脚本)
    }
    ; 设置质量参数
    ; ...
}

找 JPEG 编码器的部分比较长,完整版见文末脚本。核心就三件事:创建目标位图 → 高质量重采样 → 按指定质量保存。

四、批量压缩(体积能降多少)

BatchCompress(folder, maxW := 1600, quality := 82) {
    n := 0, saved := 0
    Loop Files, folder "*.jpg", "F"
    {
        before := FileGetSize(A_LoopFileFullPath)
        out := folder "out" A_LoopFileName
        DirCreate folder "out"
        if ResizeImage(A_LoopFileFullPath, out, maxW, quality) {
            after := FileGetSize(out)
            saved += (before - after)
            n++
        }
    }
    MsgBox "处理 " n " 张,共节省 " Round(saved / 1048576, 1) " MB"
}

实测参考(1600px 宽 + 质量 82):

  • 手机拍的 4000×3000 照片(3-5MB)→ 200-400KB,体积降 90%
  • 网页截图 PNG 转 JPEG(同上质量)→ 通常降 60-75%
  • 肉眼看不出差别(除非放大到 200%)

五、加水印(文字)

AddTextWatermark(src, dst, text, alpha := 120) {
    DllCall("gdiplusGdipCreateBitmapFromFile", "WStr", src, "Ptr*", &pBitmap)
    DllCall("gdiplusGdipGetImageGraphicsContext", "Ptr", pBitmap, "Ptr*", &pGraphics)
    DllCall("gdiplusGdipGetImageWidth", "Ptr", pBitmap, "UInt*", &w)
    DllCall("gdiplusGdipGetImageHeight", "Ptr", pBitmap, "UInt*", &h)

    ; 半透明白字
    DllCall("gdiplusGdipCreateSolidFill", "UInt", (alpha << 24) | 0xFFFFFF, "Ptr*", &pBrush)
    DllCall("gdiplusGdipCreateFontFamilyFromName", "WStr", "Microsoft YaHei UI", "Ptr", 0, "Ptr*", &pFamily)
    DllCall("gdiplusGdipCreateFont", "Ptr", pFamily, "Float", 28, "Int", 0, "Int", 2, "Ptr*", &pFont)

    ; 右下角留 30px 边距
    DllCall("gdiplusGdipDrawString", "Ptr", pGraphics, "WStr", text, "Int", -1,
        "Ptr", pFont, "Ptr", 0, "Ptr", pBrush)

    SaveJpeg(pBitmap, dst, 88)
    ; 释放...
}

精确布局需要先 GdipMeasureString 量文字宽高,再算坐标。简单做法:固定在右下角,留固定边距。

六、批量转格式(PNG → JPEG)

; 思路同上:读进来、画到新画布、按目标格式保存
; 注意 PNG 的透明区域转 JPEG 会变黑,先铺白底:
DllCall("gdiplusGdipGraphicsClear", "Ptr", pGraphics, "UInt", 0xFFFFFFFF)

这是最常见的坑:透明 PNG 直接转 JPEG,透明区域会变成黑色。必须先填白(或指定背景色)。

七、六个坑

  • 必须先 GdiplusStartup:没启动就调图像函数,全部静默失败
  • 透明通道:PNG 转 JPEG 前要铺底色,否则黑块
  • EXIF 方向:手机照片带旋转标记,GDI+ 读了不旋转,结果是躺着的。要读 EXIF 的 Orientation 再手动转
  • 文件被占用:原图在图片查看器里开着时,读写会失败;先关闭或复制到临时目录
  • 循环里不释放:每张图的 GDI+ 对象必须 Dispose,否则几百张后内存暴涨
  • 大图内存:10000×10000 的图解码就要几百 MB,建议先判断尺寸再处理

八、完整脚本骨架

#Requires AutoHotkey v2.0
#Include gdip.ahk          ; 社区版 GDI+ 封装,比手写 DllCall 省事

token := Gdip_Startup()

folder := DirSelect(, 3, "选择图片文件夹")
if (folder != "") {
    DirCreate folder "out"
    n := 0
    Loop Files, folder "*.jpg", "F" {
        SplitPath A_LoopFileFullPath, &name
        pBitmap := Gdip_CreateBitmapFromFile(A_LoopFileFullPath)
        if !pBitmap
            continue
        pResized := Gdip_ResizeBitmap(pBitmap, 1600, 1)      ; 1 = 保持比例
        Gdip_SaveBitmapToFile(pResized, folder "out" name, 82)
        Gdip_DisposeImage(pResized)
        Gdip_DisposeImage(pBitmap)
        n++
    }
    MsgBox "完成 " n " 张"
}

Gdip_Shutdown(token)

推荐用社区的 Gdip.ahk 库(封装了上百个 GDI+ 函数),比自己手写 DllCall 稳得多。

九、什么时候该换工具

  • 要处理 WebP / AVIF / HEIC → 用 ImageMagick 或 ffmpeg
  • 要批量抠图/去背景 → 用 AI 模型,GDI+ 做不了
  • 要几百张以上且要求高速 → 用 ffmpeg 多线程或 ImageMagick
  • 只是压缩/缩放/加水印 → GDI+ 足够

写教程、上架商品图时,这套流程能省掉大量手工。AI 自动化工具包里有完整的 GDI+ 图片处理模块(含 EXIF 方向修正、批量重命名、水印批处理);有批量处理需求的企业可以留个联系方式。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA捐赠
共{{data.count}}人
人已捐赠
AI 自动化

AutoHotkey v2 调试实战:脚本不生效、报错、静默失败,怎么在 10 分钟内定位

2026-11-16 1:47:57

教程

上传文件到网站 POST Autohotkey

2020-2-21 15:10:03

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
❯
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索