如何使用 GIMP 编写自定义自动裁剪脚本?

发布于 2024-11-03 17:46:13 字数 1736 浏览 3 评论 0原文

我有一堆屏幕截图,我想裁剪掉窗口边框。我想使用脚本将它们全部裁剪。

我可以使用 GIMP,但不能使用 Photoshop,所以我认为 GIMP 将是最好使用的工具。我以前没有使用 GIMP 编写过脚本,所以我查找了一些 GIMP 裁剪脚本。我找到的都与我想要的相似,但又不完全一样。我认为将脚本更改为我需要的内容是一件简单的事情。但由于我不熟悉脚本语言,事实证明它比我想象的要困难。我在这里找到了一个很棒的自动裁剪脚本。有人可以帮我根据我的需要定制它吗?

    (define (script-fu-rs-center-crop filename outfilename width height)

  (let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
          (drawable (car (gimp-image-get-active-layer image))))
          (let* ((original-width (car (gimp-image-width image)))
               (original-height (car (gimp-image-height image)))
               (new-width original-width)
               (new-height original-height)
               (offset-x 0)
               (offset-y 0))

               (if (<= (/ original-width original-height) (/ width height))
                   (gimp-image-crop image original-width (* original-width (/ height width)) 0 (/ (- original-height (* original-width (/ height width))) 2) )
                   (gimp-image-crop image (* original-height (/ width height)) original-height (/ (- original-width (* original-height (/ width height))) 2) 0)
               )
           )

  (set! drawable (car (gimp-image-get-active-layer image)))

(gimp-file-save RUN-NONINTERACTIVE image drawable outfilename outfilename)
     (gimp-image-delete image)))

所有照片的整体尺寸并不相同。但它们的顶部、左侧、右侧和底部的像素数确实与我想要裁剪的像素数相同。假设图像的像素尺寸为 widthheight,我想删除顶部的 t_junk 像素,b_junk > 偏离底部的像素、l_junk 偏离左侧的像素、以及 r_junk 偏离右侧的像素。因此,我希望新的图像尺寸为宽度 - l_junk - r_junk高度 - t_junk - b_junk

I have a bunch of screen shots and I'd like to crop out the window border. I would like to crop them all using a script.

I have access to the GIMP, but not photoshop so I assumed that the GIMP would be the best tool to use. I have not scripted with the GIMP before, so I looked up some GIMP crop scripts. The ones I found are all similar to what I want, but not quite. I thought it would be a simple matter to alter the script to what I need. But since I am not familiar with the scripting language it is proving more difficult than I thought. I found a great auto crop script here. Can someone help me customize it for what I need?

    (define (script-fu-rs-center-crop filename outfilename width height)

  (let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
          (drawable (car (gimp-image-get-active-layer image))))
          (let* ((original-width (car (gimp-image-width image)))
               (original-height (car (gimp-image-height image)))
               (new-width original-width)
               (new-height original-height)
               (offset-x 0)
               (offset-y 0))

               (if (<= (/ original-width original-height) (/ width height))
                   (gimp-image-crop image original-width (* original-width (/ height width)) 0 (/ (- original-height (* original-width (/ height width))) 2) )
                   (gimp-image-crop image (* original-height (/ width height)) original-height (/ (- original-width (* original-height (/ width height))) 2) 0)
               )
           )

  (set! drawable (car (gimp-image-get-active-layer image)))

(gimp-file-save RUN-NONINTERACTIVE image drawable outfilename outfilename)
     (gimp-image-delete image)))

The overall dimensions are not the same for all the photos. But they do all have the same number of pixels on the top, left, right and bottom that I want to crop out. So let's say the image has pixel dimensions width and height and I want to remove t_junk pixels off the top, b_junk pixels off the bottom, l_junk pixels off the left, and r_junk pixels off the right. So I want the new image dimensions to be width - l_junk - r_junk and height - t_junk - b_junk.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无妨# 2024-11-10 17:46:13

Edger 脚本

我编写了一个自定义 GIMP 脚本,它完全可以满足您的要求。只需将以下内容粘贴到文本文档中,并将其以 .scm 扩展名保存在 GIMP 脚本文件夹中(可以在 编辑 > 首选项 > 文件夹 > 脚本中找到/创建路径)):

