Skip to content

Commit ae17d01

Browse files
authored
Fix hue adjustment (#4182)
* Fix hue adjustment Hue adjustment wasn't working correctly because color channels got swapped. This has now been fixed and we're using PIL rather than cv2 to do the RGBA->HSV->RGBA conversion. The range of hue adjustment is also the more typical 0..360 degrees.
1 parent d09dfc3 commit ae17d01

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

invokeai/app/invocations/image.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -661,27 +661,23 @@ class ImageHueAdjustmentInvocation(BaseInvocation):
661661

662662
# Inputs
663663
image: ImageField = Field(default=None, description="The image to adjust")
664-
hue: int = Field(default=0, description="The degrees by which to rotate the hue")
664+
hue: int = Field(default=0, description="The degrees by which to rotate the hue, 0-360")
665665
# fmt: on
666666

667667
def invoke(self, context: InvocationContext) -> ImageOutput:
668668
pil_image = context.services.images.get_pil_image(self.image.image_name)
669669

670-
# Convert PIL image to OpenCV format (numpy array), note color channel
671-
# ordering is changed from RGB to BGR
672-
image = numpy.array(pil_image.convert("RGB"))[:, :, ::-1]
673-
674670
# Convert image to HSV color space
675-
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
671+
hsv_image = numpy.array(pil_image.convert("HSV"))
676672

677-
# Adjust the hue
678-
hsv_image[:, :, 0] = (hsv_image[:, :, 0] + self.hue) % 180
673+
# Convert hue from 0..360 to 0..256
674+
hue = int(256 * ((self.hue % 360) / 360))
679675

680-
# Convert image back to BGR color space
681-
image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
676+
# Increment each hue and wrap around at 255
677+
hsv_image[:, :, 0] = (hsv_image[:, :, 0] + hue) % 256
682678

683679
# Convert back to PIL format and to original color mode
684-
pil_image = Image.fromarray(image[:, :, ::-1], "RGB").convert("RGBA")
680+
pil_image = Image.fromarray(hsv_image, mode="HSV").convert("RGBA")
685681

686682
image_dto = context.services.images.create(
687683
image=pil_image,

0 commit comments

Comments
 (0)