将ASCII文本可视化为位图

4

我有一个用ASCII字符表现出来的像素图,现在我正在寻找一款反转ASCII艺术生成器。我希望将每个字符转换为彩色像素。

是否有任何免费工具可以完成这样的操作?

3个回答

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图形


1

使用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次,在宽度上重复8次,然后将图像保存为灰度位图。
如果不进行缩放 scH=1scW=1,像素数将等于原始文本文件中的字符数。
字符密度比例应与 ASCII 图像构建时使用的比例相同。
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;
  }
}

另见:从图像绘制ASCII艺术将图像转换为ASCII艺术


0

我刚刚编写了一个非常简单的 PHP 脚本,使用了 image-gd 库。它从文本区域表单中读取文本,并根据字符的 ASCII 值和一些乘法函数为字符分配颜色,以使邻近的 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);
}
?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接