如何使用 GIMP 编写自定义自动裁剪脚本?
我有一堆屏幕截图,我想裁剪掉窗口边框。我想使用脚本将它们全部裁剪。
我可以使用 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)))
所有照片的整体尺寸并不相同。但它们的顶部、左侧、右侧和底部的像素数确实与我想要裁剪的像素数相同。假设图像的像素尺寸为 width
和 height
,我想删除顶部的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Edger 脚本
我编写了一个自定义 GIMP 脚本,它完全可以满足您的要求。只需将以下内容粘贴到文本文档中,并将其以
.scm
扩展名保存在 GIMP 脚本文件夹中(可以在编辑 > 首选项 > 文件夹 > 脚本中找到/创建路径)
):该脚本接受输入文件名、输出文件名以及每侧要削除的像素数。您可以从 Windows 运行命令(假设您已将 GIMP 设置为环境变量),如下所示(请务必转义所示的特殊字符并将所有字符放在一行中) :
或者您可以在 Script-Fu Console 中运行它(
Filters > Script-Fu > Console
) - 无论操作系统如何,如下所示:Batch Edger Script
为了在多个图像上运行 Edger 脚本,您可以将以下脚本与上述脚本结合使用(您的脚本文件夹中将需要这两个脚本):
该脚本采用搜索模式来匹配目标图像,以及要添加到文件的后缀名称以及每张图像每一侧要削除的像素数。您可以从 Windows 运行命令(假设您将 GIMP 设置为环境变量),如下所示(请务必转义所示的特殊字符并将所有字符放在一行中) :
或者您可以在 Script-Fu Console 中运行它(
Filters > Script-Fu > Console
) - 无论操作系统如何,如下所示: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 inEdit > Preferences > Folders > Scripts
):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):
Or you can run it in the Script-Fu Console (
Filters > Script-Fu > Console
) - regardless of OS like this: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):
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):
Or you can run it in the Script-Fu Console (
Filters > Script-Fu > Console
) - regardless of OS like this: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