【问题标题】:Image Cropping Tool (Python)图像裁剪工具 (Python)
【发布时间】:2014-09-06 23:41:22
【问题描述】:

我是一名电影摄影师,经常处理裁剪/图像大小调整。因为我拍摄胶片,我必须扫描我的底片并从批量扫描中裁剪出每一帧。我的扫描仪扫描四条,每条六张图像(每次扫描 24 帧/裁剪)。

我的一个朋友给我写了一个 Python 脚本,它可以根据输入的坐标自动裁剪图像。该脚本运行良好,但导出图像的文件格式存在问题。

从扫描结果来看,每帧都应生成 240 DPI 的 37mb TIFF(当我在 Adob​​e Lightroom 中裁剪和导出时)。相反,Cropper 输出 13mb 72 DPI TIFF。

每当我运行 Cropper 时,终端(我在 Mac 上)都会警告我“减压炸弹”。我的朋友被难住了,建议我问 Stack Overflow。

我没有 Python 经验。我可以提供他写的代码和终端给我的命令。

想法? 这将不胜感激,并且可以节省大量时间。 谢谢!

ERROR MESSAGE: /Library/Python/2.7/site-packages/PIL/Image.py:2192: DecompressionBombWarning: Image size (208560540 pixels) exceeds limit of 89478485 pixels, could be decompression bomb DOS attack.

【问题讨论】:

    标签: python image python-imaging-library tiff pillow


    【解决方案1】:

    PIL 只是想保护您。它不会打开更大的图像,因为这可能是恶意用户的攻击媒介,为您提供一个大图像,该图像会扩展以耗尽所有内存。引用自PIL.Image.open() documentation

    警告:为了防止由“decompression bombs”引起的潜在 DOS 攻击(即解压成大量数据的恶意文件,旨在通过大量使用而导致崩溃或中断内存),如果图像超过一定限制,Pillow 将发出DecompressionBombWarning

    由于您不是恶意用户并且不接受其他人的图片,您可以简单地禁用限制:

    from PIL import Image
    
    Image.MAX_IMAGE_PIXELS = None
    

    设置Image.MAX_IMAGE_PIXELS 完全禁用检查。您还可以将其设置为(高)整数值;默认为1024 * 1024 * 1024 // 4 // 3,接近9000万像素或3通道图像约250MB的未压缩数据。

    请注意,对于最高 4.3.0 的 PIL 版本,默认情况下会发出 警告。您也可以禁用警告:

    import warnings
    from PIL import Image
    
    warnings.simplefilter('ignore', Image.DecompressionBombWarning)
    

    反之,如果您想完全阻止此类图像被加载,请将警告变成异常:

    import warnings
    from PIL import Image
    
    warnings.simplefilter('error', Image.DecompressionBombWarning)
    

    然后,您可以期望 Image.DecompressionBombWarning 对象在您传入图像时作为异常引发,否则会需要大量内存。

    截至PIL v5.0.0(2018 年 1 月发布),使用 两倍 像素数作为 MAX_IMAGE_PIXELS 值的图像将导致 PIL.Image.DecompressionBombError 异常。

    请注意,这些检查也适用于Image.crop() operation(您可以通过裁剪创建更大图像),如果您需要使用 PIL 版本 6.2.0 or newer(2019 年 10 月发布)希望在处理 GIF 或 ICO 文件时受益于这种保护。

    【讨论】:

    【解决方案2】:

    来自Pillow docs

    警告: 防止由“decompression bombs”引起的潜在 DOS 攻击(即解压成大量数据的恶意文件,旨在通过大量使用而导致崩溃或中断内存),如果图像超过一定限制,Pillow 将发出DecompressionBombWarning。如果需要,可以使用warnings.simplefilter('error', Image.DecompressionBombWarning) 将警告转换为错误,或者使用warnings.simplefilter('ignore', Image.DecompressionBombWarning) 完全抑制该警告。另请参阅 the logging documentation 以将警告输出到日志记录工具而不是 stderr。

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多