使用Linux上的ImageMagick命令行进行批处理转换时产生的破坏结果

时间:2022-08-23 08:58:03

This is my IM command:

这是我的IM命令:

/usr/bin/convert 
'src.tif'
  -limit memory 0
  -limit map 0
  -limit file 0
  -alpha transparent
  -clip
  -alpha opaque
  -resize 800x600
  'end.png'
2>&1

So this will remove the white background of my TIFF by clipping the path that is given in the file. It will be resized and saved as transparent PNG.

这将通过剪切文件中给定的路径来删除TIFF的白色背景。它将被调整大小并保存为透明PNG。

I got no errors from IM running this.

我运行这个程序没有错误。

But if I run this command with PHP to execute it on about 13000 files - I sometimes get these errors:

但是,如果我用PHP运行这个命令,在大约13000个文件上执行它,我有时会得到以下错误:

sh: line 1: 25065 Killed                  /usr/bin/convert \
            'public_html/source_files/XXXX123/XXXX123/XXXX123.tif' \
             -limit memory 0 -limit map 0 -limit file 0 -alpha transparent \
             -clip -alpha opaque -resize 800x600 \
            'public_html/converted/XXXX123/XXXX123/XXXX123_web.png' 2>&1

sh: line 1: 25702 Killed                  /usr/bin/convert \
            'public_html/source_files/XXXX123/XXXX123/XXXX123.tif' \
             -limit memory 0 -limit map 0 -limit file 0 -alpha transparent \
             -clip -alpha opaque -resize 800x600 \
            'public_html/converted/XXXX123/XXXX123/XXXX123_web.png' 2>&1

But the bigger problem is: Some of the pictures are broken. Below is a "bad" image on the left, a "good" image on the right (ondrag/on a dark background you see the problem better):

但更大的问题是:一些图片被破坏了。下面是左边的“坏”图像,右边的“好”图像(ondrag/在黑色背景上你能更好地看到问题):

使用Linux上的ImageMagick命令行进行批处理转换时产生的破坏结果使用Linux上的ImageMagick命令行进行批处理转换时产生的破坏结果

On running the command manually the result was ok. Only on running this PHP loop script will provide broken results. ( PHP loop script )

在手动运行命令时,结果是ok的。只有在运行这个PHP循环脚本时,才会产生错误的结果。(PHP循环脚本)

I run the script this way: php55 run.php. A simple loop with find as shell script provides same results.

我这样运行脚本:php55 run.php。一个带有find as shell脚本的简单循环将提供相同的结果。

So I searched, asked in the IM discourse server and run this procedure on 2 machines with different distribution (Debian Wheezy, Ubuntu Server 14.04)

于是我在IM discourse server上搜索,询问并在两台不同分布的机器上运行这个过程(Debian Wheezy, Ubuntu服务器14.04)

Note/EDIT 1: Running the command in the terminal with the same file provides a perfect result.

注意/编辑1:在具有相同文件的终端中运行命令将提供完美的结果。

EDIT 2: Added example TIFF file here

编辑2:在这里添加示例TIFF文件

1 个解决方案

#1


3  

I'm not sure if this is an answer. For now it is pure speculation. So here goes...

我不确定这是否是一个答案。目前这纯粹是猜测。这里是……

By setting the limits to a 0 value, you are basically telling ImageMagick: "Your resources are not limited at all. You do not need to care for any limits."

通过将限制设置为0值,基本上可以告诉ImageMagick:“您的资源根本不受限制。你不需要关心任何限制。

  • What if didn't set any limit? Remove all -limit ... 0 parts from your command. In this case ImageMagick would use its built-in defaults, or the otherise defined settings (which may be contained in the policy.xml file of your IM installation, or through various environment variables). You can query the current limits of your system with the following command:

    如果没有设置任何限制呢?移除所有限制……0部分从你的命令。在本例中,ImageMagick将使用其内置的默认值,或者其他定义的设置(可能包含在策略中)。您的IM安装的xml文件,或通过各种环境变量)。您可以使用以下命令查询系统的当前限制:

    identify -list resource
    

    On my system, I get these values:

    在我的系统中,我得到了这些值:

    File       Area     Memory     Map       Disk  Thread  Throttle        Time
    ---------------------------------------------------------------------------
     192    4.295GB       2GiB    4GiB  unlimited       1         0   unlimited
    
  • What if you did set these limits to a reasonable value, that matches your system's really available resources? Assuming you have: 8 GByte of RAM, 50 GByte of free disk space and plenty of free inodes on your disk volume. Then try to set it like this: -limit disk 10GB -limit memory 3GB -limit map 6GB.

    如果您确实将这些限制设置为一个合理的值,该值与系统的真正可用资源相匹配,该怎么办?假设您有:8 GByte of RAM, 50 GByte of free disk space和磁盘卷上大量的free inode。然后尝试这样设置:-limit disk 10GB -limit memory 3GB -limit map 6GB。


