--------------------------来自PIL import Image的--------------------------------------
image_file = Image.open( lena512.pgm )# 打开彩色图像 image_file = image_file.convert(' 1')# 将图像转换为黑白 image_file.save(' 结果。 pbm') widh,heigh = image_file.size print (widh) print (heigh) pixel_values = list(image_file.getdata()) print (pixel_values)< /conio.h></stdio.h>解决方案请参阅 PBM格式 [ ^ ]。 PBM文件包含可打印的ASCII标头和像素作为打包二进制数据。使用Python代码,您将从像素数据(无标题)创建列表并打印这些值。我不知道python在内部做什么,但我想它会从打包数据中创建一个字节(或整数)列表。 在你的C代码中你正在打印ASCII标头和打包数据。要获得类似的输出,请跳过C代码中的标题并解压缩数据字节:
// 在此处读取标题并获取图像的宽度和高度 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 ++) { // 这里的位顺序可能是错误的。 // 如果是这样,请检查掩码0x80并向左移动。 // 编辑:黑色是1,白色为零! 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 ; } } }To解析标题最好将整个文件内容读入内存(代码未经过测试但编译):
#include< stdio.h中> #include< io.h> #include< ctype.h> // ... FILE * pFile = fopen(result.pbm,rb); //注意:对于Microsoft编译器,这些函数可能需要 //领先得分(_filelength,_fileno) 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; //跳过幻数P4 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; //如果宽度不是8的倍数,则每行额外字节 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 ++) { //这里的比特顺序可能是错误的。 //如果是这样,请检查掩码0x80并向左移动。 //编辑:黑色为1,白色为零! 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; } } } 免费(缓冲);
hey friends I tried to convert image pgm (greyscale) to image black and white (0 or 255) two color I did it by python language because its so easy to convert! after I see all pixel in python and the run is good , I seen only two value 255 and 0 and I opened the image by GIMP and its all ok -after I tried to see all pixel in c language but the display is false I seen print different than the print by python , I not seen two color only I see many value in pixel 11, 200 , 23, ... I sow to you my tried I c and python language , please what my wrong !? What I have tried:
// read_image_line_by_line.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #include<conio.h> void func() { FILE *pFile; pFile = fopen("result.pbm", "rb"); char c; do { c= getc (pFile); printf("%c",c); } while (c != EOF); fclose(pFile); } int _tmain(int argc, _TCHAR* argv[]) { func(); getch(); }---------------------------------------------------------------- from PIL import Image
image_file = Image.open("lena512.pgm") # open colour image image_file = image_file.convert('1') # convert image to black and white image_file.save('result.pbm') widh , heigh =image_file.size print (widh) print (heigh) pixel_values = list(image_file.getdata()) print (pixel_values)</conio.h></stdio.h> 解决方案 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: // Read header here and get width and height of image 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++) { // Maybe the order of bits is wrong here. // If so check with mask 0x80 and shift left. // EDIT: Black is 1 and white is zero! 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++) { // Again: Maybe the order of bits is wrong here. 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"); // NOTE: With Microsoft compilers these functions might require a // leading under score (_filelength, _fileno) 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; // Skip magic number "P4" while (isalnum(*header)) header++; // Skip white spaces while (isspace(*header)) header++; // Get width int width = atoi(header); // Skip width while (isdigit(*header)) header++; // Skip whitespace while (isspace(*header)) header++; int height = atoi(header); // Skip height while (isdigit(*header)) header++; // Skip single whitespace header++; // Pointer to first data (pixel) byte const unsigned char *data = (const unsigned char *)header; // Extra byte per row if width is not a multiple of 8 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++) { // Maybe the order of bits is wrong here. // If so check with mask 0x80 and shift left. // EDIT: Black is 1 and white is zero! printf("%d,", (data_byte & 1) ? 0 : 255); data_byte >>= 1; } } if (stride) { unsigned data_byte = *data++; for (int k = 0; k < stride; k++) { // Again: Maybe the order of bits is wrong here. printf("%d,", (data_byte & 1) ? 0 : 255); data_byte >>= 1; } } } free(buffer);更多推荐
C和Python语言中的不同值像素! ! !
发布评论