change an image with python
by - Thursday, January 1, 1970 at 12:00 AM
first u need pillow PIL library for python

u can change any pixel in an image

from PIL import Image

old_image_name = input("Enter the name of the image that you want to change: ")
new_image_name = input("Enter the output file name (Default = output.format: ") or "output.{old_image_name.rsplit('.', 1)[-1]}"

image = Image.open(old_image_name) ## Insert any picture name here ##
pixel = image.load() # load pic so it can be scanned
size = image.size # Get the width and hight of the image for iterating over

##find the individual X and Y sizes so they can be iterated over
size_x = int(size[0])
size_y = int(size[1])


colour_counter = 0

for x in range (0, size_x): #loops through all the columns
    for y in range (0, size_y): #loops through all the rows

            current_pixel_value = pixel[x, y]  # Get the RGBA Value of the pixel
           
                colour_counter += 1

                if colour_counter % 3 == 0:
                    pixel[x,y] = (0, 255, 0) # make pixel green
                elif colour_counter % 2 == 0:
                    pixel[x,y] = (0, 0, 255) # make pixel blue
           
                else:
                    pixel[x,y] = (255, 0, 0) # make pixel red

image.save(new_image_name)  # Save the modified pixels as .png
Reply
what are the use cases for script like this ?
Reply
I used the pixel change code a lot for computer vision applications. For example if you were given the coordinates of an object in txt format, but you wanted to verify whether the coordinates were right, you would change the coordinates to a colour of your choosing.

Loop this through for as many images you need, and you could quickly verify if the coordinates given were correct or not by visual inspection.

Sometimes the coordinates provided could be wrong because of errors in format conversions.
Reply
(October 13, 2022, 07:01 PM)whatworm Wrote: first u need pillow PIL library for python

u can change any pixel in an image

from PIL import Image

old_image_name = input("Enter the name of the image that you want to change: ")
new_image_name = input("Enter the output file name (Default = output.format: ") or "output.{old_image_name.rsplit('.', 1)[-1]}"

image = Image.open(old_image_name) ## Insert any picture name here ##
pixel = image.load() # load pic so it can be scanned
size = image.size # Get the width and hight of the image for iterating over

##find the individual X and Y sizes so they can be iterated over
size_x = int(size[0])
size_y = int(size[1])


colour_counter = 0

for x in range (0, size_x): #loops through all the columns
    for y in range (0, size_y): #loops through all the rows

            current_pixel_value = pixel[x, y]  # Get the RGBA Value of the pixel
           
                colour_counter += 1

                if colour_counter % 3 == 0:
                    pixel[x,y] = (0, 255, 0) # make pixel green
                elif colour_counter % 2 == 0:
                    pixel[x,y] = (0, 0, 255) # make pixel blue
           
                else:
                    pixel[x,y] = (255, 0, 0) # make pixel red

image.save(new_image_name)  # Save the modified pixels as .png

Amazing
Reply
oh no man, your for loop is to expensive, please keep your code more efficient time
Reply
thanks my friend
Reply


 Users viewing this thread: change an image with python: No users currently viewing.