IMAGE_COLOR_INVERSION


The Python program featured in this tutorial traverses every pixel inside of a (rectangular) image file and inverts the red-green-blue (RGB) values of that pixel.

In the words of ChatGPT-5.2,”This program deterministically transforms a raster image by loading it from a specified filesystem location, validating that the file exists, and normalizing it into RGB color space. It then traverses the image exhaustively at the pixel level, replacing each red, green, and blue component with its arithmetic complement relative to the fixed 8-bit maximum value (255), thereby producing a complete color inversion of the original data. The process is fully explicit, non-destructive with respect to the source file, and concludes by writing the transformed image to a new output file while emitting minimal diagnostic messages to document execution state and outcome.”

To view hidden text inside each of the preformatted text boxes below, scroll horizontally.

# STEP_0: Obtain input image.
image = Image.open(input_image_path)
image = image.convert("RGB")
pixels = image.load()

#STEP_1: Iterate across each pixel in image...
for y in range(height):
  for x in range(width):
    r, g, b = pixels[x, y]
    #...and invert its RGB values.
    inverted_r = 255 - r
    inverted_g = 255 - g
    inverted_b = 255 - b
    pixels[x, y] = (inverted_r, inverted_g, inverted_b)

# STEP_2: Generate output image.
image.save(output_image_path)

SOFTWARE_APPLICATION_COMPONENTS


python_source_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_50/main/invert_image_rgb.py

input_image_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_50/main/abstract_drawing_karbytes_26december2025_p0.jpg

input_image_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_50/main/input_image_inverted.png


PROGRAM_INTERPRETATION_AND_EXECUTION


STEP_0: Copy and paste the Python source code into a new text editor document and save that document as the following file name:

invert_image_rgb.py

STEP_1: Open a Unix command line terminal application and set the current directory to wherever the Python program file is located on the local machine (e.g. Desktop).

cd Desktop

STEP_2: Run the program by entering the following command:

python3 invert_image_rgb.py

STEP_3: If the program interpretation command does not work, then use the following commands (in top-down order) to install the Python interpreter:

sudo apt update
sudo apt install python3

STEP_4: Observe program results on the command line terminal and in the output file.


PROGRAM_SOURCE_CODE


Note that the text inside of each of the the preformatted text boxes below appears on this web page (while rendered correctly by the web browser) to be identical to the content of that preformatted text box text’s respective plain-text file or source code output file (whose Uniform Resource Locator is displayed as the green hyperlink immediately above that preformatted text box (if that hyperlink points to a source code file) or whose Uniform Resource Locator is displayed as the orange hyperlink immediately above that preformatted text box (if that hyperlink points to a plain-text file)).

(Note that angle brackets which resemble HTML tags (i.e. an “is less than” symbol (i.e. ‘<‘) followed by an “is greater than” symbol (i.e. ‘>’)) displayed on this web page have been replaced (at the source code level of this web page) with the Unicode symbols U+003C (which is rendered by the web browser as ‘<‘) and U+003E (which is rendered by the web browser as ‘>’). That is because the WordPress web page editor or web browser interprets a plain-text version of an “is less than” symbol followed by an “is greater than” symbol as being an opening HTML tag (which means that the WordPress web page editor or web browser deletes or fails to display those (plain-text) inequality symbols and the content between those (plain-text) inequality symbols)).

python_source_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_50/main/invert_image_rgb.py


#########################################################################################
# file: invert_image_rgb.py
# type: Python
# date: 30_DECEMBER_2025
# author: karbytes 
# license: PUBLIC_DOMAIN
#########################################################################################

from PIL import Image
import os

# Define the only function in this program.
def invert_image_rgb(input_image_path, output_image_path):

    print("\n\n--------------------------------")
    print("\n\nThis Python program inverts the RGB color values of each pixel in an image.")
    print("\n\n--------------------------------")

    # Verify that the input file exists.
    if not os.path.isfile(input_image_path):
        print(f"\n\nERROR: Input image not found: {input_image_path}")
        print("\n\n--------------------------------\n\n")
        return

    # Open the image.
    image = Image.open(input_image_path)

    # Ensure the image is in RGB mode.
    image = image.convert("RGB")

    width, height = image.size
    pixels = image.load()

    print(f"\n\nProcessing image: {input_image_path}")
    print(f"Image size: {width} x {height}")

    # Iterate over every pixel.
    for y in range(height):
        for x in range(width):
            r, g, b = pixels[x, y]

            # Invert RGB values.
            inverted_r = 255 - r
            inverted_g = 255 - g
            inverted_b = 255 - b

            pixels[x, y] = (inverted_r, inverted_g, inverted_b)
            #end of nested for loop definition
    
    # Save the modified image.
    image.save(output_image_path)

    print(f"\n\nInverted image written to: {output_image_path}")
    print("\n\n--------------------------------\n\n")
    # end of function definition

# Set file paths.
input_image_path = "abstract_drawing_karbytes_26december2025_p0.jpg"      # Replace with your actual image path.
output_image_path = "input_image_inverted.png"

# Execute the function defined in this program file.
invert_image_rgb(input_image_path, output_image_path)

SAMPLE_PROGRAM_OUTPUT


The first image embedded in this web page below this paragraph was generated by one use case of the Python program featured in this computer programming tutorial web page.

image_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_50/main/input_image_inverted.png



The image embedded in this web page below this paragraph is the respective input image to the aforementioned output image.

image_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_extension_pack_50/main/abstract_drawing_karbytes_26december2025_p0.jpg



This web page was last updated on 30_DECEMBER_2025. The content displayed on this web page is licensed as PUBLIC_DOMAIN intellectual property.