1

我需要一个非常简单的 shell 脚本来处理文件夹中的所有图像并更改其大小。图像处理是用 gimp script-fu 完成的,shell 脚本唯一要做的就是 for 循环。

我做的:

#!/bin/sh

mkdir processed
for image in `ls`
do
    if [ $image != "script.sh" ]
    then
        if [ $image != "processed" ]
        then
            gimp -i -b '(let* ( (img (gimp-file-load 1 "1.jpg" "1.jpg")) (drw (gimp-image-get-active-drawable (car img))) ) (gimp-image-scale-full 1 400 300 3) (file-jpeg-save 1 (car img) (car drw) "processed/1.jpg" "1.jpg" 0.6 0 1 1 "" 3 0 0 2) (gimp-quit 0) )'
        fi
    fi
done

这段代码有效,但是在 script-fu 代码中,我将 1.jpg 作为文件名,当然,我希望出现 $image 变量的值。我的 shell 脚本知识有限,我迷失了将变量放在命令中的方式。

你能帮助我吗?谢谢你的时间 :)

4

3 回答 3

2

使用for image in *而不是ls.

要将变量传递给 Gimp 脚本并保留 Gimp 的引号,您需要对外部引号使用双引号并转义内部引号:

gimp -i -b "(let* ( (img (gimp-file-load 1 \"$image\" \"$image\")) (drw (gimp-image-get-active-drawable (car img))) ) (gimp-image-scale-full 1 400 300 3) (file-jpeg-save 1 (car img) (car drw) \"processed/$image\" \"$image\" 0.6 0 1 1 "" 3 0 0 2) (gimp-quit 0) )"

您还可以简化脚本:

mkdir processed
for image in *.jpg
do
    if [[ -f $image ]]
    then
        gimp ...
    fi
done

如果您想包含更多扩展:

for image in *.{jpg,JPG,jpeg,JPEG,gif,GIF,png,PNG}
于 2011-02-28T14:40:47.957 回答
2

另一种可能更具可读性的方法:

mkdir processed

gimp_script_template='(let* ( 
    (img (gimp-file-load 1 "%s" "%s")) 
    (drw (gimp-image-get-active-drawable (car img))) 
  ) 
  (gimp-image-scale-full 1 400 300 3) 
  (file-jpeg-save 1 (car img) (car drw) "processed/%s" "%s" 0.6 0 1 1 "" 3 0 0 2) 
  (gimp-quit 0) 
)'

for img in *; do
    [ ! -f "$img" ] && continue
    [ "$img" = "script.sh" ] && continue
    gimp_script="$( printf "$gimp_script_template" "$img" "$img" "$img" "$img" )"
    gimp -i -b "$gimp_script"
done
于 2011-02-28T15:34:25.933 回答
0

你可以试试

"'(let* ( (img (gimp-file-load 1 "$image" "$image")) (drw (gimp-image-get-active-drawable (car img))) ) (gimp-image-scale-full 1 400 300 3) (file-jpeg-save 1 (car img) (car drw) "processed/$image" "$image" 0.6 0 1 1 "" 3 0 0 2) (gimp-quit 0) )'"
于 2011-02-28T14:30:23.300 回答