Change a colour of a pixel in python

Change a colour of a pixel in python

To change the color of a pixel in an image using Python, you can use the Pillow (PIL) library, which provides easy-to-use methods for image processing. Here's a simple example:

from PIL import Image def change_pixel_color(image_path, x, y, new_color): # Open the image image = Image.open(image_path) # Get the pixel value at the specified position pixel = image.getpixel((x, y)) # Change the pixel color image.putpixel((x, y), new_color) # Save the modified image image.save("modified_image.png") # Example usage change_pixel_color("original_image.png", 100, 50, (255, 0, 0)) # Change pixel at position (100, 50) to red 

In this example:

  • Image.open(image_path) opens the image file.
  • image.getpixel((x, y)) gets the RGB color values of the pixel at position (x, y).
  • image.putpixel((x, y), new_color) changes the color of the pixel at position (x, y) to the specified new_color.
  • image.save("modified_image.png") saves the modified image to a new file.

Make sure to replace "original_image.png" with the path to your original image file. The new_color argument should be a tuple representing the RGB values of the desired color. For example, (255, 0, 0) represents red.

Note: Pillow can be installed using the following:

pip install Pillow 

Keep in mind that this approach is suitable for small-scale image processing. For more advanced image manipulation, consider using libraries like OpenCV.

Examples

  1. "Python PIL change pixel color"

    from PIL import Image # Open an image file img = Image.open("image.jpg") # Get the pixel at coordinates (x, y) pixel = img.getpixel((x, y)) # Change the color of the pixel to a new RGB value img.putpixel((x, y), (new_red, new_green, new_blue)) # Save the modified image img.save("modified_image.jpg") 

    Description: This code uses the Python Imaging Library (PIL) to open an image, retrieve a pixel's RGB values, and then change its color before saving the modified image.

  2. "Python OpenCV change pixel color"

    import cv2 # Read an image img = cv2.imread("image.jpg") # Get and modify the pixel at coordinates (x, y) img[y, x] = [new_blue, new_green, new_red] # Save the modified image cv2.imwrite("modified_image.jpg", img) 

    Description: This code utilizes OpenCV to read an image, access and modify a pixel's color values, and then save the modified image.

  3. "Python NumPy change pixel color"

    import numpy as np from PIL import Image # Open an image file img = Image.open("image.jpg") img_array = np.array(img) # Change the color of the pixel at coordinates (x, y) img_array[y, x] = [new_red, new_green, new_blue] # Save the modified image modified_img = Image.fromarray(img_array) modified_img.save("modified_image.jpg") 

    Description: This code uses NumPy to manipulate pixel color in the image array obtained from the PIL library.

  4. "Python matplotlib change pixel color"

    import matplotlib.pyplot as plt import matplotlib.image as mpimg # Read an image img = mpimg.imread("image.jpg") # Change the color of the pixel at coordinates (x, y) img[y, x] = [new_red, new_green, new_blue] # Display and save the modified image plt.imshow(img) plt.imsave("modified_image.jpg", img) 

    Description: This code uses matplotlib to read, modify, and display an image, along with saving the modified version.

  5. "Python Pygame change pixel color"

    import pygame from pygame.locals import * # Initialize Pygame pygame.init() # Load an image img = pygame.image.load("image.jpg") # Change the color of the pixel at coordinates (x, y) img.set_at((x, y), (new_red, new_green, new_blue)) # Save the modified image pygame.image.save(img, "modified_image.jpg") 

    Description: This code uses Pygame to load an image, change the color of a pixel, and save the modified image.


More Tags

ts-node deployment operation spatial-query piecewise apache-zookeeper double jquery-ui-tabs serenity-bdd andengine

More Programming Questions

More Entertainment Anecdotes Calculators

More Everyday Utility Calculators

More Stoichiometry Calculators

More Chemical reactions Calculators