From 6c8f3a726b4dd5ddca6077dbee00f64c124be034 Mon Sep 17 00:00:00 2001 From: "TamorStewartSr." <134035795+TamorStewartSr@users.noreply.github.com> Date: Fri, 11 Apr 2025 18:47:36 -0400 Subject: [PATCH 01/14] [Term entry] .point() #6518 --- .../concepts/image/terms/point/point.md | 63 ++++++++++++++++++ media/inverted-gradient.png | Bin 0 -> 177 bytes 2 files changed, 63 insertions(+) create mode 100644 content/pillow/concepts/image/terms/point/point.md create mode 100644 media/inverted-gradient.png diff --git a/content/pillow/concepts/image/terms/point/point.md b/content/pillow/concepts/image/terms/point/point.md new file mode 100644 index 00000000000..1a97a24bbf7 --- /dev/null +++ b/content/pillow/concepts/image/terms/point/point.md @@ -0,0 +1,63 @@ +--- +Title: 'point()' +Description: 'Applies a function or lookup table to each pixel in an image using the Pillow library.' +Subjects: + - 'Python' + - 'Image Processing' + - 'Pillow' +Tags: + - 'Image-manipulation' + - 'Pillow' + - 'point()' + - 'Python-method' +CatalogContent: + - 'Learn-python-3' + - 'paths/data-science' +--- + +The **`.point()`** method in the Python Pillow library is used to apply a function or lookup table to each pixel in an image. It is useful for performing operations like thresholding, gamma correction, or channel manipulation. + +## Syntax + +```python +Image.point(function, mode=None) +``` + +**Parameters:** + +* `function` (callable or sequence): +A function or sequence that maps each pixel value to a new value. If a function is used, it's called once per pixel. +* `mode`(optional string): +A mode string (like "L" or "RGB") that defines the mode of the output image. This is rarely needed unless changing the image mode. + +**Returns:** +* A new `image` object with the transformed pixel data. + + +## Example + +```py +from PIL import Image + +# Create a 4x4 grayscale image +image = Image.new("L", (4, 4)) +pixels = [i * 16 for i in range(16)] # Gradient values +image.putdata(pixels) + +# Invert grayscale values using point() +inverted_image = image.point(lambda p: 255 - p) + +# Save the result +inverted_image.save("inverted-image.png") +``` +![Inverted Grayscale Image](/codecademyDocs/media/inverted-image.png) + + + + + + + + + + diff --git a/media/inverted-gradient.png b/media/inverted-gradient.png new file mode 100644 index 0000000000000000000000000000000000000000..9ba960f7c5a4358530c2a0cf155d5a87cb6426f1 GIT binary patch literal 177 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5j5t8#v^!BNfK-L2i(^Q|oVOPo1sN204j3HR z|6EbI-;}Fr@8&z!r%fH$Gk7mBMlqK(Tw{=3Kt*ot)>Z#gES$G-gWTfj>gTe~DWM4f DVPrM? literal 0 HcmV?d00001 From 70bfeb835a3cb3e2b709946f2d7fcb073b70d87b Mon Sep 17 00:00:00 2001 From: "TamorStewartSr." <134035795+TamorStewartSr@users.noreply.github.com> Date: Fri, 11 Apr 2025 19:42:35 -0400 Subject: [PATCH 02/14] [Edit] .point() #6518 --- .../terms/point/media/inverted-gradient.png | Bin 0 -> 177 bytes .../concepts/image/terms/point/point.md | 34 +++++++++--------- 2 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 content/pillow/concepts/image/terms/point/media/inverted-gradient.png diff --git a/content/pillow/concepts/image/terms/point/media/inverted-gradient.png b/content/pillow/concepts/image/terms/point/media/inverted-gradient.png new file mode 100644 index 0000000000000000000000000000000000000000..9ba960f7c5a4358530c2a0cf155d5a87cb6426f1 GIT binary patch literal 177 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5j5t8#v^!BNfK-L2i(^Q|oVOPo1sN204j3HR z|6EbI-;}Fr@8&z!r%fH$Gk7mBMlqK(Tw{=3Kt*ot)>Z#gES$G-gWTfj>gTe~DWM4f DVPrM? literal 0 HcmV?d00001 diff --git a/content/pillow/concepts/image/terms/point/point.md b/content/pillow/concepts/image/terms/point/point.md index 1a97a24bbf7..7a9bf498ddc 100644 --- a/content/pillow/concepts/image/terms/point/point.md +++ b/content/pillow/concepts/image/terms/point/point.md @@ -1,5 +1,5 @@ --- -Title: 'point()' +Title: '.point()' Description: 'Applies a function or lookup table to each pixel in an image using the Pillow library.' Subjects: - 'Python' @@ -25,34 +25,36 @@ Image.point(function, mode=None) **Parameters:** -* `function` (callable or sequence): -A function or sequence that maps each pixel value to a new value. If a function is used, it's called once per pixel. -* `mode`(optional string): -A mode string (like "L" or "RGB") that defines the mode of the output image. This is rarely needed unless changing the image mode. +- `function` *(callable or sequence)*: A function or lookup table that maps each pixel value to a new value... + +- `mode` *(optional, str)*: + The mode of the output image. Typically not needed unless changing image type. **Returns:** * A new `image` object with the transformed pixel data. ## Example +This example creates a horizontal grayscale gradient, inverts it using `.point()`, and saves the result: ```py from PIL import Image -# Create a 4x4 grayscale image -image = Image.new("L", (4, 4)) -pixels = [i * 16 for i in range(16)] # Gradient values -image.putdata(pixels) +# Create a horizontal grayscale gradient image +width, height = 256, 50 +image = Image.new("L", (width, height)) +for x in range(width): + for y in range(height): + image.putpixel((x, y), x) -# Invert grayscale values using point() -inverted_image = image.point(lambda p: 255 - p) +# Invert grayscale values +inverted = image.point(lambda p: 255 - p) -# Save the result -inverted_image.save("inverted-image.png") +# Save +inverted.save("inverted-gradient.png") ``` -![Inverted Grayscale Image](/codecademyDocs/media/inverted-image.png) - - +## Output: +![Output image ](/media/inverted-gradient.png) From 007c43f9ed1b38599761719fb3b4d99f7fa10936 Mon Sep 17 00:00:00 2001 From: "TamorStewartSr." <134035795+TamorStewartSr@users.noreply.github.com> Date: Fri, 11 Apr 2025 20:20:05 -0400 Subject: [PATCH 03/14] [Edit] .point() #6518 Edited tags and subjects --- content/pillow/concepts/image/terms/point/point.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/content/pillow/concepts/image/terms/point/point.md b/content/pillow/concepts/image/terms/point/point.md index 7a9bf498ddc..25aa292ee33 100644 --- a/content/pillow/concepts/image/terms/point/point.md +++ b/content/pillow/concepts/image/terms/point/point.md @@ -2,14 +2,12 @@ Title: '.point()' Description: 'Applies a function or lookup table to each pixel in an image using the Pillow library.' Subjects: - - 'Python' - - 'Image Processing' - - 'Pillow' + - 'Computer Science' + - 'Data Science' Tags: - - 'Image-manipulation' + - 'Images' - 'Pillow' - - 'point()' - - 'Python-method' + - 'Python' CatalogContent: - 'Learn-python-3' - 'paths/data-science' From b40cf88224d409e19dd00b6f51c833118d535bf2 Mon Sep 17 00:00:00 2001 From: "TamorStewartSr." <134035795+TamorStewartSr@users.noreply.github.com> Date: Mon, 14 Apr 2025 12:00:26 -0400 Subject: [PATCH 04/14] [Edit] .point() #6518 Co-authored-by: Mamta Wardhani --- content/pillow/concepts/image/terms/point/point.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pillow/concepts/image/terms/point/point.md b/content/pillow/concepts/image/terms/point/point.md index 25aa292ee33..d5c2df3d0d8 100644 --- a/content/pillow/concepts/image/terms/point/point.md +++ b/content/pillow/concepts/image/terms/point/point.md @@ -9,7 +9,7 @@ Tags: - 'Pillow' - 'Python' CatalogContent: - - 'Learn-python-3' + - 'learn-python-3' - 'paths/data-science' --- From 025924041a0784827dfce5bf894eb28ef73b375f Mon Sep 17 00:00:00 2001 From: "TamorStewartSr." <134035795+TamorStewartSr@users.noreply.github.com> Date: Mon, 14 Apr 2025 12:00:46 -0400 Subject: [PATCH 05/14] [Edit] .point() #6518 Co-authored-by: Mamta Wardhani --- content/pillow/concepts/image/terms/point/point.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pillow/concepts/image/terms/point/point.md b/content/pillow/concepts/image/terms/point/point.md index d5c2df3d0d8..e885e3d10d6 100644 --- a/content/pillow/concepts/image/terms/point/point.md +++ b/content/pillow/concepts/image/terms/point/point.md @@ -17,7 +17,7 @@ The **`.point()`** method in the Python Pillow library is used to apply a functi ## Syntax -```python +```pseudo Image.point(function, mode=None) ``` From 1df97f1ddedc080f12ace206e69fae472aefa7f0 Mon Sep 17 00:00:00 2001 From: "TamorStewartSr." <134035795+TamorStewartSr@users.noreply.github.com> Date: Mon, 14 Apr 2025 12:01:02 -0400 Subject: [PATCH 06/14] [Edit] .point() #6518 Co-authored-by: Mamta Wardhani --- content/pillow/concepts/image/terms/point/point.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pillow/concepts/image/terms/point/point.md b/content/pillow/concepts/image/terms/point/point.md index e885e3d10d6..5f58771b875 100644 --- a/content/pillow/concepts/image/terms/point/point.md +++ b/content/pillow/concepts/image/terms/point/point.md @@ -18,7 +18,7 @@ The **`.point()`** method in the Python Pillow library is used to apply a functi ## Syntax ```pseudo -Image.point(function, mode=None) +Image.point(lut, mode=None) ``` **Parameters:** From c9676714db01cdb537359796354955f929963e78 Mon Sep 17 00:00:00 2001 From: "TamorStewartSr." <134035795+TamorStewartSr@users.noreply.github.com> Date: Mon, 14 Apr 2025 12:01:30 -0400 Subject: [PATCH 07/14] [Edit] .point() #6518 Co-authored-by: Mamta Wardhani --- content/pillow/concepts/image/terms/point/point.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/content/pillow/concepts/image/terms/point/point.md b/content/pillow/concepts/image/terms/point/point.md index 5f58771b875..10ac59671a1 100644 --- a/content/pillow/concepts/image/terms/point/point.md +++ b/content/pillow/concepts/image/terms/point/point.md @@ -23,7 +23,9 @@ Image.point(lut, mode=None) **Parameters:** -- `function` *(callable or sequence)*: A function or lookup table that maps each pixel value to a new value... +- `lut`: This parameter can be: + - A lookup table: A list or sequence with 256 values (or 65536 for 16-bit images) per band. + - A function: Takes a single integer (0–255) and returns a value. It’s called once for each possible pixel value to build a lookup table internally. - `mode` *(optional, str)*: The mode of the output image. Typically not needed unless changing image type. From 8185b307585d7b497e6b5027876d2797638d5ee5 Mon Sep 17 00:00:00 2001 From: "TamorStewartSr." <134035795+TamorStewartSr@users.noreply.github.com> Date: Mon, 14 Apr 2025 12:02:48 -0400 Subject: [PATCH 08/14] [Edit] .point() #6518 Co-authored-by: Mamta Wardhani --- content/pillow/concepts/image/terms/point/point.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pillow/concepts/image/terms/point/point.md b/content/pillow/concepts/image/terms/point/point.md index 10ac59671a1..a7ea8468ff5 100644 --- a/content/pillow/concepts/image/terms/point/point.md +++ b/content/pillow/concepts/image/terms/point/point.md @@ -30,7 +30,7 @@ Image.point(lut, mode=None) - `mode` *(optional, str)*: The mode of the output image. Typically not needed unless changing image type. -**Returns:** +**Returns value:** * A new `image` object with the transformed pixel data. From 870a88dd1efef650bbf0c90232fb48eaed5e6e4b Mon Sep 17 00:00:00 2001 From: "TamorStewartSr." <134035795+TamorStewartSr@users.noreply.github.com> Date: Mon, 14 Apr 2025 12:03:17 -0400 Subject: [PATCH 09/14] [Edit] .point() #6518 Co-authored-by: Mamta Wardhani --- content/pillow/concepts/image/terms/point/point.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/pillow/concepts/image/terms/point/point.md b/content/pillow/concepts/image/terms/point/point.md index a7ea8468ff5..c930358c3e6 100644 --- a/content/pillow/concepts/image/terms/point/point.md +++ b/content/pillow/concepts/image/terms/point/point.md @@ -44,8 +44,8 @@ from PIL import Image width, height = 256, 50 image = Image.new("L", (width, height)) for x in range(width): - for y in range(height): - image.putpixel((x, y), x) + for y in range(height): + image.putpixel((x, y), x) # Invert grayscale values inverted = image.point(lambda p: 255 - p) From 77d4557c095e6a1e106c9fa1f6b69c97891f8bcb Mon Sep 17 00:00:00 2001 From: "TamorStewartSr." <134035795+TamorStewartSr@users.noreply.github.com> Date: Mon, 14 Apr 2025 12:03:31 -0400 Subject: [PATCH 10/14] [Edit] .point() #6518 Co-authored-by: Mamta Wardhani --- content/pillow/concepts/image/terms/point/point.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/pillow/concepts/image/terms/point/point.md b/content/pillow/concepts/image/terms/point/point.md index c930358c3e6..0f8d846634d 100644 --- a/content/pillow/concepts/image/terms/point/point.md +++ b/content/pillow/concepts/image/terms/point/point.md @@ -54,7 +54,7 @@ inverted = image.point(lambda p: 255 - p) inverted.save("inverted-gradient.png") ``` ## Output: -![Output image ](/media/inverted-gradient.png) +![Output image](https://raw.githubusercontent.com/Codecademy/docs/main/media/inverted-gradient.png) From e8dfb4d5711b414559bb877e9d56f1c0eb68e603 Mon Sep 17 00:00:00 2001 From: "TamorStewartSr." <134035795+TamorStewartSr@users.noreply.github.com> Date: Mon, 14 Apr 2025 12:21:05 -0400 Subject: [PATCH 11/14] [Edit] .point() #6518 Specified Modes --- content/pillow/concepts/image/terms/point/point.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/content/pillow/concepts/image/terms/point/point.md b/content/pillow/concepts/image/terms/point/point.md index 10ac59671a1..a3ecffb78b4 100644 --- a/content/pillow/concepts/image/terms/point/point.md +++ b/content/pillow/concepts/image/terms/point/point.md @@ -28,7 +28,15 @@ Image.point(lut, mode=None) - A function: Takes a single integer (0–255) and returns a value. It’s called once for each possible pixel value to build a lookup table internally. - `mode` *(optional, str)*: - The mode of the output image. Typically not needed unless changing image type. +The mode of the output image. Use this if you want to change the image type during transformation. + Common modes include: + - `"L"`: 8-bit pixels, black and white (grayscale) + - `"RGB"`: 3x8-bit pixels, true color + - `"RGBA"`: 4x8-bit pixels, true color with transparency + - `"1"`: 1-bit pixels, black and white, stored with one pixel per byte + - `"P"`: 8-bit pixels, mapped to any other mode using a color palette + + ⚠️ *Most uses of `.point()` don’t require this argument unless you’re explicitly changing image type (e.g., converting grayscale to binary).* **Returns:** * A new `image` object with the transformed pixel data. From 6ba47c5cc8bfd45751fd1aa95424a2c465210000 Mon Sep 17 00:00:00 2001 From: TamorStewartSr <134035795+TamorStewartSr@users.noreply.github.com> Date: Tue, 15 Apr 2025 12:03:51 -0400 Subject: [PATCH 12/14] Deleted the media folder --- .../image/terms/point/media/inverted-gradient.png | Bin 177 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 content/pillow/concepts/image/terms/point/media/inverted-gradient.png diff --git a/content/pillow/concepts/image/terms/point/media/inverted-gradient.png b/content/pillow/concepts/image/terms/point/media/inverted-gradient.png deleted file mode 100644 index 9ba960f7c5a4358530c2a0cf155d5a87cb6426f1..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 177 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K5j5t8#v^!BNfK-L2i(^Q|oVOPo1sN204j3HR z|6EbI-;}Fr@8&z!r%fH$Gk7mBMlqK(Tw{=3Kt*ot)>Z#gES$G-gWTfj>gTe~DWM4f DVPrM? From d3cc0a432738518b63862fb348923663aae88125 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 19 Apr 2025 16:22:05 +0530 Subject: [PATCH 13/14] Update point.md --- .../concepts/image/terms/point/point.md | 28 +++++++------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/content/pillow/concepts/image/terms/point/point.md b/content/pillow/concepts/image/terms/point/point.md index db2c31f5b67..94e4a39b636 100644 --- a/content/pillow/concepts/image/terms/point/point.md +++ b/content/pillow/concepts/image/terms/point/point.md @@ -24,25 +24,23 @@ Image.point(lut, mode=None) **Parameters:** - `lut`: This parameter can be: - - A lookup table: A list or sequence with 256 values (or 65536 for 16-bit images) per band. + - A lookup table: A list or sequence with 256 values for 8-bit images per band (e.g., 768 values for RGB). For 16-bit images, 65536 values are required per band. - A function: Takes a single integer (0–255) and returns a value. It’s called once for each possible pixel value to build a lookup table internally. - -- `mode` *(optional, str)*: -The mode of the output image. Use this if you want to change the image type during transformation. - Common modes include: +- `mode` (optional, str): The mode of the output image. Use this if you want to change the image type during transformation. Common modes include: - `"L"`: 8-bit pixels, black and white (grayscale) - `"RGB"`: 3x8-bit pixels, true color - - `"RGBA"`: 4x8-bit pixels, true color with transparency - - `"1"`: 1-bit pixels, black and white, stored with one pixel per byte + - `"RGBA"`: 4x8-bit pixels, true color with alpha channel + - `"1"`: 1-bit pixels, black and white - `"P"`: 8-bit pixels, mapped to any other mode using a color palette - ⚠️ *Most uses of `.point()` don’t require this argument unless you’re explicitly changing image type (e.g., converting grayscale to binary).* +> **Note:** Most uses of `.point()` don’t require the argument `mode` unless there is a need to explicitly change the image type (e.g., converting grayscale to binary). -**Returns value:** -* A new `image` object with the transformed pixel data. +**Return value:** +A new `Image` object with the transformed pixel data. ## Example + This example creates a horizontal grayscale gradient, inverts it using `.point()`, and saves the result: ```py @@ -61,13 +59,7 @@ inverted = image.point(lambda p: 255 - p) # Save inverted.save("inverted-gradient.png") ``` -## Output: -![Output image](https://raw.githubusercontent.com/Codecademy/docs/main/media/inverted-gradient.png) - - - - - - +This example generates the output as follows: +![Output image containing horizontal grayscale gradient](https://raw.githubusercontent.com/Codecademy/docs/main/media/inverted-gradient.png) From 15d9e98c2fc0b8431d7e52916f1fc665955525d3 Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Fri, 25 Apr 2025 12:42:03 +0530 Subject: [PATCH 14/14] Minor changes --- .../pillow/concepts/image/terms/point/point.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/content/pillow/concepts/image/terms/point/point.md b/content/pillow/concepts/image/terms/point/point.md index 94e4a39b636..d7bfda28b88 100644 --- a/content/pillow/concepts/image/terms/point/point.md +++ b/content/pillow/concepts/image/terms/point/point.md @@ -1,11 +1,11 @@ --- Title: '.point()' -Description: 'Applies a function or lookup table to each pixel in an image using the Pillow library.' +Description: 'Applies a function or lookup table to each pixel in an image.' Subjects: - 'Computer Science' - 'Data Science' Tags: - - 'Images' + - 'Images' - 'Pillow' - 'Python' CatalogContent: @@ -13,7 +13,7 @@ CatalogContent: - 'paths/data-science' --- -The **`.point()`** method in the Python Pillow library is used to apply a function or lookup table to each pixel in an image. It is useful for performing operations like thresholding, gamma correction, or channel manipulation. +In Pillow, the **`.point()`** method is used to apply a function or lookup table to each pixel in an image. It is useful for performing operations like thresholding, gamma correction, or channel manipulation. ## Syntax @@ -31,13 +31,13 @@ Image.point(lut, mode=None) - `"RGB"`: 3x8-bit pixels, true color - `"RGBA"`: 4x8-bit pixels, true color with alpha channel - `"1"`: 1-bit pixels, black and white - - `"P"`: 8-bit pixels, mapped to any other mode using a color palette + - `"P"`: 8-bit pixels, uses a color palette to map to other modes > **Note:** Most uses of `.point()` don’t require the argument `mode` unless there is a need to explicitly change the image type (e.g., converting grayscale to binary). **Return value:** -A new `Image` object with the transformed pixel data. +This method returns a new `Image` object with the transformed pixel data. ## Example @@ -53,13 +53,13 @@ for x in range(width): for y in range(height): image.putpixel((x, y), x) -# Invert grayscale values +# Invert grayscale values using .point() inverted = image.point(lambda p: 255 - p) -# Save +# Save the result inverted.save("inverted-gradient.png") ``` -This example generates the output as follows: +The output for the example will be: -![Output image containing horizontal grayscale gradient](https://raw.githubusercontent.com/Codecademy/docs/main/media/inverted-gradient.png) +![A horizontal grayscale gradient image with inverted grayscale values](https://raw.githubusercontent.com/Codecademy/docs/main/media/inverted-gradient.png)