Skip to content

Commit 100d52b

Browse files
committed
Fixed ImageMagick failures when applied in postprocessing.
1 parent 0027a86 commit 100d52b

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

halostack/image.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -164,29 +164,25 @@ def _to_numpy(self):
164164
self.img = to_numpy(self.img).astype(np.float64)
165165
self.shape = self.img.shape
166166

167-
def _to_imagemagick(self):
167+
def _to_imagemagick(self, bits=16):
168168
'''Convert from numpy to PMImage.
169169
'''
170-
self.img = to_imagemagick(self.img)
170+
self.img = to_imagemagick(self.img, bits=bits)
171171

172-
def save(self, fname, bits=16, scale=True, enhancements=None):
172+
def save(self, fname, bits=16, enhancements=None):
173173
'''Save the image data.
174174
175175
:param fname: output filename
176176
:type fname: str
177177
:param bits: output bit-depth
178178
:type bits: int
179-
:param scale: scale image values to cover *bits* bit-depth
180-
:type scale: boolean
181179
:param enhancements: image processing applied to the image before saving
182180
:type enhancements: dictionary or None
183181
'''
184182

185183
if enhancements:
186184
self.enhance(enhancements)
187-
if scale:
188-
self._scale(bits)
189-
self._to_imagemagick()
185+
self._to_imagemagick(bits=bits)
190186
self.img.write(fname)
191187

192188
def min(self):
@@ -581,23 +577,6 @@ def _gradient_fit_surface(self, x_pts, y_pts, order=2, chan=None):
581577
order=order)
582578
return polyval2d(x_locs, y_locs, poly).T
583579

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-
601580

602581
def _rotate(self, *args):
603582
'''Rotate image.
@@ -805,7 +784,7 @@ def to_numpy(img):
805784

806785
return img
807786

808-
def to_imagemagick(img):
787+
def to_imagemagick(img, bits=16):
809788
'''Convert numpy array to Imagemagick format.
810789
811790
:param img: image to convert
@@ -814,6 +793,25 @@ def to_imagemagick(img):
814793
'''
815794

816795
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+
817815
LOGGER.debug("Converting from Numpy to ImageMagick.")
818816
out_img = PMImage()
819817
if img.dtype == np.uint8:

0 commit comments

Comments
 (0)