66from pathlib import Path
77import numpy as np
88from collections import OrderedDict
9+ from fractions import Fraction
910import PIL .Image as PilImage
1011import PIL .TiffImagePlugin as PilTiff
1112
@@ -182,8 +183,8 @@ def __create_tiff_tags(self):
182183 dpcm_width = 1 / (float (pixel_width ) * 100 )
183184 dpcm_height = 1 / (float (pixel_height ) * 100 )
184185
185- tiff_tags [PilTiff .X_RESOLUTION ] = int (dpcm_width )
186- tiff_tags [PilTiff .Y_RESOLUTION ] = int (dpcm_height )
186+ tiff_tags [PilTiff .X_RESOLUTION ] = Fraction ( int (dpcm_width ), 1 )
187+ tiff_tags [PilTiff .Y_RESOLUTION ] = Fraction ( int (dpcm_height ), 1 )
187188
188189 # Bit Depth & Color Interpretation
189190 bit_depth = metadata .get ("bit_depth" , 16 )
@@ -201,7 +202,7 @@ def save(self,
201202
202203 :param fn: Filepath
203204 :type fn: Path or str
204- :param bool thumbnail: Create a 512px-wide thumbnail, height is adjusted to keep the aspect ratio. Only for non-MRC formats
205+ :param bool thumbnail: Create a 512px-wide 8-bit thumbnail, height is adjusted to keep the aspect ratio. Only for non-MRC formats
205206 :param bool overwrite: Overwrite existing file
206207 """
207208 fn = os .path .abspath (fn )
@@ -219,11 +220,12 @@ def save(self,
219220 raise FileExistsError ("File %s already exists, use overwrite flag" % fn )
220221
221222 logging .getLogger ("PIL" ).setLevel (logging .INFO )
222- pil_image = PilImage . fromarray ( self .data .copy (), mode = 'I;16' )
223+ data = self .data .copy ()
223224
224225 if thumbnail :
225- # create a thumbnail
226- width , height = self .data .shape
226+ # create an 8-bit thumbnail
227+ pil_image = PilImage .fromarray (data , mode = 'L' )
228+ height , width = data .shape
227229 if width < height :
228230 width = max (round (width * 512 / height ), 1 )
229231 thumbnail_size = (width , 512 )
@@ -232,6 +234,8 @@ def save(self,
232234 thumbnail_size = (512 , height )
233235
234236 pil_image .thumbnail (size = thumbnail_size , resample = PilImage .Resampling .LANCZOS )
237+ else :
238+ pil_image = PilImage .fromarray (data , mode = 'I;16' )
235239
236240 # create tiff tags
237241 if ext in [".tif" , ".tiff" ] and not thumbnail :
0 commit comments