4

我有一个巨大的 ASCII 文本,表示像 ASCII-art 这样的位图。现在我正在寻找类似倒置 ASCII-art 生成器的东西。我喜欢将每个字符转换为彩色像素。

有没有免费的工具可以做这样的事情?

4

3 回答 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?$$$$$$$"`
                    `""

我们得到

数学图形

于 2012-11-14T09:45:03.883 回答
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图片

还原图片:

恢复图片


此代码读取一个文本文件,从字符密度获取亮度值,从中创建灰度颜色,将这些颜色中的每一种重复 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 艺术

于 2021-07-02T02:50:12.693 回答
0

我刚刚使用 image-gd 库编写了一个非常简单的 php 脚本。它从 textarea 公式中读取文本,并使用 ASCII 值和一些乘法器函数为字符分配颜色,以使“a”和“b”等近邻 ASCII 之间的颜色差异可见。目前它只适用于已知的文本大小。

<?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);
}
?>
于 2012-11-18T22:35:48.490 回答