问题描述
大家好
我想开发一个c程序,将图像文件转换为文本文件,并在提供文本文件时将其转换为从中生成文本文件的图像.
任何想法和帮助材料将不胜感激.
问候
TanzeelurRehman
Hi Every one
I want to develop a c program which converts an image file into text file, and when a text file is supplied that will convert into image from which the text file had generated.
Any idea and helping material will be appreciated.
Regards
TanzeelurRehman
推荐答案
// note: 75+155+25 == 255, the values I used here are inaccurate, I just guessed
// use the wiki page to get more accurate distribution
unsigned char intensity = (unsigned char)(((int)R*75 + (int)G*155 + (int)B*25) / 255);
假设您将图片的每个像素转换为一个字节(灰度强度值).下一步是将一个ascii字符与每个inteity值(从0到255)相关联.问题是您只有有限数量的ascii字符,远远少于所需的256个,因此首先您必须对"256色"进行下采样grascale位图以使用少得多的强度值(这是有损转换,例如将32位颜色位图转换为1或4或16或任何颜色位图时).在降采样期间,可以通过应用抖动来获得更好的质量,但这远远超出了此迷你初学者教程所能解决的范围,因此我将使用一种更简单的方法.而不是我的灰度图像中的256个值,我只需要8个,所以我将每个强度值除以32.此后,每个inteity值将在[0..7]范围内.
之后,我将所有强度值与一个ascii字符关联:
0:空格
1:.
2::
3:我
4:o
5:V
6:M
7:@
在纯C中,此转换如下所示:
Lets say you converted each pixel of your picture into a byte (grayscale intensity value). The next step is to associate an ascii character with each intesity value from 0 to 255. The problem is that you have only a limited number of ascii characters, much less than 256 that you need so first you have to downsample your "256 color" grascale bitmap to use much less intensity values (this is a lossy conversion like when you convert a 32 bit color bitmap into a 1 or 4 or 16 or whatever color bitmap). During downsampling you can achieve much better quality by applying dithering but that goes far beyond what fits into this mini beginner tutorial so I will use a much simpler method. Instead of 256 values in my grayscale image I want only 8, so I divide each intensity value by 32. After this each intesity value will be in the range [0..7].
After this I associate all the intensity values with an ascii character:
0: space
1: .
2: :
3: i
4: o
5: V
6: M
7: @
In plain C this conversion looks like this:
char ascii_pixel = " .:ioVM@"[intensity_value];
注意:您可以使用8个以上的强度值,因为您有更多的ascii字符,为简单起见,我使用8个强度值.您可以将Inteity值除以16来使用16,也可以将Inteity除以8来使用32.如何为强度值选择字符?每个ASCII字符本身也是一个由黑白像素形成的小图像.黑白像素的比例以及它们在ASCII字符小图像上的分布方式可以定义强度,当您像使用ASCII技术那样从远处看时就可以确定强度.不必费心找出自己的ascii字符集,例如,如果您想使用16个ascii字符进行转换,您应该在google上搜索以找到适合您工作的16个正确的ascii值.
由于转换是有损的,因此您无法完全撤消转换.如果您了解上述过程,那么反向过程应该很简单.请注意,我描述的方法非常简单,您可以使用抖动和不同的图像处理算法(例如边缘检测),以更好地转换不同类型的图像.如果您以某种字符视频模式在显示器上显示ascii艺术,则可以在ascii字符中使用颜色.可以使用Ascii art在角色模式下播放视频!对于其他图像到ascii艺术的转换方法,您可以查看以下内容: http://en.wikipedia.org/wiki/ASCII_art [< ^ ]
其他说明:使用此方法转换大图片(例如1024x768)会导致文件很大,因此,您可能需要在转换前缩小图片,使其宽度为80-100像素,因为如果您查看文本即使使用大型显示器,文件也不会在屏幕上显示太多字符.有很多图像缩放方法( http://en.wikipedia.org/wiki/Image_scaling [ ^ ]),但是不幸的是,讨论它们不在本教程的讨论范围之内.您可以通过仅提供小图片作为程序输入或通过使用像软件一样好的photoshop进行缩放来模拟缩放. :-)
其他有用的参考资料:
http://www.cprogramming.com/ [ ^ ]
http://tipsandtricks.runicsoft.com/Cpp/BitmapTutorial.html [ ^ ]
Notes: You can use more than 8 intensity values since you have more ascii characters, I used 8 just for the sake of simplicity. You could use 16 by dividing the intesity values just by 16 or you could use 32 by dividing intesity by 8. How to pick characters for your intensity values? Each ascii character itself is also a small image formed from black and white pixels. The ratio of the black and white pixels and the way they are distributed on the small image of the ascii character can define an intensity when you look at it from a distance like you doo it with ascii art. Don''t bother with finding out your own set of ascii characters for example if you wanna use 16 ascii characters for your conversion, you should rather search with google to find 16 right ascii values for your job.
Since the conversion is quite lossy you can not reverse it fully. If you understood the above process then the reversing process should be straightforward. Note that the method I described is quite a simple one, you can involve dithering and different image processing algorithms (like edge detection) to make the conversion of different kind of images better. If you are showing ascii art on the monitor with some character video mode then you can use colors with your ascii characters. Ascii art can be used to play video in character mode! For other image to ascii art conversion methods you can check out this: http://en.wikipedia.org/wiki/ASCII_art[^]
Other note: Converting a big (lets say 1024x768) picture with this method can lead to a huge file, for this reason you might want to shrink your picture before conversion so that its width is around 80-100 pixels because if you view a text file not much more characters fit on the screen even with huge monitors. There are a lot of image rescale methods (http://en.wikipedia.org/wiki/Image_scaling[^]) but discussing them is unfortunately out of the scope of this tutorial. You can simulate rescaling by giving only small pictures as an input to your program or by rescaling it with a good photoshop like software. :-)
Other useful references:
http://www.cprogramming.com/[^]
http://tipsandtricks.runicsoft.com/Cpp/BitmapTutorial.html[^]
这篇关于使用C编程将图像文件转换为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!