首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在位图中可视化ASCII文本

在位图中可视化ASCII文本
EN

Stack Overflow用户
提问于 2012-11-14 07:50:15
回答 2查看 2.3K关注 0票数 4

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-19 06:35:48

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

代码语言:javascript
复制
<?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);
}
?>
票数 0
EN

Stack Overflow用户

发布于 2012-11-14 17:45:04

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

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

代码语言:javascript
复制
toPixel[c_String] := toPixel[c] = Mean[Flatten[ImageData[Rasterize[
 Style[c, 30, FontFamily -> "Courier"], "Image", ColorSpace -> "Grayscale"]]]]

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

代码语言:javascript
复制
data = toPixel /@ Characters[#] & /@ StringSplit[text, "\n"];
Image@(PadRight[#, 40, 1] & /@ data) // ImageAdjust

对于此文本

代码语言:javascript
复制
           ,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?$$$$$$$"`
                    `""

我们会得到

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13370897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档