(define (script-fu-wirebear-edger filename outfilename top right bottom left)
(let* (
    (img (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
    (owidth (car (gimp-image-width img)))
    (oheight (car (gimp-image-height img)))
    (width (- owidth (+ right left)))
    (height (- oheight (+ top bottom)))
      )

   ;Crop Image
    (gimp-image-crop img width height left top)

   ;Save
    (gimp-file-save RUN-NONINTERACTIVE
        img
        (car (gimp-image-active-drawable img))
        outfilename
        outfilename)

   ;Cleanup
    (gimp-image-delete img)
))
(script-fu-register "script-fu-wirebear-edger"
    "Edger"
    "Removes junk from the edges of an image"
    "Chris Kent"
    "WireBear.com"
    "August 2011"
    "RGB* GRAY*"
    SF-STRING "Filename" ""
    SF-STRING "OutputFilename" ""
    SF-VALUE "TopEdge" "0"
    SF-VALUE "RightEdge" "0"
    SF-VALUE "BottomEdge" "0"
    SF-VALUE "LeftEdge" "0"
)
script-fu-wirebear-edger()

该脚本接受输入文件名、输出文件名以及每侧要削除的像素数。您可以从 Windows 运行命令(假设您已将 GIMP 设置为环境变量),如下所示(请务必转义所示的特殊字符并将所有字符放在一行中)

C:>gimp-2.6 -i -c -b
   "(script-fu-wirebear-edger \"C:\\Users\\You\\Desktop\\Images\\1.png\" 
   \"C:\\Users\\You\\Desktop\\Images\\1_edged.png\" 10 30 25 5)"
   -b "(gimp-quit 0)"

或者您可以在 Script-Fu Console 中运行它(Filters > Script-Fu > Console) - 无论操作系统如何,如下所示:

(script-fu-wirebear-edger "C:\\Users\\You\\Desktop\\Images\\1.png" 
"C:\\Users\\You\\Desktop\\Images\\1_edged.png" 10 30 25 5)

Batch Edger Script

为了在多个图像上运行 Edger 脚本,您可以将以下脚本与上述脚本结合使用(您的脚本文件夹中将需要这两个脚本):

(define (script-fu-wirebear-batch-edger pattern outsuffix top right bottom left)
(let* (
    (filelist (cadr (file-glob pattern 1)))
    (filename "")
    (outfn "")
      )
    (while (not (null? filelist))
        (set! filename (car filelist))
        (set! outfn 
            (string-append 
                (string-append 
                    (substring filename 0 (- (string-length filename) 4))
                    outsuffix)
                (substring filename (- (string-length filename) 4))
            )
        )
        (script-fu-wirebear-edger filename outfn top right bottom left)
        (set! filelist (cdr filelist))
    )
))
(script-fu-register "script-fu-wirebear-batch-edger"
    "Batch Edger"
    "Removes junk from the edges of a series of images"
    "Chris Kent"
    "WireBear.com"
    "August 2011"
    "RGB* GRAY*"
    SF-STRING "Pattern" "*.png"
    SF-STRING "OutputSuffix" "_edged"
    SF-VALUE "TopEdge" "0"
    SF-VALUE "RightEdge" "0"
    SF-VALUE "BottomEdge" "0"
    SF-VALUE "LeftEdge" "0"
)
script-fu-wirebear-batch-edger()

该脚本采用搜索模式来匹配目标图像,以及要添加到文件的后缀名称以及每张图像每一侧要削除的像素数。您可以从 Windows 运行命令(假设您将 GIMP 设置为环境变量),如下所示(请务必转义所示的特殊字符并将所有字符放在一行中)

C:>gimp-2.6 -i -c -b
   "(script-fu-wirebear-batch-edger \"C:\\Users\\You\\Desktop\\Images\\*.png\" 
   \"_edged\" 10 30 25 5)"
   -b "(gimp-quit 0)"

或者您可以在 Script-Fu Console 中运行它(Filters > Script-Fu > Console) - 无论操作系统如何,如下所示:

(script-fu-wirebear-batch-edger "C:\\Users\\You\\Desktop\\Images\\*.png"
"_edged" 10 30 25 5)

Edger Script

I have written a custom GIMP script that does exactly what you have asked for. Just paste the following into a text document and save it with a .scm extension in your GIMP scripts folder (the path can be found/created in Edit > Preferences > Folders > Scripts):

(define (script-fu-wirebear-edger filename outfilename top right bottom left)
(let* (
    (img (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
    (owidth (car (gimp-image-width img)))
    (oheight (car (gimp-image-height img)))
    (width (- owidth (+ right left)))
    (height (- oheight (+ top bottom)))
      )

   ;Crop Image
    (gimp-image-crop img width height left top)

   ;Save
    (gimp-file-save RUN-NONINTERACTIVE
        img
        (car (gimp-image-active-drawable img))
        outfilename
        outfilename)

   ;Cleanup
    (gimp-image-delete img)
))
(script-fu-register "script-fu-wirebear-edger"
    "Edger"
    "Removes junk from the edges of an image"
    "Chris Kent"
    "WireBear.com"
    "August 2011"
    "RGB* GRAY*"
    SF-STRING "Filename" ""
    SF-STRING "OutputFilename" ""
    SF-VALUE "TopEdge" "0"
    SF-VALUE "RightEdge" "0"
    SF-VALUE "BottomEdge" "0"
    SF-VALUE "LeftEdge" "0"
)
script-fu-wirebear-edger()

This script takes in the input filename, the output filename and the number of pixels to shave off on each side. You can run the command from Windows (Assuming you have GIMP setup as an environment variable) like this (Be sure to escape special characters as shown and place all on one line):

C:>gimp-2.6 -i -c -b
   "(script-fu-wirebear-edger \"C:\\Users\\You\\Desktop\\Images\\1.png\" 
   \"C:\\Users\\You\\Desktop\\Images\\1_edged.png\" 10 30 25 5)"
   -b "(gimp-quit 0)"

Or you can run it in the Script-Fu Console (Filters > Script-Fu > Console) - regardless of OS like this:

(script-fu-wirebear-edger "C:\\Users\\You\\Desktop\\Images\\1.png" 
"C:\\Users\\You\\Desktop\\Images\\1_edged.png" 10 30 25 5)

Batch Edger Script

In order to run the Edger script on multiple images, you can use the following script in conjunction with the above script (You will need both in your Scripts folder):

(define (script-fu-wirebear-batch-edger pattern outsuffix top right bottom left)
(let* (
    (filelist (cadr (file-glob pattern 1)))
    (filename "")
    (outfn "")
      )
    (while (not (null? filelist))
        (set! filename (car filelist))
        (set! outfn 
            (string-append 
                (string-append 
                    (substring filename 0 (- (string-length filename) 4))
                    outsuffix)
                (substring filename (- (string-length filename) 4))
            )
        )
        (script-fu-wirebear-edger filename outfn top right bottom left)
        (set! filelist (cdr filelist))
    )
))
(script-fu-register "script-fu-wirebear-batch-edger"
    "Batch Edger"
    "Removes junk from the edges of a series of images"
    "Chris Kent"
    "WireBear.com"
    "August 2011"
    "RGB* GRAY*"
    SF-STRING "Pattern" "*.png"
    SF-STRING "OutputSuffix" "_edged"
    SF-VALUE "TopEdge" "0"
    SF-VALUE "RightEdge" "0"
    SF-VALUE "BottomEdge" "0"
    SF-VALUE "LeftEdge" "0"
)
script-fu-wirebear-batch-edger()

The script takes in a search pattern to match target images, a suffix to add to the file name and the number of pixels to shave off on each side of every image. You can run the command from Windows (Assuming you have GIMP setup as an environment variable) like this (Be sure to escape special characters as shown and place all on one line):

C:>gimp-2.6 -i -c -b
   "(script-fu-wirebear-batch-edger \"C:\\Users\\You\\Desktop\\Images\\*.png\" 
   \"_edged\" 10 30 25 5)"
   -b "(gimp-quit 0)"

Or you can run it in the Script-Fu Console (Filters > Script-Fu > Console) - regardless of OS like this:

(script-fu-wirebear-batch-edger "C:\\Users\\You\\Desktop\\Images\\*.png"
"_edged" 10 30 25 5)
甜心小果奶 2024-11-10 17:46:13

Gimp 不是适合这项工作的工具。安装 imagemagick 或graphicsmagick。它包含 convert 二进制文件。

请参阅此处:http://www.imagemagick.org/Usage/crop/#crop_repage

Gimp is not the right tool for the job. Install imagemagick or graphicsmagick. It contains convert binary.

See here: http://www.imagemagick.org/Usage/crop/#crop_repage

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文