Why Does Pillow's .convert('L') Not Modify Images In-Place and How to Get a 2D Grayscale Numpy Arra
Автор: vlogommentary
Загружено: 2025-12-23
Просмотров: 0
Learn why Pillow's image conversion methods return new images instead of modifying in-place, and how to properly convert images to grayscale or black & white before converting to numpy arrays.
---
This video is based on the question https://stackoverflow.com/q/79477454/ asked by the user 'Sun Bear' ( https://stackoverflow.com/u/5722359/ ) and on the answer https://stackoverflow.com/a/79477513/ provided by the user 'chrslg' ( https://stackoverflow.com/u/20037042/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Why is the greyscale & B&W images created by Pillow giving a numpy.ndarray that has a 3D shape?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to drop me a comment under this video.
---
Understanding Pillow’s .convert() Behavior
When working with Pillow (PIL) to convert images to grayscale or black & white, a common pitfall is expecting .convert() to change the original image object directly.
The Core Issue
Pillow's Image.convert() method does not modify the original image. Instead, it returns a new image object with the requested mode. If you call img.convert('L') without capturing the result, the original image remains unchanged.
Example mistake:
[[See Video to Reveal this Text or Code Snippet]]
Here, image_pil retains the 3-channel RGB shape because img was never replaced.
Correct Usage to Get a 2D Grayscale Numpy Array
To obtain a 2D numpy array representing a grayscale image:
Use .convert('L') and assign its result to a variable.
Convert that new image to a numpy array.
[[See Video to Reveal this Text or Code Snippet]]
What about .convert('1')?
L mode: 8-bit pixels, black and white
1 mode: 1-bit pixels, black and white, stored with 1 bit per pixel
The same principle applies: you must assign the converted image returned from .convert('1') to a variable.
Summary
Pillow's .convert() returns a new image; it does not alter the original image in-place.
Always assign the result of .convert() if you want the converted image.
Converting with .convert('L') or .convert('1') gives you single-channel images.
Converting those images to a numpy array yields a 2D array, reflecting greyscale or black & white pixels.
Correcting your code this way ensures you get the expected 2D numpy array shape instead of a 3D RGB array.
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: