Skip to content

Commit

Permalink
Negate opacity channel on ImageMagick 7
Browse files Browse the repository at this point in the history
In ImageMagick 7, the semantics of the opacity channel is flipped.
  • Loading branch information
quantum5 committed Apr 24, 2022
1 parent ffb9c5e commit 7863775
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions win2xcur/shadow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

if 'copy_opacity' in COMPOSITE_OPERATORS:
COPY_ALPHA = 'copy_opacity' # ImageMagick 6 name
NEEDS_NEGATE = False
else:
COPY_ALPHA = 'copy_alpha' # ImageMagick 7 name
NEEDS_NEGATE = True


def apply_to_image(image: BaseImage, *, color: str, radius: float, sigma: float, xoffset: float,
Expand All @@ -18,8 +20,14 @@ def apply_to_image(image: BaseImage, *, color: str, radius: float, sigma: float,
new_width = image.width + 3 * xoffset
new_height = image.height + 3 * yoffset

if NEEDS_NEGATE:
channel = image.channel_images['opacity'].clone()
channel.negate()
else:
channel = image.channel_images['opacity']

opacity = Image(width=new_width, height=new_height, pseudo='xc:white')
opacity.composite(image.channel_images['opacity'], left=xoffset, top=yoffset)
opacity.composite(channel, left=xoffset, top=yoffset)
opacity.gaussian_blur(radius * image.width, sigma * image.width)
opacity.negate()
opacity.modulate(50)
Expand All @@ -28,8 +36,8 @@ def apply_to_image(image: BaseImage, *, color: str, radius: float, sigma: float,
shadow.composite(opacity, operator=COPY_ALPHA)

result = Image(width=new_width, height=new_height, pseudo='xc:transparent')
result.composite(shadow)
result.composite(image)
result.composite(shadow, operator='difference')

trimmed = result.clone()
trimmed.trim(color=Color('transparent'))
Expand Down

0 comments on commit 7863775

Please sign in to comment.