Skip to content

Fix adjust_hue to preserve alpha channel on RGBA images - #9691

Open
Aks27-hub wants to merge 1 commit into
pytorch:mainfrom
Aks27-hub:fix-preserve-alpha-adjust-hue
Open

Aks27-hub wants to merge 1 commit into
pytorch:mainfrom
Aks27-hub:fix-preserve-alpha-adjust-hue

Conversation

@Aks27-hub

Copy link
Copy Markdown

Summary

transforms.functional.adjust_hue (via _functional_pil.adjust_hue) converts
the input image to PIL's HSV mode before shifting the hue channel. Since
HSV mode has no alpha band, any alpha channel present on the input is lost
and silently reset to fully opaque (255) once the result is converted back
to the original mode.

Fixes #3219

Changes

  • In torchvision/transforms/_functional_pil.py::adjust_hue, extract the alpha channel (if present) before converting to HSV, perform the hue shift on the RGB data only, then reapply the original alpha channel via putalpha before converting back to the input mode.
  • Updated the adjust_hue docstring to note that the alpha channel is preserved if present.
  • No behavior change for images without an alpha channel (L, RGB, etc.).
  • Added a test asserting alpha values are unchanged after adjust_hue for an RGBA input across several hue factors.

Testing

  • Ran pytest test/test_transforms.py -vvv -k test_adjust_hue_rgba_preserves_alpha locally - all passing.
  • Verified manually with a sample RGBA PNG that alpha values are identical before and after adjust_hue for factors in [-0.5, 0.5].

## Changes
- In `torchvision/transforms/functional_pil.py::adjust_hue`, extract the
  alpha channel (if present) before converting to `HSV`, perform the hue
  shift on the RGB data only, then reapply the original alpha channel via
  `putalpha` before converting back to the input mode.
- Updated the `adjust_hue` docstring to note that the alpha channel is
  preserved if present.
- No behavior change for images without an alpha channel (L, RGB, etc.).
- Added a test asserting alpha values are unchanged after `adjust_hue` for
  an RGBA input across several hue factors.

## Testing
- Ran `pytest test/test_transforms.py -vvv -k test_adjust_hue_rgba_preserves_alpha` locally — all passing.
- Verified manually with a sample RGBA PNG that alpha values are identical
  before and after `adjust_hue` for factors in [-0.5, 0.5].
@pytorch-bot

pytorch-bot Bot commented Oct 1, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9691

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Oct 1, 2026

Copy link
Copy Markdown

Hi @Aks27-hub!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the cla signed label Oct 1, 2026
@meta-cla

meta-cla Bot commented Oct 1, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

torchvision.transforms.functional.adjust_hue does not respect alpha channel

1 participant