abshi
abshi

Reputation: 73

Image processing: Trying to make a photo negative?

I understand to make an image negative, you have to change the RGB values so that the current value is being subtracted from 255.

What is wrong with my following code?

def negative(im):
height=len(im)
width = len(im[0])
for row in range(height):
    for col in range(width):
        red = im[row][col][0] - 255
        green = im[row][col][1] - 255
        blue = im[row][col][2] - 255
        im[row][col]=[red,green,blue]
return im

It returns the error "TclError: can't parse color "#-1d-c-2""

Upvotes: 4

Views: 9289

Answers (2)

Adam Hughes
Adam Hughes

Reputation: 16309

Why not use scikit-image instead? It's vectorized:

from skimage.io import imread

image = imread(image)
negative = 255 - image

Upvotes: 3

neil
neil

Reputation: 3635

Your problem is that you are getting negative numbers. I think you should be doing 255 - x rather than x - 255

Upvotes: 5

Related Questions