使用OpenCV保存TIFF时,文件大小超出预期 [英] Larger than expected file sizes when saving a TIFF with OpenCV

查看:763
本文介绍了使用OpenCV保存TIFF时,文件大小超出预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个python程序来加载.tiff图像,从图像中裁剪选择,并将选择另存为tiff.数据集图像很大,超过1GB.我可以成功裁剪出所需的内容并将其保存为tiff,但是新的图像文件大小比我的预期和需要大得多.

I am creating a python program to load a .tiff image, crop out a selection from the image, and save the selection as a tiff. The dataset images are large, exceeding 1GB. I can successfully crop out what I need and save as a tiff, but the new image file sizes are much larger than what I expect and need.

打开
我正在使用tifffile将图像作为numpy数组打开.由于大小,OpenCV和PIL无法打开文件.我也尝试使用OpenSlide,但是在使用read_region()时遇到了其他问题.

Opening
I am using tifffile to open the image as a numpy array. OpenCV and PIL were not able to open the files due to size. I tried using OpenSlide as well, but encountered other problems down the road with read_region().

裁剪
numpy数组的形状为(height, width, 3),因此我使用large_image[top:bottom, left:right, :]之类的方式进行裁剪.这可以按预期工作.

Cropping
The numpy array has the shape (height, width, 3), so I crop using something like large_image[top:bottom, left:right, :]. This works as intended.

保存
到目前为止,使用cv2.imwrite()可使文件最小,但它们仍然比应有的要大得多. PIL.Image.save()中的PIL.Image.save()TiffWriter创建了更大的图像.

Saving
Using cv2.imwrite() has resulted in the smallest file sizes thus far, but they are still much larger than they should be. PIL.Image.save() and TiffWriter from tifffile created even larger images.

最佳结果:从250MB文件中裁剪13张新图像-仅使用原始图像的20%-给我的文件总计超过900MB.我希望总数约为50MB.

Best results: Cropping 13 new images from a 250MB file - using only about 20% of the original image - gives me files totaling over 900MB. I would expect the total to be something like 50MB.

注意:裁剪的.tiff文件具有正确的尺寸.如果原始文件为200,000 x 50,000,则裁剪后的文件为8,000 x 3,000.另外,我无法在Mac上使用预览"来打开原始的250MB图像,但是当我用TiffWriter保存图像时,我可以快速打开由程序创建的500MB裁剪图像(我也可以打开用opencv保存的文件)

Note: The cropped .tiff files have correct dimensions. If the original is 200,000 x 50,000, then the cropped file will be, say, 8,000 x 3,000. Also, I am unable to open the original 250MB image using Preview on my Mac, but I can quickly open a 500MB cropped image created by my program when I save the image with TiffWriter (I can open files saved with opencv as well).

代码摘要:

import tifffile
import cv2
import numpy as np

original_image = tifffile.imread('filepath') #original_image is a numpy array

#...calculations for top, bottom, etc...

cropped_image = original_image[top:bottom, left:right, :]
cv2.imwrite('output_filepath', cropped_image)

这3行都是我使用的IO.

These 3 lines are all the IO that I use.

tl; dr-尝试加载图像,裁剪和将新图像另存为.tiff,但是新文件的大小比预期的大得多.

tl;dr - trying to load images, crop, and save new images as .tiff, but new file sizes are much larger than expected.

推荐答案

如果您使用的是Mac,自制软件很棒,您可以安装libtiff ImageMagick 与:

If you are on a Mac, homebrew is great and you can install libtiff and ImageMagick with:

brew install libtiff imagemagick

然后,您可以真正开始使用以下方法了解压缩率,位数/样本数和数据大小/类型:

Then you can really start to understand what compression, number of bits/sample and data sizes/types using:

tiffinfo YOURINPUTFILE.TIF
tiffinfo YOUROUTPUTFILE.TIF

和:

magick identify -verbose YOURINPUTFILE.TIF
magick identify -verbose YOUROUTPUTFILE.TIF

如果要同时看到两个,请使用:

If you want to see the two side-by-side, use:

magick identify -verbose YOURINPUTFILE.TIF  > a
magick identify -verbose YOUROUTPUTFILE.TIF > b
opendiff a b

这篇关于使用OpenCV保存TIFF时,文件大小超出预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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