ImageMagick resource management

For all its processing and intermediate steps, ImageMagick needs access to an intermediate pixel cache memory/storage, before it can deliver the final result.

对于所有的处理和中间步骤,ImageMagick需要访问一个中间像素缓存内存/存储,然后才能交付最终结果。

This need for pixel cache storage can be satisfied by different resources:

这种对像素缓存的需求可以通过不同的资源得到满足:

  • heap memory,
  • 堆内存,
  • anonymous memory map,
  • 匿名内存映射,
  • disk-based memory map,
  • 基于磁盘的内存映射,
  • direct disk.
  • 直接的磁盘。

ImageMagick makes use of all these resources progressively:

ImageMagick逐步利用这些资源:

  • Once heap memory is exhausted, it stores pixels in an anonymous map.
  • 一旦堆内存耗尽,它就在匿名映射中存储像素。
  • Once the anonymous memory map is exhausted, it creates the pixel cache on disk and attempts to memory-map it.
  • 一旦匿名内存映射耗尽,它将在磁盘上创建像素缓存并尝试内存映射它。
  • Once memory-map memory is exhausted, it simply uses standard disk I/O.
  • 一旦内存映射内存耗尽,它只使用标准磁盘I/O。

Disk storage is cheap but very slow too: it is in the order of 3 magnitudes (a thousand times) slower than memory. Some speed improvements (up to 5 times) can be obtained by using memory mapping to the disk-based cache.

磁盘存储很便宜,但速度也很慢:它比内存慢3个量级(1000倍)。通过使用到基于磁盘的缓存的内存映射,可以获得一些速度改进(最多5次)。

ImageMagick is aware of various ways to control the amount of these resources:

