Option to directly invert the grayscale heatmap

Theoretically less work inverting the image while it's small but I can't measure a significant difference. Though, handy option to have in some cases.
This commit is contained in:
spezialspezial 2022-11-01 21:11:19 +01:00 committed by Lincoln Stein
parent 611a3a9753
commit bbf4c03e50

View File

@ -43,8 +43,8 @@ class SegmentedGrayscale(object):
self.heatmap = heatmap self.heatmap = heatmap
self.image = image self.image = image
def to_grayscale(self)->Image: def to_grayscale(self,invert:bool=False)->Image:
return self._rescale(Image.fromarray(np.uint8(self.heatmap*255))) return self._rescale(Image.fromarray(np.uint8((255 if invert else 0) - self.heatmap * 255)))
def to_mask(self,threshold:float=0.5)->Image: def to_mask(self,threshold:float=0.5)->Image:
discrete_heatmap = self.heatmap.lt(threshold).int() discrete_heatmap = self.heatmap.lt(threshold).int()
@ -52,11 +52,9 @@ class SegmentedGrayscale(object):
def to_transparent(self,invert:bool=False)->Image: def to_transparent(self,invert:bool=False)->Image:
transparent_image = self.image.copy() transparent_image = self.image.copy()
gs = self.to_grayscale()
# The following line looks like a bug, but isn't.
# For img2img, we want the selected regions to be transparent, # For img2img, we want the selected regions to be transparent,
# but to_grayscale() returns the opposite. # but to_grayscale() returns the opposite. Thus invert.
gs = ImageOps.invert(gs) if not invert else gs gs = self.to_grayscale(invert)
transparent_image.putalpha(gs) transparent_image.putalpha(gs)
return transparent_image return transparent_image