ImageMagick单次调用中的多个操作 [英] ImageMagick multiple operations in single invocation

查看:352
本文介绍了ImageMagick单次调用中的多个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让ImageMagick在一次调用中执行多个操作(在本例中为crop),但似乎不可能。例如,我可以裁剪,旋转,然后另一个裁剪作为两个单独的命令:

I'm trying to get ImageMagick to perform multiple operations (in this case, crops) in a single invocation, but it doesn't seem to be possible. For example, I can do a crop, rotate, and then another crop as two separate commands:

$ convert test.jpg -crop 223x187+588+757 -rotate 330 crop2.jpg 
$ convert crop2.jpg -crop 200x100+43+87 crop3.jpg

但如果我尝试将它们组合成一个命令,我会收到错误:

But if I try to combine them into a single command, I get an error:


$ convert test.jpg -crop 223x187 + 588 + 757 -rotate 330 -crop
200x100 + 43 + 87 crop3.jpg
convert:geometry不包含图片`test.jpg'@ warning / transform.c / CropImage / 666。

$ convert test.jpg -crop 223x187+588+757 -rotate 330 -crop 200x100+43+87 crop3.jpg convert: geometry does not contain image `test.jpg' @ warning/transform.c/CropImage/666.

显然我可以创建一个管道,但如果可能的话,我想避免多个进程的额外开销。有没有办法做到这一点?

Obviously I could just create a pipe, but if possible I want to avoid the extra overhead of multiple processes. Is there a way to do this?

更新:
此外,管道似乎有问题:我不想要额外的压缩/解压缩阶段所以我尝试将它作为RGB管道,但我似乎无法正确获取参数:

Update: Also, a pipe seems problematic: I don't want an extra compress/decompress stage so I try piping it as RGB, but I can't seem to get the parameters right:

