See
The PBM Format[
^].
The PBM file contains a printable ASCII header and the pixels as packed binary data. With your Python code you are creating a list from the pixel data (without header) and printing these values. I did not know what python is doing internally but I guess it creates a list of bytes (or integers) from the packed data.
In your C code you are printing the ASCII header and the packed data as they are. To get a similar output, skip the header in your C code and unpack the data bytes:
int stride = width % 8;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width / 8; j++)
{
unsigned data_byte = (unsigned)getc(pFile);
for (int k = 0; k < 8; k++)
{
printf("%d,", (data_byte & 1) ? 0 : 255);
data_byte >>= 1;
}
}
if (stride)
{
unsigned data_byte = (unsigned)getc(pFile);
for (int k = 0; k < stride; k++)
{
printf("%d,", (data_byte & 1) ? 0 : 255);
data_byte >>= 1;
}
}
}
[EDIT]
To parse the header it would be better to read the whole file content into memory (code is not tested but compiles):
#include <stdio.h>
#include <io.h>
#include <ctype.h>
FILE *pFile = fopen("result.pbm", "rb");
long file_len = filelength(fileno(pFile));
unsigned char *buffer = (unsigned char *)malloc(file_len);
fread(buffer, 1, file_len, pFile);
fclose(pFile);
const char *header = (const char *)buffer;
while (isalnum(*header)) header++;
while (isspace(*header)) header++;
int width = atoi(header);
while (isdigit(*header)) header++;
while (isspace(*header)) header++;
int height = atoi(header);
while (isdigit(*header)) header++;
header++;
const unsigned char *data = (const unsigned char *)header;
int stride = width % 8;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width / 8; j++)
{
unsigned data_byte = *data++;
for (int k = 0; k < 8; k++)
{
printf("%d,", (data_byte & 1) ? 0 : 255);
data_byte >>= 1;
}
}
if (stride)
{
unsigned data_byte = *data++;
for (int k = 0; k < stride; k++)
{
printf("%d,", (data_byte & 1) ? 0 : 255);
data_byte >>= 1;
}
}
}
free(buffer);