简体   繁体   English

图像裁剪工具 (Python)

[英]Image Cropping Tool (Python)

I'm a film photographer who deals a lot with cropping/image resizing.我是一名电影摄影师,经常处理裁剪/图像大小调整。 Because I shoot film, I have to scan my negatives and crop each frame out of the batch scan.因为我拍摄胶片,我必须扫描我的底片并从批量扫描中裁剪每一帧。 My scanner scans four strips of six images each (24 frames/crops per scan).我的扫描仪扫描四个条带,每条六幅图像(每次扫描 24 帧/裁剪)。

A friend of mine wrote me a script for Python that automatically crops images based on inputted coordinates.我的一个朋友给我写了一个 Python 脚本,它根据输入的坐标自动裁剪图像。 The script works well but it has problems in the file format of the exported images.该脚本运行良好,但导出图像的文件格式存在问题。

From the scan, each frame should produce a 37mb TIFF at 240 DPI (when I crop and export in Adobe Lightroom).从扫描来看,每帧都应该在 240 DPI 下产生一个 37mb 的 TIFF(当我在 Adob​​e Lightroom 中裁剪和导出时)。 Instead, the Cropper outputs a 13mb 72 DPI TIFF.相反,Cropper 输出 13mb 72 DPI TIFF。

Terminal (I'm on Mac) warns me about a "Decompression Bomb" whenever I run the Cropper.每当我运行 Cropper 时,终端(我在 Mac 上)都会警告我“减压炸弹”。 My friend is stumped and suggested I ask Stack Overflow.我的朋友被难住了,建议我问 Stack Overflow。

I've no Python experience.我没有 Python 经验。 I can provide the code he wrote and the commands Terminal gives me.我可以提供他写的代码和终端给我的命令。

Thoughts?想法? This would be greatly appreciated and a huge HUGE timesaver.这将不胜感激,并且可以节省大量时间。 THANK YOU!谢谢你!

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.

PIL is merely trying to protect you. PIL 只是想保护您。 It'll not open larger images, as that could be a vector of attack for a malicious user to give you a large image that'll expand to use up all memory.它不会打开较大的图像,因为这可能是恶意用户的攻击媒介,可以为您提供会扩展以耗尽所有内存的大图像。 Quoting from the PIL.Image.open() documentation :引用PIL.Image.open()文档

Warning : To protect against potential DOS attacks caused by “ decompression bombs ” (ie malicious files which decompress into a huge amount of data and are designed to crash or cause disruption by using up a lot of memory), Pillow will issue a DecompressionBombWarning if the image is over a certain limit.警告:为了防止“解压炸弹”(即解压成大量数据的恶意文件,旨在通过使用大量内存而崩溃或造成中断)引起的潜在 DOS 攻击,Pillow 将发出DecompressionBombWarning如果图像超过一定限度。

Since you are not a malicious user and are not accepting images from anyone else, you can simply disable the limit:由于您不是恶意用户并且不接受来自其他任何人的图像,您可以简单地禁用限制:

from PIL import Image

Image.MAX_IMAGE_PIXELS = None

Setting Image.MAX_IMAGE_PIXELS disables the check altogether.设置Image.MAX_IMAGE_PIXELS会完全禁用检查。 You can also set it to a (high) integer value;您还可以将其设置为(高)整数值; the default is 1024 * 1024 * 1024 // 4 // 3 , nearly 90 million pixels or about a 250MB uncompressed data for a 3-channel image.默认为1024 * 1024 * 1024 // 4 // 3 ,近 9000 万像素或大约 250MB 的 3 通道图像未压缩数据。

Note that for PIL versions up to 4.3.0, by default, all that happens is that a warning is issued.请注意,对于高达 4.3.0 的 PIL 版本,默认情况下,发生的只是发出警告 You could also disable the warning:您还可以禁用警告:

import warnings
from PIL import Image

warnings.simplefilter('ignore', Image.DecompressionBombWarning)

Inversely, if you want to prevent such images from being loaded altogether, turn the warning into an exception:相反,如果您想完全阻止加载此类图像,请将警告转换为异常:

import warnings
from PIL import Image

warnings.simplefilter('error', Image.DecompressionBombWarning)

and you can then expect the Image.DecompressionBombWarning object to be raised as an exception whenever you pass an image in that would otherwise demand a lot of memory.然后您可以期望Image.DecompressionBombWarning对象在您传递图像时作为异常引发,否则将需要大量内存。

As of PIL v5.0.0 (released Jan 2018), images that use twice the number of pixels as the MAX_IMAGE_PIXELS value will result in a PIL.Image.DecompressionBombError exception.PIL v5.0.0 (2018 年 1 月发布)开始,使用两倍MAX_IMAGE_PIXELS值的像素数的图像将导致PIL.Image.DecompressionBombError异常。

Note that these checks also apply to the Image.crop() operation (you can create a larger image by cropping), and you need to use PIL version 6.2.0 or newer (released in October 2019) if you want to benefit from this protection when working with GIF or ICO files.请注意,这些检查也适用于Image.crop()操作(您可以通过裁剪创建更大的图像),如果您想从中受益,您需要使用 PIL 6.2.0 或更高版本(2019 年 10 月发布)使用 GIF 或 ICO 文件时的保护。

From the Pillow docs :枕头文档

Warning: To protect against potential DOS attacks caused by " decompression bombs " (ie malicious files which decompress into a huge amount of data and are designed to crash or cause disruption by using up a lot of memory), Pillow will issue a DecompressionBombWarning if the image is over a certain limit.警告:为了防止“解压炸弹”(即解压成大量数据的恶意文件,旨在通过使用大量内存而崩溃或造成中断)引起的潜在 DOS 攻击,Pillow 将发出DecompressionBombWarning如果图像超过一定限度。 If desired, the warning can be turned into an error with warnings.simplefilter('error', Image.DecompressionBombWarning) or suppressed entirely with warnings.simplefilter('ignore', Image.DecompressionBombWarning) .如果需要,可以使用warnings.simplefilter('error', Image.DecompressionBombWarning)将警告转换为错误,或使用warnings.simplefilter('ignore', Image.DecompressionBombWarning)完全抑制。 See also the logging documentation to have warnings output to the logging facility instead of stderr.另请参阅日志记录文档以将警告输出到日志记录工具而不是 stderr。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM