Larger than expected file sizes when saving a TIFF with OpenCV

。_饼干妹妹 提交于 2020-07-23 06:33:29

问题


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.

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().

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.

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.

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.

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

Summary of the code:

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)

These 3 lines are all the IO that I use.

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


回答1:


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

and:

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


来源:https://stackoverflow.com/questions/62857038/larger-than-expected-file-sizes-when-saving-a-tiff-with-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!