ImageMagick了解控制这些资源数量的各种方法:

  1. Built-in default values. These limits are: 768 files, 3GB of image area, 1.5GiB memory, 3GiB memory map, and 18.45EB of disk space.

    内置的默认值。这些限制是:768个文件、3GB的图像区域、1.5 gb的内存、3GiB的内存映射和18.45EB的磁盘空间。

  2. policy.xml config file. Please look up what's in your own policy.xml file. Use convert -list policy to find the location of this file first. Then use cat /some/path/policy.xml to see its contents. (The file uses an XML syntax. Don't forget: anything enclosed in <!-- and --> is a comment!) It also contains comments explaining various details. The policy.xml can define much more things than just the available limit resources. Settings in policy.xml take precedence over the built-in default values if they are defined there.

    政策。xml配置文件。请查一下你自己的保险单。xml文件。使用转换列表策略首先查找文件的位置。然后使用cat /一些/道路/政策。xml以查看其内容。(该文件使用XML语法。不要忘记:任何包含在 是一个评论!它还包含解释各种细节的评论。这一政策。xml可以定义的东西远不止可用的有限资源。设置在政策。如果在那里定义了xml,那么它优先于内置的默认值。

  3. Environment variables. Here is a list of environment variables which can limit IM resources: MAGICK_AREA_LIMIT (image area limits), MAGICK_DISK_LIMIT (disk space limit), MAGICK_FILE_LIMIT (maximum no. of open files limit), MAGICK_MEMORY_LIMIT (heap memory limit), MAGICK_MAP_LIMIT (memory map limit), MAGICK_THREAD_LIMIT (maximum no. of threads limit) and MAGICK_TIME_LIMIT (maximum elapsed time in seconds). These environment variables, if set, take precedence over the policy.xml config file.

    环境变量。下面是可以限制IM资源的环境变量列表:MAGICK_AREA_LIMIT(图像区域限制)、MAGICK_DISK_LIMIT(磁盘空间限制)、MAGICK_FILE_LIMIT(最大编号)。对于打开的文件限制)、MAGICK_MEMORY_LIMIT(堆内存限制)、MAGICK_MAP_LIMIT(内存映射限制)、MAGICK_THREAD_LIMIT(最大no)。(线程限制)和MAGICK_TIME_LIMIT(秒内最大消耗时间)。如果设置这些环境变量,则优先于策略。xml配置文件。

  4. -limit <name> <value> settings on command line. The following <names> are recognized:

    -limit <值> 设置在命令行。>的名称识别如下:

    • width (maximum width of an image). When limit is exceeded, exception is thrown and processing stops.
    • 宽度(图像的最大宽度)。当超过限制时,抛出异常并停止处理。
    • height (maximum height of an image). When limit is exceeded, exception is thrown and processing stops.
    • 高度(图像的最大高度)。当超过限制时,抛出异常并停止处理。
    • area (maximum number of bytes for any single image to reside in pixel cache memory). When limit is exceeded, automagical caching to disk (possibly memory-mapped) sets in.
    • 区域(每个图像驻留在像素缓存中的最大字节数)。当超出限制时,自动缓存到磁盘(可能是内存映射)。
    • memory (maximum memory allocated for the pixel cache from anonymous mapped memory or heap).
    • 内存(从匿名映射内存或堆中为像素缓存分配的最大内存)。
    • map (maximum amount for memory map allocated for pixel cache).
    • 映射(为像素缓存分配的内存映射的最大数量)。
    • disk (maximum amount of disk space permitted for use by pixel cache). When limit is exceeded, pixel cache is not created and a fatal exception is thrown.
    • 磁盘(像素缓存允许使用的最大磁盘空间)。当超过限制时,不会创建像素缓存并抛出致命异常。
    • files (maximum number of open pixel cache files). When limit is exceeded, all subsequent pixels cached to disk are closed and reopened on demand.
    • 文件(打开像素缓存文件的最大数量)。当超过限制时,缓存到磁盘的所有后续像素将被关闭并按需重新打开。
    • thread (maximum number of threads which can in parallel).
    • 线程(可并行执行的最大线程数)。
    • time (maximum time in seconds a process is permitted to execute). When this limit is exeeded, an exception is thrown and processing stops.
    • 时间(允许进程执行的最大时间(以秒为单位)。当执行此限制时,抛出异常并停止处理。

    The -limit setting on a command line takes precendence and overrides all other settings.

    命令行上的-limit设置具有优势并覆盖所有其他设置。

#1


3  

I'm not sure if this is an answer. For now it is pure speculation. So here goes...

我不确定这是否是一个答案。目前这纯粹是猜测。这里是……

By setting the limits to a 0 value, you are basically telling ImageMagick: "Your resources are not limited at all. You do not need to care for any limits."

通过将限制设置为0值,基本上可以告诉ImageMagick:“您的资源根本不受限制。你不需要关心任何限制。

  • What if didn't set any limit? Remove all -limit ... 0 parts from your command. In this case ImageMagick would use its built-in defaults, or the otherise defined settings (which may be contained in the policy.xml file of your IM installation, or through various environment variables). You can query the current limits of your system with the following command:

    如果没有设置任何限制呢?移除所有限制……0部分从你的命令。在本例中,ImageMagick将使用其内置的默认值,或者其他定义的设置(可能包含在策略中)。您的IM安装的xml文件,或通过各种环境变量)。您可以使用以下命令查询系统的当前限制:

    identify -list resource
    

    On my system, I get these values:

    在我的系统中,我得到了这些值:

    File       Area     Memory     Map       Disk  Thread  Throttle        Time
    ---------------------------------------------------------------------------
     192    4.295GB       2GiB    4GiB  unlimited       1         0   unlimited
    
  • What if you did set these limits to a reasonable value, that matches your system's really available resources? Assuming you have: 8 GByte of RAM, 50 GByte of free disk space and plenty of free inodes on your disk volume. Then try to set it like this: -limit disk 10GB -limit memory 3GB -limit map 6GB.

    如果您确实将这些限制设置为一个合理的值,该值与系统的真正可用资源相匹配,该怎么办?假设您有:8 GByte of RAM, 50 GByte of free disk space和磁盘卷上大量的free inode。然后尝试这样设置:-limit disk 10GB -limit memory 3GB -limit map 6GB。


ImageMagick resource management

For all its processing and intermediate steps, ImageMagick needs access to an intermediate pixel cache memory/storage, before it can deliver the final result.

对于所有的处理和中间步骤,ImageMagick需要访问一个中间像素缓存内存/存储,然后才能交付最终结果。

This need for pixel cache storage can be satisfied by different resources:

这种对像素缓存的需求可以通过不同的资源得到满足:

  • heap memory,
  • 堆内存,
  • anonymous memory map,
  • 匿名内存映射,
  • disk-based memory map,
  • 基于磁盘的内存映射,
  • direct disk.
  • 直接的磁盘。

ImageMagick makes use of all these resources progressively:

ImageMagick逐步利用这些资源:

  • Once heap memory is exhausted, it stores pixels in an anonymous map.
  • 一旦堆内存耗尽,它就在匿名映射中存储像素。
  • Once the anonymous memory map is exhausted, it creates the pixel cache on disk and attempts to memory-map it.
  • 一旦匿名内存映射耗尽,它将在磁盘上创建像素缓存并尝试内存映射它。
  • Once memory-map memory is exhausted, it simply uses standard disk I/O.
  • 一旦内存映射内存耗尽,它只使用标准磁盘I/O。

Disk storage is cheap but very slow too: it is in the order of 3 magnitudes (a thousand times) slower than memory. Some speed improvements (up to 5 times) can be obtained by using memory mapping to the disk-based cache.

