Frage

I have a huge ASCII-text representing an bitmap like ASCII-art. Now I'm looking for something like an inverted ASCII-art generator. I like to convert each character to a colored pixel.

Is there any free tool that can stuff like this?

War es hilfreich?

Lösung 3

I just coded a little very spartanic php script using image-gd library. It read's a text from textarea formular and assigns colors to the characters using there ASCII-Value and some multiplier functions to make color differences between near-neighborhood-ASCII like "a" and "b" visible. At now it's just working for known text size.

<?php

if(isset($_POST['text'])){
    //in my case known size of text is 204*204, add your own size here:
    asciiToPng(204,204,$_POST['text']);
}else{
    $out  = "<form name ='textform' action='' method='post'>";
    $out .= "<textarea type='textarea' cols='100' rows='100' name='text' value='' placeholder='Asciitext here'></textarea><br/>";
    $out .= "<input type='submit' name='submit' value='create image'>";
    $out .= "</form>";
    echo $out;
}

function asciiToPng($image_width, $image_height, $text)
{
    // first: lets type cast;
    $image_width = (integer)$image_width;
    $image_height = (integer)$image_height;
    $text = (string)$text;
    // create a image
    $image  = imagecreatetruecolor($image_width, $image_height);

    $black = imagecolorallocate($image, 0, 0, 0);
    $x = 0;
    $y = 0;
    for ($i = 0; $i < strlen($text)-1; $i++) {
        //assign some more or less random colors, math functions are just to make a visible difference e.g. between "a" and "b"
        $r = pow(ord($text{$i}),4) % 255;
        $g = pow(ord($text{$i}),3) % 255;
        $b = ord($text{$i})*2 % 255;
        $color = ImageColorAllocate($image, $r, $g, $b);
        //assign random color or predefined color to special chars ans draw pixel
        if($text{$i}!='#'){
            imagesetpixel($image, $x, $y, $color);
        }else{
            imagesetpixel($image, $x, $y, $black);
        }
        $x++;
        if($text{$i}=="\n"){
            $x = 0;
            $y++;
        }
    }
    // show image, free memory
    header('Content-type: image/png');
    ImagePNG($image);
    imagedestroy($image);
}
?>

Andere Tipps

You did not use the tag of a specific programming language. Therfore, Mathematica go..

I use Rasterize to convert a letter into an image of a letter. Then I can extract the pixel-matrix with ImageData. The Mean of all pixel is one possibility to calculate your final pixel-value for the letter. Putting this into a function which memorizes the pixel-values, so that we don't have to calculate this over and over again:

toPixel[c_String] := toPixel[c] = Mean[Flatten[ImageData[Rasterize[
 Style[c, 30, FontFamily -> "Courier"], "Image", ColorSpace -> "Grayscale"]]]]

Now you can split your string into lines and then apply this to every character. After padding the resulting lists to get a full matrix again you have your image

data = toPixel /@ Characters[#] & /@ StringSplit[text, "\n"];
Image@(PadRight[#, 40, 1] & /@ data) // ImageAdjust

For this text

           ,i!!!!!!;,
      .,;i!!!!!'`,uu,o$$bo.
    !!!!!!!'.e$$$$$$$$$$$$$$.
   !!!!!!! $$$$$$$$$$$$$$$$$P
   !!!!!!!,`$$$$$$$$P""`,,`"
  i!!!!!!!!,$$$$",oed$$$$$$
 !!!!!!!!!'P".,e$$$$$$$$"'?
 `!!!!!!!! z$'J$$$$$'.,$bd$b,
  `!!!!!!f;$'d$$$$$$$$$$$$$P',c,.
   !!!!!! $B,"?$$$$$P',uggg$$$$$P"
   !!!!!!.$$$$be."'zd$$$P".,uooe$$r
   `!!!',$$$$$$$$$c,"",ud$$$$$$$$$L
    !! $$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    !'j$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  d@@,?$$$$$$$$$$$$$$$$$$$$$$$$$$$$P
  ?@@f:$$$$$$$$$$$$$$$$$$$$$$$$$$$'
   "" `$$$$$$$$$$$$$$$$$$$$$$$$$$F
       `3$$$$$$$$$$$$$$$$$$$$$$F
          `"$$$$$P?$$$$$$$"`
                    `""

we get

Mathematica graphics

Restore an image from an ASCII art with Java

Let's assume that we have a density scale of the characters that make up an ASCII image, so we can restore a grayscale bitmap from it. And let's assume that each character occupies an area of 21×8 pixels, so when restoring, we have to scale up the picture.

ASCII text (image.txt):

***************************************
***************************************
*************o/xiz|{,/1ctx*************
************77L*```````*_1{j***********
**********?i```````````````FZ**********
**********l`````````````````7**********
**********x`````````````````L**********
**********m?i`````````````iz1**********
************]x```````````\x{***********
********?1w]c>```````````La{]}r********
******jSF~```````````````````^xv>******
*****l1,```````````````````````*Sj*****
****7t```````````````````````````v7****
***uL`````````````````````````````t]***

ASCII picture (screenshot):

ASCII picture

Restored picture:

Restored picture


This code reads a text file, obtaines a brightness value from character density, creates grayscale colors from them, repeats each of those colors 21 times in height and 8 times in width, and then saves the picture as grayscale bitmap.

Without scaling scH=1 and scW=1, the number of pixels is equal to the number of characters in the original text file.

The character density scale should be the same with which the ASCII image was built.

class ASCIIArtToImage {
  int width = 0, height = 0;
  ArrayList<String> text;
  BufferedImage image;

  public static void main(String[] args) throws IOException {
    ASCIIArtToImage converter = new ASCIIArtToImage();
    converter.readText("/tmp/image.txt");
    converter.restoreImage(21, 8);
    ImageIO.write(converter.image, "jpg", new File("/tmp/image.jpg"));
  }

  public void readText(String path) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
    this.text = new ArrayList<>();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
      this.width = Math.max(this.width, line.length());
      this.text.add(line);
    }
    this.height = this.text.size();
  }

  public void restoreImage(int scH, int scW) {
    this.image = new BufferedImage( // BufferedImage.TYPE_BYTE_GRAY
            this.width * scW, this.height * scH, BufferedImage.TYPE_INT_RGB);
    for (int i = 0; i < this.height; i++) {
      for (int j = 0; j < this.width; j++) {
        // obtaining a brightness value depending on the character density
        int val = getBrightness(this.text.get(i).charAt(j));
        Color color = new Color(val, val, val);
        // scaling up the image
        for (int k = 0; k < scH; k++)
          for (int p = 0; p < scW; p++)
            this.image.setRGB(j * scW + p, i * scH + k, color.getRGB());
      }
    }
  }

  static final String DENSITY =
        "@QB#NgWM8RDHdOKq9$6khEPXwmeZaoS2yjufF]}{tx1zv7lciL/\\|?*>r^;:_\"~,'.-`";

  static int getBrightness(char ch) {
    // Since we don't have 255 characters, we have to use percentages
    int val = (int) Math.round(DENSITY.indexOf(ch) * 255.0 / DENSITY.length());
    val = Math.max(val, 0);
    val = Math.min(val, 255);
    return val;
  }
}

See also: Draw an ASCII art from an imageConvert image to ASCII art

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top