convert test.jpg -crop 223x187+588+757 -rotate 330 rgb:- | convert -size 287x273 -depth 8 rgb:- -crop 200x100+43+87 crop3.jpg
convert: unexpected end-of-file `-': No such file or directory @ error/rgb.c/ReadRGBImage/231.

好的,所以事实证明上面的错误是因为我计算的图像大小与ImageMagick的计算不同两个像素。因此,显然必须尝试使图像大小计算与IM完全相同是管道这两个命令的需要引入的额外问题。

Ok, so it turns out the above error is because my calculated size for the image differs from ImageMagick's calculations by two pixels. So obviously having to try to get the image size calculation exactly the same as IM is an extra problem introduced by the need to pipeline the two commands.

推荐答案

这是一个更通用的问题方法。

Here is a more generic approach to the question.

ImageMagick提供了一些巧妙的技巧,可以帮助优化处理速度......

ImageMagick provides a few clever tricks which can help to optimize processing speed when...


  1. ...您想要处理大图像和

  2. ...当您想要创建不同的输出时来自同一原文。

有不同的方法甚至可以合并:

There are different methods which can even be combined:


  1. 利用ImageMagick的特殊 mpr:{label} 格式和语法。
    这告诉ImageMagick暂时将输入图像保存到命名的 magick程序寄存器标签中。
    (我也看到它解释为 magick持久性(内存)寄存器内存程序寄存器。)
    命令管道可以稍后(处理时)从那里读取图像数据的速度比从硬盘读取的速度快得多。
    该命令可以多次读取它。@ / li>
  2. 利用ImageMagick的特殊 mpc:{name} 格式和语法。
    这类似于 mpr:
    它的长名称是 magick pixel cache
    我也把它看作 magick persistent(磁盘)缓存
    MPC是ImageMagick的本机内存未压缩文件格式,但转储到磁盘。
    一旦写入磁盘,它可以比JPEG,TIFF或PNG更快地重新读入内存,因为不需要转换。

  3. 构造一个聪明的命令序列。
    在适当的情况下,使用转义括号中的 \(... \)语法,使用sideway处理。

  1. Take advantage of ImageMagick's special mpr:{label} format and syntax. This tells ImageMagick to temporarily save the input image into a named magick program register label. (I've also seen it explained as magick persistent (memory) register or memory program register.) The command pipeline can then later (while processing) read the image data much faster from there than it could read from harddisk. And the command can read it as often as you want, multiple times.
  2. Take advantage of ImageMagick's special mpc:{name} format and syntax. This works similar to mpr:. Its long name is magick pixel cache. I've also seen it as magick persistent (disk) cache. MPC is the native in-memory uncompressed file format of ImageMagick, but dumped to disk. Once written to disk, it can be re-read faster into memory again than a JPEG, TIFF or PNG could, because no conversion is needed.
  3. Construct a clever command sequence. Make use of "sideway" processing, using the \( ... \) syntax in escaped parentheses where appropriate.

MPC

转换时命令读取输入图像(JPEG,TIFF,GIF,PNG ......它当前可能采用的任何格式),它首先将此格式处理为MPC。

When the convert command reads input image (JPEG, TIFF, GIF, PNG... whatever format it may currently be), it first processes this format into an MPC.

此处理结果作为未压缩的栅格格式保存在RAM中。
从那里进行进一步的转换和图像处理。
如果要将MPC写入磁盘 作为MPC ,可以使用 + write mpc:myfilename
这基本上只是直接内存转储到磁盘。

This processed result is kept in RAM as an uncompressed raster format. From there the further conversions and image manipulations happen. When you want write an MPC to disk as MPC you can use +write mpc:myfilename. This basically is simply a direct memory dump to disk.

然后ImageMagick写入两个,而不是一个(并且比通常更大)的二进制文件: myfilename.mpc myfilename.cache
.mpc 文件保存图像的元数据, .cache 保存实际的像素缓存数据。

ImageMagick then writes two, not one, (and larger than usual) binary files: myfilename.mpc and myfilename.cache. The .mpc file holds the metadata of the image, .cache holds the actual pixel cache data.

从这个磁盘文件读取的是一个从磁盘到内存的快速内存映射(类似于内存页面交换)。
但是由于最初的 + write 操作确实存储了本机未压缩的内部IM本机栅格格式,现在不需要图像解码。

A read from this disk file(s) is a fast memory map from disk to memory as needed (similar to memory page swapping). But since the initial +write operation did store the native uncompressed internal, IM-native raster format, now no image decoding is needed.

所以这可以更快(特别是对于大图像)使用, IF 你无法避免写出你需要阅读的临时文件再次。
但是,请小心磁盘空间。
一旦不再需要磁盘上的MPC文件,请记得清理。
IM不会自动跟踪您的 + write 命令。

So this can be faster (especially for large images) to use, IF you cannot avoid writing temporary files out which you need to read in again. However, be careful with your disk space. Remember to clean up, once you no longer need the MPC file on disk. IM will not automatically track your +write commands for you.

(更多技术细节:作为磁盘文件格式的MPC不可移植。
它也不适合作为长期存档格式。
它唯一的适用性是作为高性能的中间格式图像处理。
它需要两个文件来支持一个图像。
在ImageMagick版本之间不能保证稳定。
它可能无法在与您创建它的机器不同的机器上运行。)

如果您仍想将此格式保存到磁盘,请记住这个:

If you still want to save this format to disk, remember this:


  • 图像属性被写入扩展名为 .mpc 的文件中。

  • 图像像素被写入扩展名为 .cache 的文件。

  • Image attributes are written to a file with the extension .mpc.
  • Image pixels are written to a file with the extension .cache.

MPC的主要优势在于...

MPC's main advantages lay in...


  1. ...处理非常大的图像,或
  2. ...在操作管道中的同一个图像上应用多个操作

  1. ...processing very large images, or when
  2. ...applying several operations on one and the same image in "operation pipelines".

MPC专为符合条件多次读,写一次的工作流模式而设计。

MPC was designed especially for workflow patterns which match the criteria "read many times, write once".

MPR

MPR格式(内存持久寄存器)做与MPC类似的事情。
它通过命名的内存寄存器使图像可用。
您可以使用您想要的任何名称(偶数)。
您的流程管道还可以从该寄存器再次读取图像 - 如果需要,也可以多次读取。
图像 mpr:label 在寄存器中保留,直到当前命令管道退出。 (这与磁盘写入 mpc:filename 的区别。这会持续完成当前管道;它甚至可以在系统重新启动后继续存在。)

The MPR format (memory persistent register) does something similar as MPC. It makes the image available via a named memory register. You can use whatever name you want (even numbers). Your process pipeline can also read the image again from that register -- multiple times even, should it need to do so. The image mpr:label persists in the register until the current command pipeline exits. (This is the difference to a to-disk-written mpc:filename. This persists the completion of the current pipeline; it even survives system reboots.)

(我在这里使用'管道'一词不是与shell中的管道混淆,其中多个命令和进程被启动和链接。
这里我只讨论一次调用 convert 哪个链多个图像处理和操作分为一个过程。)

(My use of the word 'pipeline' here is not to be confused with the pipelines in a shell, where multiple commands and processes are started and chained. Here I talk only about a single invocation of convert which chains multiple image manipulations and operations into one process.)

因此可以完成所有调整大小,裁剪,追加,缩略图,颜色操作,模糊,。 ..在一个过程中你有什么操作,写出你需要的不同中间输出。

So it is possible to accomplish all resize, crop, append, thumbnailing, color-manipulating, blurring, ... what-have-you operations in one single process writing out the different intermediate outputs you require.

......嗯,我不太确定它是否 soooo 实用。我还没有测试过,我只是用我的幻想来构建一个示例,该示例显示了不同概念的使用原则。

...hmm, I'm not so sure if it is soooo practical. I haven't tested it, I just used my phantasy to construct an example which shows the principles about the usage of the different concepts.

注意: +写someimage 相同-write someimage + delete

convert \
    very-very-large.jpg \
   -write mpr:XY \
   +delete \
   -respect-parentheses \
     \( mpr:XY -crop '3000x2001+0+491' -resize '170x116!>'   +write pic1.png   \) \
     \( mpr:XY -crop '2981x2883+8+0'   -resize '75x75!>'     +write pic2.png   \) \
     \( mpr:XY -crop '1100x1983+0+0'   -resize '160x160!>'   +write pic3.png   \) \
     \( mpr:XY -crop '2000x2883+0+0'   -resize '1024x960!>'  +write pic4.png   \) \
     \( mpr:XY -crop '1000x2883+0+0'   -resize '190x188!>'   +write mpr:pic5   \) \
     \( mpr:pic5                                             +write pic5.png   \) \
     \( mpr:XY -crop '3000x2000+0+0'   -resize '2048x2047!>' +write pic6.png   \) \
     \( mpr:XY -crop '3000x2883+0+0'   -resize '595x421!>'   +write pic7.png   \) \
     \( mpr:XY -crop '3000x2883+0+0'   -resize '3000x2883!>' +write mpr:AB     \) \
     \( mpr:AB                                               +write pic8.tiff  \) \
     \( mpr:AB -blur 0x8                                     +write blur1.gif  \) \
     \( mpr:pic5 mpr:AB +append mpr:pic5 -append             +write append.jpg \) \
     \( mpr:pic5 -rotate -130 mpr:AB -gravity center                              \
                 -compose difference -composite +write final.png               \) \
null:

之后第一个操作, -write mpr:XY ,堆栈中有两个图像:

After the first operation, -write mpr:XY, there are two images in the stack:


  1. 输入文件, very-very-large.png

  2. 可以从内存中读取的副本使用其标签 XY

  1. the input file, very-very-large.png, and
  2. a copy of it which can be read from memory using its label XY.

我们不需要这两个中的第一个再也没有。
因此我们使用 + delete 将其从堆栈中删除。

We do not need the first of these two any more. Hence we use +delete to remove it from the stack.

所以这个命令使用了一个命令管道,它执行多个命令和操作,一次创建11个不同的输出图像:
pic {1,2,3,4,5,6,7} .png blur1.gif pic8。 tiff append.jpg final.png

So this command used one single command pipeline which carried out multiple commands and manipulations creating 11 different output images in one go: pic{1,2,3,4,5,6,7}.png, blur1.gif, pic8.tiff, append.jpg and final.png.

这篇关于ImageMagick单次调用中的多个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