简体   繁体   中英

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

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

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

from skimage.io import imread

image = imread(image)
negative = 255 - image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM