在Linux上使用ImageMagick命令行进行批量转换的结果中断 [英] Broken results on batch convert with ImageMagick command line on Linux

查看:163
本文介绍了在Linux上使用ImageMagick命令行进行批量转换的结果中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的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

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

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.

IM运行时没有错误。

I got no errors from IM running this.

但是如果我用PHP运行这个命令来执行大约13000个文件 - 我有时会遇到这些错误:

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

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

但更大的问题是:部分图片坏了下面左边是坏图像,右边是好图像(ondrag /在深色背景上你看到问题更好):

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):


手动运行命令后,结果还可以。仅在运行此PHP循环脚本时才会提供损坏的结果。 ( PHP循环脚本

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

我以这种方式运行脚本: php55 run.php 。一个带有的简单循环找到作为shell脚本提供相同的结果。

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

所以我搜索了,在IM话语服务器中询问并在具有不同发行版的两台计算机上运行此过程(Debian Wheezy,Ubuntu Server 14.04)

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)

注意/编辑1:在具有相同文件的终端提供了完美的结果。

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

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

EDIT 2: Added example TIFF file here

推荐答案

我不确定是否这是一个答案。现在这是纯粹的猜测。所以这里... ...

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

通过将限制设置为 0 值,您基本上是在告诉ImageMagick:您的资源根本不受限制。您无需关心任何限制。

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."


  • 如果没有设置任何限制怎么办? 从命令中删除所有 -limit ... 0 部分。在这种情况下,ImageMagick将使用其内置默认值或其他定义的设置(可能包含在IM安装的 policy.xml 文件中,或通过各种环境变量)。您可以使用以下命令查询系统的当前限制:

  • 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:

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


  • 如果您将这些限制设置为一个合理的值,它与您系统真正可用的资源相匹配? 假设你有:8 GB的RAM,50 GB的可用磁盘空间和磁盘上充足的免费 inode 卷。然后尝试设置如下: -limit disk 10GB -limit memory 3GB -limit map 6GB

  • 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.

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

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

    这种对像素缓存存储的需求可以通过不同的资源:

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


    • 堆内存,

    • 匿名内存映射,

    • 基于磁盘的内存映射,

    • 直接磁盘。

    • heap memory,
    • anonymous memory map,
    • disk-based memory map,
    • direct disk.

    ImageMagick使用所有这些资源的逐步增加:

    ImageMagick makes use of all these resources progressively:


    • 一旦堆内存耗尽,它会将像素存储在匿名地图中。

    • 一旦匿名内存映射耗尽,它就会在磁盘上创建像素缓存并尝试对其进行内存映射。

    • 内存映射内存耗尽后,它只使用标准磁盘I / O.

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

    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.

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

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


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

    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.

    policy.xml 配置文件 。请查看您自己的 policy.xml 文件中的内容。使用 convert -list policy 首先查找此文件的位置。然后使用 cat /some/path/policy.xml 查看其内容。 (该文件使用XML语法。不要忘记:<! - - > 是评论!)它还包含解释各种细节的评论。 policy.xml 可以定义比可用限制资源更多的东西。 policy.xml 中的设置优先于内置默认值(如果已在此处定义)。

    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.

    环境变量 。以下是可以限制IM资源的环境变量列表: MAGICK_AREA_LIMIT (图像区域限制), MAGICK_DISK_LIMIT (磁盘空间)限制), MAGICK_FILE_LIMIT (最大打开文件数限制), MAGICK_MEMORY_LIMIT (堆内存限制), MAGICK_MAP_LIMIT (内存映射限制), MAGICK_THREAD_LIMIT (最大线程数限制)和 MAGICK_TIME_LIMIT (以秒为单位的最长经过时间)。这些环境变量(如果已设置)优先于 policy.xml 配置文件。

    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.

    -limit< name> < value> 命令行上的设置 。识别以下< names>

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


    • width (图像的最大宽度)。超过限制时,抛出异常并停止处理。

    • height (图像的最大高度)。超出限制时,抛出异常并停止处理。

    • area (任何单个图像驻留在像素中的最大字节数高速缓存存储器)。超出限制时,自动缓存到磁盘(可能是内存映射)设置为。

    • memory (为像素分配的最大内存)从匿名映射内存或堆缓存)。

    • map (为像素缓存分配的内存映射的最大金额)。

    • disk (允许像素缓存使用的最大磁盘空间量)。超出限制时,像素缓存创建并抛出致命异常。

    • files (最大打开像素缓存文件数)。超过限制时,缓存到磁盘的所有后续像素都将关闭并按需重新打开。

    • thread (最大线程数可以并行)。

    • time (允许进程执行的最长时间(以秒为单位))。当执行此限制时,抛出异常并停止处理。

    • 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.

    -limit 在命令行上设置优先并覆盖所有其他设置。

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

    这篇关于在Linux上使用ImageMagick命令行进行批量转换的结果中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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