-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Neon and sepia filters #9341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Neon and sepia filters #9341
Conversation
for more information, see https://pre-commit.ci
|
Very cool, thank you! |
|
I've created matheusmpff#1 with some suggestions. |
| image = image.convert("L") | ||
|
|
||
| Kx = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]] | ||
| Ky = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just want to confirm - you're sure the Ky values are correct? Looking at https://en.wikipedia.org/wiki/Sobel_operator#Formulation, one might expect that Ky should actually be [[-1, -2, -1], [0, 0, 0], [1, 2, 1]]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had used an inverted kernel for the Sobel operator in the y direction. The right one is the [[-1, -2, -1], [0, 0, 0], [1, 2, 1]]. But I think in the implementation we use the absolute values of gy so I think the results will be the same. It should be great to change for the correct one
Simplified code
| :param image: Image to create the effect | ||
| :param color: RGB color used for neon effect | ||
| :alpha: controls the intensity of the neon effect |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| :alpha: controls the intensity of the neon effect | |
| :param alpha: Controls the intensity of the neon effect. If alpha is 0.0, a copy of | |
| the image is returned unaltered. |
| This function computes the Sobel gradient magnitude using the | ||
| horizontal (Gx) and vertical (Gy) Sobel kernels. | ||
| :param image: the image to apply the filter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| :param image: the image to apply the filter | |
| :param image: The image to be filtered |
| for y in range(1, image.height - 1): | ||
| for x in range(1, image.width - 1): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| for y in range(1, image.height - 1): | |
| for x in range(1, image.width - 1): | |
| for x in range(1, image.width - 1): | |
| for y in range(1, image.height - 1): |
Nitpick: I think it would be nice if the order of the loops matched the order in sepia()
| def test_sepia_preserves_size_and_mode() -> None: | ||
| img = Image.new("RGB", (10, 10), (100, 150, 200)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def test_sepia_preserves_size_and_mode() -> None: | |
| img = Image.new("RGB", (10, 10), (100, 150, 200)) | |
| @pytest.mark.parametrize("mode", ("L", "RGB")) | |
| def test_sepia_size_and_mode(mode: str) -> None: | |
| img = Image.new(mode, (10, 10)) |
Tests sepia() with a non-RGB image.
Description:
This PR introduces two new image processing functions to the
PIL.ImageOpsmodule:sepiaApplies a classic sepia tone effect to RGB images.
Converts the image to RGB if necessary.
Computes new pixel values using the standard sepia formula and clamps results to [0, 255].
Preserves the original image size and mode.
Practical use: adds a warm, vintage look to images with minimal processing overhead.
neon_effectApplies a neon/glow effect to an image.
Internally uses a
Sobel edge-detection filter, Gaussian blur, and colorization.Combines the neon layer with the original image using alpha blending.
Supports RGB images and customizable neon color.
Practical use: highlights edges and details in a visually striking, glowing style.
Benefits:
Expands Pillow’s “ready-made” filters with two highly requested effects.
Provides developers with visually appealing filters without third-party dependencies.
Results
Image after sepia effect:

Image after neon-glow effect:

Notes
There are tests for all functions created in this PR covering the main basic behaviors