Given an image in PGM format and the task is to invert the image color (making negative) content in PGM format.
PGM image represents a gray scale graphic image. PGM is the abbreviation of Portable Gray Map. This image file contains one or more PGM images file.
Significance of data chunks: The data which are used to create PGM image listed below:
- P2 is the type of image that is grey image
- 4 4 is the image dimension
- 255 is the maximum grey level
- Since, the image data are stored in matrix format and each row indicates the image row and the value indicates the grey level of corresponding pixel. The maximum value (255) is used for white and minimum value (0) is used for black.
P2 4 4 255 255 0 255 0 0 255 0 255 100 200 150 100 50 150 200 0
The input image looks like:
How to invert the image data?
Invert the grey image means change the grey level of an image using (255 – grey lavel) i.e. if grey level of a pixel is 150 then grey level in negative image is (255 – 150) = 105. It means 255 will changes with 0 and 0 will change with 255. It is changing of proportion of black and white present in grey.
P2 4 4 255 0 255 0 255 255 0 255 0 155 55 105 155 205 105 55 255
The output image looks like:
Negative (Invert) Image
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- C program to write an image in PGM format
- Image Processing in Java | Set 4 (Colored image to Negative image conversion)
- C program to print characters without using format specifiers
- C program to print the length of a String using %n format specifier
- C program to Check Whether a Number is Positive or Negative or Zero
- C program to reverse the content of the file and print it
- C program to append content of one text file to another
- C program to count Positive and Negative numbers in an Array
- Image Processing in Java | Set 6 (Colored image to Sepia image conversion)
- Image Processing in Java | Set 3 (Colored image to greyscale image conversion)
- Node Jimp | invert
- Python PIL | logical_xor() and invert() method
- OpenCV C++ Program to blur an image
- OpenCV Python Program to blur an image
- C program to print odd line contents of a File followed by even line content
- Python | OpenCV program to read and save an Image
- Matlab program to rotate an image 180 degrees clockwise without using function
- OpenCV C++ Program to create a single colored blank image
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- Image Processing in Java | Set 5 (Colored to Red Green Blue Image Conversion)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.