我有一个很大的ASCII文本,表示一个像ASCII-art这样的位图。现在我正在寻找类似于倒置ASCII-art生成器的东西。我喜欢将每个字符转换为彩色像素。
有没有免费的工具可以做这样的事情?
发布于 2012-11-18 22:35:48
我刚刚使用image-gd库编写了一个非常简约的php脚本。它读取文本区域公式中的文本,并使用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);
}
?>
发布于 2012-11-14 09:45:04
您没有使用特定编程语言的标记。因此,Mathematica go..
我使用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?$$$$$$$"`
`""
我们会得到
https://stackoverflow.com/questions/13370897
复制相似问题