简体   繁体   中英

SimpleITK: tif to numpy array and back to tif makes file size bigger

I am using simpleITK on python 2.7 Anaconda, I did a test reading a big tif file (around 2Gb), converting it to numpy array, converting it back to tif and saving on disk, the new file is around 4Gb (I tried 2 different ways of saving it, but results are the same). The data type is the same at each step, any idea why is it happening ? Thanks !. :)

import SimpleITK as sitk

p1 = sitk.ReadImage('my_image.tif') #read image
# print properties
print 'Width ',p1.GetWidth()
print 'Height ',p1.GetHeight()
print 'Depth ',p1.GetDepth()
print 'Dimension ',p1.GetDimension()
print 'Pixel Id Value ',p1.GetPixelIDValue()
print 'Pixel ID Type ',p1.GetPixelIDTypeAsString()
print 'Size ',p1.GetSize()
print '__________ '
p1_np = sitk.GetArrayFromImage(p1) #get numpy array from Image
print 'Array type ',p1_np.dtype  #print array type
print '__________ '
p1_np_img = sitk.GetImageFromArray(p1_np) #get image from array
# print properties
print 'Width ',p1_np_img.GetWidth()
print 'Height ',p1_np_img.GetHeight()
print 'Depth ',p1_np_img.GetDepth()
print 'Dimension ',p1_np_img.GetDimension()
print 'Pixel Id Value ',p1_np_img.GetPixelIDValue()
print 'Pixel ID Type ',p1_np_img.GetPixelIDTypeAsString()
print 'Size ',p1_np_img.GetSize()

sitk.WriteImage(p1_np_img,'my_image_test_a.tif') #save new image 
sitk.WriteImage(sitk.Cast(p1_np_img,sitk.sitkUInt16),'my_image_test_b.tif') #save new image

print "End "

Results are like this:

在此处输入图片说明

The original images are compressed (LZW compression), and simpleITK upload them and save them with their original size. I used the option 'useCompression'=True in WriteImage, but the images get almost the same size 3.8 Gb. Therefore I need to use a compression method outside SimpleITK which exceeds the discussion in this post. Anyway, thank you for your comments. WriteImage(Image image, std::string const & fileName, bool useCompression=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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