@@ -164,29 +164,25 @@ def _to_numpy(self):
164
164
self .img = to_numpy (self .img ).astype (np .float64 )
165
165
self .shape = self .img .shape
166
166
167
- def _to_imagemagick (self ):
167
+ def _to_imagemagick (self , bits = 16 ):
168
168
'''Convert from numpy to PMImage.
169
169
'''
170
- self .img = to_imagemagick (self .img )
170
+ self .img = to_imagemagick (self .img , bits = bits )
171
171
172
- def save (self , fname , bits = 16 , scale = True , enhancements = None ):
172
+ def save (self , fname , bits = 16 , enhancements = None ):
173
173
'''Save the image data.
174
174
175
175
:param fname: output filename
176
176
:type fname: str
177
177
:param bits: output bit-depth
178
178
:type bits: int
179
- :param scale: scale image values to cover *bits* bit-depth
180
- :type scale: boolean
181
179
:param enhancements: image processing applied to the image before saving
182
180
:type enhancements: dictionary or None
183
181
'''
184
182
185
183
if enhancements :
186
184
self .enhance (enhancements )
187
- if scale :
188
- self ._scale (bits )
189
- self ._to_imagemagick ()
185
+ self ._to_imagemagick (bits = bits )
190
186
self .img .write (fname )
191
187
192
188
def min (self ):
@@ -581,23 +577,6 @@ def _gradient_fit_surface(self, x_pts, y_pts, order=2, chan=None):
581
577
order = order )
582
578
return polyval2d (x_locs , y_locs , poly ).T
583
579
584
- def _scale (self , * args ):
585
- '''Scale image to cover the whole bit-range.
586
- '''
587
-
588
- self ._to_numpy ()
589
- LOGGER .info ("Scaling image to %d bits." , args [0 ])
590
-
591
- img = self .img - self .img .min ()
592
- img_max = np .max (img )
593
- if img_max != 0 :
594
- img = (2 ** args [0 ] - 1 ) * img / img_max
595
-
596
- if args [0 ] <= 8 :
597
- self .img = img .astype ('uint8' )
598
- else :
599
- self .img = img .astype ('uint16' )
600
-
601
580
602
581
def _rotate (self , * args ):
603
582
'''Rotate image.
@@ -805,7 +784,7 @@ def to_numpy(img):
805
784
806
785
return img
807
786
808
- def to_imagemagick (img ):
787
+ def to_imagemagick (img , bits = 16 ):
809
788
'''Convert numpy array to Imagemagick format.
810
789
811
790
:param img: image to convert
@@ -814,6 +793,25 @@ def to_imagemagick(img):
814
793
'''
815
794
816
795
if not isinstance (img , PMImage ):
796
+
797
+ def _scale (img , bits = 16 ):
798
+ '''Scale image to cover the whole bit-range.
799
+ '''
800
+
801
+ LOGGER .info ("Scaling image to %d bits." , bits )
802
+
803
+ img -= img .min ()
804
+ img_max = np .max (img )
805
+ if img_max != 0 :
806
+ img = (2 ** bits - 1 ) * img / img_max
807
+
808
+ if bits <= 8 :
809
+ return img .astype ('uint8' )
810
+ else :
811
+ return img .astype ('uint16' )
812
+
813
+ img = _scale (img , bits = bits )
814
+
817
815
LOGGER .debug ("Converting from Numpy to ImageMagick." )
818
816
out_img = PMImage ()
819
817
if img .dtype == np .uint8 :
0 commit comments