In this paper, the inverse color implementation method of Python image processing is described. Share to everyone for your reference. Specific as follows:
Let's first load a 8-bit grayscale image
Each pixel corresponds to a grayscale value from 0-255
You only need to read the grayscale value A of each pixel, and then write the 255-a
Once this is done, the image will be reversed.
Here the operating environment is:
Python is: Python2.7.6
Version OpenCV2.4.10 (available to http://sourceforge.net/projects/opencvlibrary/files/opencv-win/download)
NumPy: numpy-1.9.1-win32-superpack-python2.7 (available to http://sourceforge.net/projects/numpy/files/NumPy/1.9.1/download)
The specific Python code is as follows:
Import CV2.CV as Cvimage = CV. LoadImage (' angelababy.jpg ', 0) size = (image.width,image.height) itmp = cv. CreateImage (size,image.depth,image.nchannels) for I in Range (image.height): for J in Range (Image.width): itmp [I,j] = 255-IMAGE[I,J]CV. Namedwindow (' image ') cv. Namedwindow (' itmp ') CV. ShowImage (' image ', image) CV. ShowImage (' itmp ', itmp) CV. Waitkey (0)
The results of the operation are as follows:
Let's just change the code a little bit.
For color images, OPENCV is stored as a tuple (r,g,b) in Python for each pixel point.
So for color image inverse color, only need to get a tuple (255-r,255-g,255-b) on the line
The code is as follows:
Import CV2.CV as Cvimage = CV. LoadImage (' angelababy.jpg ', 1) size = (image.width,image.height) itmp = cv. CreateImage (size,image.depth,image.nchannels) for I in Range (image.height): for J in Range (Image.width): itmp [I,j] = (255-image[i,j][0],255-image[i,j][1],255-image[i,j][2]) CV. Namedwindow (' image ') cv. Namedwindow (' itmp ') CV. ShowImage (' image ', image) CV. ShowImage (' itmp ', itmp) CV. Waitkey (0)
The effect is as follows:
Hopefully this article will help you with Python programming.