磁盘存储很便宜,但速度也很慢:它比内存慢3个量级(1000倍)。通过使用到基于磁盘的缓存的内存映射,可以获得一些速度改进(最多5次)。

ImageMagick is aware of various ways to control the amount of these resources:

ImageMagick了解控制这些资源数量的各种方法:

  1. Built-in default values. These limits are: 768 files, 3GB of image area, 1.5GiB memory, 3GiB memory map, and 18.45EB of disk space.

    内置的默认值。这些限制是:768个文件、3GB的图像区域、1.5 gb的内存、3GiB的内存映射和18.45EB的磁盘空间。

  2. policy.xml config file. Please look up what's in your own policy.xml file. Use convert -list policy to find the location of this file first. Then use cat /some/path/policy.xml to see its contents. (The file uses an XML syntax. Don't forget: anything enclosed in <!-- and --> is a comment!) It also contains comments explaining various details. The policy.xml can define much more things than just the available limit resources. Settings in policy.xml take precedence over the built-in default values if they are defined there.

    政策。xml配置文件。请查一下你自己的保险单。xml文件。使用转换列表策略首先查找文件的位置。然后使用cat /一些/道路/政策。xml以查看其内容。(该文件使用XML语法。不要忘记:任何包含在 是一个评论!它还包含解释各种细节的评论。这一政策。xml可以定义的东西远不止可用的有限资源。设置在政策。如果在那里定义了xml,那么它优先于内置的默认值。

  3. Environment variables. Here is a list of environment variables which can limit IM resources: MAGICK_AREA_LIMIT (image area limits), MAGICK_DISK_LIMIT (disk space limit), MAGICK_FILE_LIMIT (maximum no. of open files limit), MAGICK_MEMORY_LIMIT (heap memory limit), MAGICK_MAP_LIMIT (memory map limit), MAGICK_THREAD_LIMIT (maximum no. of threads limit) and MAGICK_TIME_LIMIT (maximum elapsed time in seconds). These environment variables, if set, take precedence over the policy.xml config file.

    环境变量。下面是可以限制IM资源的环境变量列表:MAGICK_AREA_LIMIT(图像区域限制)、MAGICK_DISK_LIMIT(磁盘空间限制)、MAGICK_FILE_LIMIT(最大编号)。对于打开的文件限制)、MAGICK_MEMORY_LIMIT(堆内存限制)、MAGICK_MAP_LIMIT(内存映射限制)、MAGICK_THREAD_LIMIT(最大no)。(线程限制)和MAGICK_TIME_LIMIT(秒内最大消耗时间)。如果设置这些环境变量,则优先于策略。xml配置文件。

  4. -limit <name> <value> settings on command line. The following <names> are recognized:

    -limit <值> 设置在命令行。>的名称识别如下:

    • width (maximum width of an image). When limit is exceeded, exception is thrown and processing stops.
    • 宽度(图像的最大宽度)。当超过限制时,抛出异常并停止处理。
    • height (maximum height of an image). When limit is exceeded, exception is thrown and processing stops.
    • 高度(图像的最大高度)。当超过限制时,抛出异常并停止处理。
    • area (maximum number of bytes for any single image to reside in pixel cache memory). When limit is exceeded, automagical caching to disk (possibly memory-mapped) sets in.
    • 区域(每个图像驻留在像素缓存中的最大字节数)。当超出限制时,自动缓存到磁盘(可能是内存映射)。
    • memory (maximum memory allocated for the pixel cache from anonymous mapped memory or heap).
    • 内存(从匿名映射内存或堆中为像素缓存分配的最大内存)。
    • map (maximum amount for memory map allocated for pixel cache).
    • 映射(为像素缓存分配的内存映射的最大数量)。
    • disk (maximum amount of disk space permitted for use by pixel cache). When limit is exceeded, pixel cache is not created and a fatal exception is thrown.
    • 磁盘(像素缓存允许使用的最大磁盘空间)。当超过限制时,不会创建像素缓存并抛出致命异常。
    • files (maximum number of open pixel cache files). When limit is exceeded, all subsequent pixels cached to disk are closed and reopened on demand.
    • 文件(打开像素缓存文件的最大数量)。当超过限制时,缓存到磁盘的所有后续像素将被关闭并按需重新打开。
    • thread (maximum number of threads which can in parallel).
    • 线程(可并行执行的最大线程数)。
    • time (maximum time in seconds a process is permitted to execute). When this limit is exeeded, an exception is thrown and processing stops.
    • 时间(允许进程执行的最大时间(以秒为单位)。当执行此限制时,抛出异常并停止处理。

    The -limit setting on a command line takes precendence and overrides all other settings.

    命令行上的-limit设置具有优势并覆盖所有其他设置。