Sitemap
Kumareshcbe

Kumareshcbe

IMAGE PROCESSING THROUGH C

3 min readOct 21, 2026

I always thought that image processing required high level of coding knowledge and cannot be done easily by a fresher at college. But I was shocked to discover that it is an easier task than what I had previously imagined it to be. I came across something known as pgm formatted files(portable graymap) files, which are files that store data of images in text format.

These pgm files contain data about each pixel of the image and the data represent the intensity of the pixel ranging from 0 ( pitch black ) to 255 ( pure white ) . It basically begins with something known as magic number which is like the meta data .

P5-> this means that the data is in binary format.

P2->this means that the data is in ASCII format.

The file also contains meta data such as the dimensions of the image and the maximum intensity.

As these are text file, these can be easily manipulated using C program. All I needed to know was how to handle files , structures and functions to change and invert the colours in the image, increase the brightness of the image and even sharpen the image. Once I knew what I was doing, it was nothing but a matter of finding the logic to each problem and not a lack of tools and knowledge that held me back.

This is a sample of how a pgm formatted file might look like , though it is just a 3x3 image.

Get Kumareshcbe’s stories in your inbox

Join Medium for free to get updates from this writer.

Here, the P2 specifies that the texts are ASCII values. In pgm files, comments can be given in the header part of the file where comments are recognized by #.

The “3 3” in the next line is the image dimensions and 255 is the maximum intensity value of the image.

With this, let us try inverting the colours of the image .

Since we now know a way to manipulate image and that the values here are nothing but ASCII values of pixel intensity, we can invert the colours by just subtracting the current intesity from 255(eg:255–0=255). Here, 0 was the initial value of the pixel meaning it was pitch black. When I subtracted it from 256 , the new pixel value became 255 meaning now it is pure white.Any other intermediate pixel intensities are shades of gray.

Press enter or click to view image in full size

This is a small code snippet of how I’ve handled image colour inversion.

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
typedef struct
{
unsigned char *data;
char magic[3];
int rows;
int columns;
int max;
}pgmImg;

void invert_image(pgmImg *img)
{
size_t total=(size_t)img->rows*img->columns,i;
for(i=0;i<total;i++)
{
img->data[i]=(unsigned char)(img->max-img->data[i]);
}
printf("\nSuccess:Colours inverted");
}

In this code, I used a structure to save the image data from the pgm file.The function invert_image handles the colour inversion. The variable total has the value of the total number of pixels and i is used as the counter variable which iterates through every pixel data and inverts the image.

In the same way all other basic image processing and manipulation can be carried out

--

--