C和Python语言中的不同值像素! ! !

编程入门 行业动态 更新时间:2024-10-26 07:28:20
本文介绍了C和Python语言中的不同值像素! ! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嘿朋友 我试图将图像pgm(灰度)转换为图像黑白(0或255)两种颜色 我是用python语言做的,因为它的这么容易转换!在我看到python中的所有像素并且运行良好之后,我只看到了两个值255和0并且我通过GIMP打开了图像并且它全部确定 -之后我试图看到c中的所有像素语言,但显示是假的我看到打印不同于python的打印,我没有看到两种颜色只有我在像素11,200,23看到很多价值,... 我播种给你我试过我c和python语言,请问我错了!? 我尝试了什么:

// read_image_line_by_line.cpp:定义入口点控制台应用程序。 // #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(); }

--------------------------来自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语言中的不同值像素! ! !

本文发布于:2023-06-08 06:30:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/576137.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:像素   语言   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!