在位图中可视化Ascii文本

时间:2012-11-13 23:50:15

标签: image-processing bitmap ascii ascii-art

我有一个巨大的Ascii文本,表示像ASCII-art这样的位图。现在我正在寻找像倒置Ascii艺术发电机的东西。我喜欢将每个角色转换为彩色像素。 有没有可以像这样的免费工具?

更新: 我使用下面的代码编写了这个小工具,它在线:http://www.webspice.eu/ascii2png/

3 个答案:

答案 0 :(得分:2)

您没有使用特定编程语言的标记。因此,Mathematica去..

我使用Rasterize将字母转换为字母图像。然后我可以用ImageData提取像素矩阵。所有像素的Mean是计算字母最终像素值的一种可能性。把它放到一个记忆像素值的函数中,这样我们就不必一遍又一遍地计算:

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

现在您可以将字符串拆分为行,然后将其应用于每个字符。填充结果列表以再次获得完整矩阵后,您将获得图像

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

本文

           ,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?$$$$$$$"`
                    `""

我们得到了

Mathematica graphics

答案 1 :(得分:0)

我只使用image-gd库编写了一个非常简单的php脚本。它读取来自textarea公式的文本,并使用Ascii-Value和一些乘数函数为字符分配颜色,以使近邻Ascii之间的颜色差异如“a”和“b”可见。现在它只适用于已知的文本大小。

<?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); 
}
?>

您可以在此处找到在线工作工具:http://www.webspice.eu/ascii2png/

答案 2 :(得分:0)

使用 Java 从 ASCII 艺术作品中恢复图像

假设我们有组成 ASCII 图像的字符的密度比例,因此我们可以从中恢复灰度位图。并且假设每个字符占据21×8像素的区域,所以在恢复的时候,我们要放大图片。

ASCII 文本 (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 图片(截图):

ASCII picture

恢复图片:

Restored picture


这段代码读取一个文本文件,从字符密度中获得一个亮度值,从中创建灰度颜色,在高度和宽度上重复每种颜色 21 次,然后将图片保存为灰度位图。

>

不缩放scH=1scW=1,像素数等于原始文本文件中的字符数。

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

另见:Draw an ASCII art from an image