creating an image file from an ASCII text

Discussion in 'C Programming' started by Karthik, Jun 15, 2005.

  1. Karthik

    Karthik Guest

    Hi,

    I have an ASCII text file which contains 1's,0's and *'s.
    The file looks something like this
    0 1 1 * 1
    1 1 0 * *
    * 0 1 0 1

    I want to create an image file which shows the 1's in green squares,
    0's in red squares and *'s in yellow squares.

    I have a ton of these files and would like to create a script which
    does the whole process of conversion and dumps out an image file in one
    go.

    Image files can be in jpeg/gif/png/eps/ps and the script can be in
    perl/tcl/c/c++ or any thing else

    Is there something out there which does this. Could someone please
    help/guide me on this topic.

    Thanks
    Karthik
    Karthik, Jun 15, 2005
    #1
    1. Advertising

  2. Karthik

    Mike Scott Guest

    Karthik wrote:
    > Hi,
    >
    > I have an ASCII text file which contains 1's,0's and *'s.
    > The file looks something like this
    > 0 1 1 * 1
    > 1 1 0 * *
    > * 0 1 0 1
    >
    > I want to create an image file which shows the 1's in green squares,
    > 0's in red squares and *'s in yellow squares.
    >
    > I have a ton of these files and would like to create a script which
    > does the whole process of conversion and dumps out an image file in one
    > go.
    >
    > Image files can be in jpeg/gif/png/eps/ps and the script can be in
    > perl/tcl/c/c++ or any thing else
    >
    > Is there something out there which does this. Could someone please
    > help/guide me on this topic.


    Offhand, I'd look at ImageMagick which is a command-line program (or
    rather, suite of) which can create images by tiling component parts (and
    also does lots of other good things). Then think about a short script to
    read your file of 0/1/* and generate suitable commands to run
    ImageMagick appropriately.

    Maybe there are prettier ways. The best solution may depend on whether
    this is a one-off, or whether you need high efficiency.

    --
    Please use the corrected version of the address below for replies.
    Replies to the header address will be junked, as will mail from
    various domains listed at www.scottsonline.org.uk
    Mike Scott Harlow Essex England.(unet -a-t- scottsonline.org.uk)
    Mike Scott, Jun 15, 2005
    #2
    1. Advertising

  3. Re : creating an image file from an ASCII text

    >
    > Offhand, I'd look at ImageMagick which is a command-line program (or
    > rather, suite of) which can create images by tiling component parts (and
    > also does lots of other good things). Then think about a short script to
    > read your file of 0/1/* and generate suitable commands to run
    > ImageMagick appropriately.


    An awk script to convert to ppm, for example ? (awk is just a suggestion,
    any other will do).
    Jean-Claude Arbaut, Jun 15, 2005
    #3
  4. In article <>,
    Karthik <> wrote:
    >I have an ASCII text file which contains 1's,0's and *'s.
    >The file looks something like this
    >0 1 1 * 1
    >1 1 0 * *
    >* 0 1 0 1


    >I want to create an image file which shows the 1's in green squares,
    >0's in red squares and *'s in yellow squares.


    >I have a ton of these files and would like to create a script which
    >does the whole process of conversion and dumps out an image file in one
    >go.


    >Image files can be in jpeg/gif/png/eps/ps and the script can be in
    >perl/tcl/c/c++ or any thing else


    You can encode the files in ppm (portable pixmap file format) and
    then convert to another format, such as by using 'ppmtogif' or
    a program such as 'xv'.

    Or, since perl is one of your options, you could use one of the perl
    graphics libraries from cpan.org and do it all in one go.


    The below sample encoding script is OT for C, but you can
    convert it to C code if you want ;-)


    $ cat rgy
    #!/bin/ksh

    [[ $# -ne 1 ]] && echo "Usage: $0 toppm FILE" >&2 && exit
    FID=$1
    NFID=$FID.ppm
    [[ ! -f "$FID" ]] && echo "Not a readable file: $FID" >&2 && exit
    ((WID=$(head -1 $FID | wc -c) / 2))
    ((HIGH=$(wc -l < $FID)))

    (
    echo "P3"
    echo "# $FID converted to ppm format"
    echo "$WID $HIGH"
    echo "# 1 is the maximum colour intensity"
    echo "1"
    sed -e 's/0/R/g' -e 's/1/G/g' -e 's/\*/Y/g' \
    -e 's/R/1 0 0/g' -e 's/G/0 1 0/g' -e 's/Y/1 1 0/g' $FID | fmt -66
    ) > $NFID

    $ cat testfid
    0 1 1 * 1
    1 1 0 * *
    * 0 1 0 1

    $ ./rgy testfid
    $ cat testfid.ppm
    P3
    # testfid converted to ppm format
    5 3
    # 1 is the maximum colour intensity
    1
    1 0 0 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 0 1 0 1 0 0 1 1 0 1 1 0 1 1 0
    1 0 0 0 1 0 1 0 0 0 1 0


    --
    Usenet is like a slice of lemon, wrapped around a large gold brick.
    Walter Roberson, Jun 15, 2005
    #4
  5. Karthik wrote:
    <snip>
    >
    > Image files can be in jpeg/gif/png/eps/ps and the script can be in
    > perl/tcl/c/c++ or any thing else
    >
    > Is there something out there which does this. Could someone please
    > help/guide me on this topic.


    This is off-topic in comp.lang.c. Please post image processing
    questions elsewhere.


    Mark F. Haigh
    Mark F. Haigh, Jun 16, 2005
    #5
  6. Karthik

    Guest

    This can be done easily in Java.

    Create an Image buffer and open your ASCII file. Depending on the value
    plot the squares and save this buffer to an Image file.

    If u want, i can give u the code.
    , Jun 18, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TOXiC
    Replies:
    5
    Views:
    1,246
    TOXiC
    Jan 31, 2007
  2. James O'Brien
    Replies:
    3
    Views:
    247
    Ben Morrow
    Mar 5, 2004
  3. Alextophi
    Replies:
    8
    Views:
    502
    Alan J. Flavell
    Dec 30, 2005
  4. bruce
    Replies:
    38
    Views:
    270
    Mark Lawrence
    Nov 1, 2013
  5. MRAB
    Replies:
    0
    Views:
    95
Loading...

Share This Page