【问题标题】:Batch rotate files with Gimp使用 Gimp 批量旋转文件
【发布时间】:2014-05-09 00:26:58
【问题描述】:

我正在使用以下 gimp 脚本将文件夹中的所有图像旋转 180°(该脚本保存在 ~/.gimp-2.8/scripts/batch-rotate.scm 下)

(define (batch-rotate pattern rotate-type)
  (let* ((filelist (cadr (file-glob pattern 1))))
    (while (not (null? filelist))
           (let* ((filename (car filelist))
                  (image (car (gimp-file-load RUN-NONINTERACTIVE
                                              filename filename)))
                  (drawable (car (gimp-image-get-active-layer image))))
             (gimp-image-rotate image rotate-type)
             (gimp-file-save RUN-NONINTERACTIVE
                             image drawable filename filename)
             (gimp-image-delete image))
           (set! filelist (cdr filelist)))))

我这样调用脚本:

gimp -i -b "(batch-rotate \"*.JPG\" 1)" -b '(gimp-quit 0)'

我收到以下错误:

While parsing XMP metadata:
Error on line 85 char 1: End of element <exif:Flash> not expected in this context
While parsing XMP metadata:
Error on line 99 char 1: End of element <exif:Flash> not expected in this context

Metadata parasite seems to be corrupt
While parsing XMP metadata:
Error on line 85 char 1: End of element <exif:Flash> not expected in this context

** (file-jpeg:29145): WARNING **: JPEG - unable to decode XMP metadata packet
While parsing XMP metadata:
Error on line 85 char 1: End of element <exif:Flash> not expected in this context
While parsing XMP metadata:
Error on line 99 char 1: End of element <exif:Flash> not expected in this context

Metadata parasite seems to be corrupt
While parsing XMP metadata:
Error on line 85 char 1: End of element <exif:Flash> not expected in this context

** (file-jpeg:29149): WARNING **: JPEG - unable to decode XMP metadata packet
JPEG image-Warning: Premature end of JPEG file

While parsing XMP metadata:
Error on line 71 char 1: End of element <exif:Flash> not expected in this context
While parsing XMP metadata:
Error on line 85 char 1: End of element <exif:Flash> not expected in this context

Metadata parasite seems to be corrupt

...等等,直到我打断它。

你能帮帮我吗?这里出了什么问题?

【问题讨论】:

    标签: image-processing batch-processing gimp script-fu


    【解决方案1】:

    我实际上并没有检查您的脚本代码 - 但您尝试调用的方式 这是不正确的。查看使用“echo”尝试命令行时显示的内容 而不是调用 GIMP:

    $ echo "(batch-rotate \"*.JPG\" 1)" -b '(gimp-quit 0)'
    (batch-rotate "*.JPG" 1) -b (gimp-quit 0)
    

    也就是说,*.JPG 模式包含在用于调用脚本的引号中,而 bash 不会将其展开到文件列表中。

    你可以用命令行语法来解决你的问题,然后修复你的脚本——其中还有一些其他的错误,否则,它将无法找到一个名为“*.JPG”的文件并结束。 但是,我建议您使用 Python 而不是 script-fu 编写脚本。它是一种更高级别的多用途语言,而不是像内置 script-fu 那样精简的学术解释器,并且可以轻松读取目录中的所有 JPG 文件,结构如下:

    import glob
    for filename in directory + "/*.JPG":
       <code>
    

    而不是尝试破解通过命令行传递文件列表的方法,然后破解从列表中提取每个文件名的方法。

    我很抱歉推荐另一种语言,但除非您已经精通 Scheme(script-fu 语言),否则学习 Python 在许多其他情况下对您更有用 - 包括作为其他程序的脚本语言,以及包括其他图像库,如 PILLOW、pygame、vips、python-gegl、python-magick、python-opencv,所有这些都可以旋转或翻转图像以及 Python-GIMP 绑定。

    【讨论】:

    • 我已经非常精通python了——只是我认为方案绑定更原生,并且只使用shell与所有东西交互会更简单。我可以用python在~/.gimp*/scripts/下写文件吗?
    • Python 脚本位于~/.gimp*/plugins 下,必须标记为可执行。唯一需要的两件事是调用“注册”和“主” - 前者用于注册您的过程。 Python 绑定实际上比方案更完整:两者都可以使用完全导出的 PDB,但是从 Python 中,您可以对像素区域进行字节级访问 - 还有一些不错的对象,例如 Image、Layer。层组,建立在 PDB 值之上。
    • 调用代码没有错,因为 scheme 函数调用了 globbing 例程——它实际上是在传递一个字面量模式,并不期望 shell 进行扩展。
    • @Julian: 与其仅仅投反对票,您是否会在意发现 O.P. 的方案脚本问题,因为您似乎喜欢这种语言?我重申我的观点,即它的级别太低,并且具有难以阅读和难以编写的语法,并且应该用 Python 编写新脚本,而不管原始问题有什么问题。
    • 实际上我也想不通方案——我为我的问题走上了 python 路线。你的回答说“我没有读过这个问题,但你做错了”,而他们没有。如果您想改写您的答案以推荐 python 而不批评“传入模式并让脚本执行 globbing”方法,我会很高兴地支持它。
    【解决方案2】:

    这是一个本机脚本,我已经测试过它可以正确地将一堆 JPG 旋转 90 度:

      (define (batch-rotate90 pattern)
      (let* ((filelist (cadr (file-glob pattern 1))))
        (while (not (null? filelist))
               (let* ((filename (car filelist))
                      (image (car (gimp-file-load RUN-NONINTERACTIVE
                                                  filename filename)))
                      (drawable (car (gimp-image-get-active-layer image))))
    
                 (gimp-image-rotate image 0)
    
                 (gimp-file-save RUN-NONINTERACTIVE
                                 image drawable filename filename)
                 (gimp-image-delete image))
               (set! filelist (cdr filelist)))))
    

    调用批处理文件是:

    gimp-2.6 -i -b "(batch-rotate90 \"*.jpg\")" -b "(gimp-quit 0)"
    

    将脚本中的第 9 行调整为 (gimp-image-rotate image 1) 应使旋转 180 度(0 = 90 度,1 = 180 度,3 = 270 度)。发现于the website of Gregory Alan Hildstrom

    由于此脚本和 OP 似乎实际上相同,因此 OP 的原始图像文件实际上可能已损坏(如显示的错误所示,“元数据寄生虫似乎已损坏”)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多