Click here to Skip to main content
13,357,514 members (71,463 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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.

Posted 28-Sep-12 8:48am
ryanb31 28-Sep-12 14:27pm
What would the text file look like? Do you literally mean you want to convert images into ascii images? What are you doing?
Sergey Alexandrovich Kryukov 28-Sep-12 15:02pm
I answered based on guesswork. Maybe I wasn't right, not 100% sure, please see...
TanzeelurRehman 28-Sep-12 15:09pm
Thanks for your reply, i just want to convert image into ascii text file and the convert to image when needed by another module, first i want to convert image file into ascii text file, in another module i want to convert that ascii text file into image file

ryanb31 28-Sep-12 15:12pm
As in ascii art?
pasztorpisti 28-Sep-12 14:47pm
ASCII art? :-) But that is usually lossy...
Sergey Alexandrovich Kryukov 28-Sep-12 15:01pm
Most likely, OCR -- then I answered, please see...
Member 12015170 27-Sep-15 15:29pm
Friends I want to build an c application that takes input as image takes text from image and displays in screen
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

So you wanna generate ascii art? Here is how to do that: First you have to read in the picture file and convert it if necessary so that you have the width and height of the picture and the color (usually RGB) for each pixel in a 2 dimensional array (the dimensions are the width and height of the picture). It might sound easy to "just read in a picture file" but it is very tricky even for the simplest formats like .bmp and .tiff or .tga if you want to handle every cases. What you think, why are there programs out there that specialize on picture viewing? Because if you want to handle all picture formats without the help of some libraries you will spend your remaining life (at least a few years) writing libraries that handle picture formats before you can create your ascii generator prog. :-) For this simple reason I recommend you to handle only a simple format that doesn't emply compression and other tricks, for example the windows BMP format. Even in BMP files there are 1, 2, 4, 8, 16, 24, and 32 bit color depth representations that totally differ in binary, and it might contain RLE compression (and a lot of other things if we supported various other headers:[^]). For first handle just BMP files without RLE (I never supported RLE compression in my progs) and restrict the color depth to 32 bits to make your work easier. So support just pure 32 bit bmp files. Search for bmp tutorials using google, I wont write the 1000th bmp tutorial. Its not just reading in bytes from a file and using them as color values, basically every picture file format contains a header at the beginning of the file and different kind of other garbage here and there... So use google.

Lets say you successfully allocated your memory and read in the color values of each pixels into memory and by specifying a row (Y) and column (X) value you can pick the color (RGB) of a pixel from your picture. To make ascii art you have to be able to convert every pixel into grayscale (the Red, Green, and Blue - aka RGB - components of it into intesity), there are a lot of methods to do that ([^]). The color of a pixel consists of 3 values (3 channels - at least in RGB color space) and you will convert it to a single value (1 channel) - intensity. This is already a lossy conversion. The R G and B values in bmp files are bytes, this means that their range is [0..255]. Your resulting intensity is also a byte with range [0..255]. The linked wiki article mentions the method I always used to convert RGB to a single grayscale intensity value: INTENSITY = R*0.3 + G*0.6 + B*0.1
With byte magic in C its something like the following:
// 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);

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];

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:[^]

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 ([^]) 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:[^][^]
Espen Harlinn 29-Sep-12 18:50pm
5'ed! :-D
pasztorpisti 29-Sep-12 18:55pm
Thank you! :-)
TanzeelurRehman 30-Sep-12 4:10am
Hmmm that is what i want. Thank you @pasztorpisti
pasztorpisti 30-Sep-12 4:39am
You are welcome!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is not "convert", this is OCR — Optical Character Recognition:[^].

The is the list of well-known products, some are open-source:[^].

You can try to find some other library, but I must warn you: everything from the open source I tried so far did not satisfy me:[^].

Good luck,
pasztorpisti 28-Sep-12 15:14pm
5ed, you must be right - after reading the question I haven't associated it with OCR at all...
Sergey Alexandrovich Kryukov 28-Sep-12 16:09pm
Thank you very much.
What the... ah, 1-voter's attack, I see...
Marcus Kramer 28-Sep-12 20:58pm
+5. I'm glad you understood what he wanted. Until I read your answer, OCR didn't even register with me.
Sergey Alexandrovich Kryukov 28-Sep-12 21:16pm
Well, hopefully. We did not see if anything is formally accepted (hey, Tanzeelur, how about the green button? :-)
Thank you, Marcus.
TanzeelurRehman 29-Sep-12 0:18am
Thank you for your reply

I am going to elaborate it in step by step way what i have to develop

1. A c program which will ask for an image file.
2. when the image file is provided, the program will convert it into text file something like the conversion in

3. Another module in the program should convert the converted text file in step 1 and step 2 into image file again. it doesn't matter if the picture quality loss if the text file is converted into image again.

So i need help. How can i achieve this? what should be the logic?

Why would you need help, where? all looks clear. And what library do you use for imagine, if any?
Basically, you need to manually select some 256 symbols to map gray colors 0 to 255, read all pixels and map each pixel value to characters. This is the simplest algorithm of the "ASCII art". More complex ways -- needs a lot of thinking.
TanzeelurRehman 29-Sep-12 0:38am
Is this possible that i will get the image file in byte stream, and convert each byte to some ascii values as you have mentioned above. If so then would you please provide me how to convert image to byte steam or byte array in c.
Sure, but you need to represent the stream or file (not difference, a file will be a stream anyway) as an array of pixels (or pointer to the buffer with such data, pretty much the same thing), and it depends on the library you use and input format.
pasztorpisti 29-Sep-12 17:48pm
:ROFL: This time my guess was better... :-)
Sergey Alexandrovich Kryukov 30-Sep-12 18:50pm
Wow! That's right.
pasztorpisti 30-Sep-12 20:00pm
This was quite unexpected even for me, your solution seems to be THE logical answer after reading the question. A typical case when the OP doesn't know what he is searching for. :-)
TanzeelurRehman 29-Sep-12 2:02am
I am using stdio.h from the standard library.I will do the following steps

1) I will read the file in binary mode, that will provide me the byte arrary (Am i Right), And i will modify each byte (Pixel) according to my need.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Reading all, I didn't understand your question.
What exactly do you want?
1. do you want to read text from picture(I mean the view)
2. or do you want to convert your binary file into text file

if it is 1 then as SA said

but if it is two then read base64 encoding
TanzeelurRehman 29-Sep-12 0:16am
Thank you for your reply

I am going to elaborate it in step by step way what i have to develop

1. A c program which will ask for an image file.
2. when the image file is provided, the program will convert it into text file something like the conversion in

3. Another module in the program should convert the converted text file in step 1 and step 2 into image file again.

So i need help. How can i achieve this? what should be the logic?

Mohibur Rashid 29-Sep-12 2:14am
Now this one make sense.......... but cant help you much with your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180111.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100