Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image processing: Trying to make a photo negative?

Tags:

python

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""

like image 380
abshi Avatar asked Mar 16 '23 16:03

abshi


2 Answers

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

like image 96
neil Avatar answered Mar 24 '23 20:03

neil


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

from skimage.io import imread

image = imread(image)
negative = 255 - image
like image 44
Adam Hughes Avatar answered Mar 24 '23 20:03

Adam Hughes