- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这样的图像转换算法是如何工作的?
我想将位图转换为 ASCII 艺术。谁能帮我看看我应该使用哪种算法?
. W ,
W W @
W ,W W
, W, :W* .W .
# WW @WW WW #
W WW.WWW WW: W
W. WW*WWW# WW@ W
* :WW.WWWWWWW@WWW@W #
+* #WW#WWWWWWWWWWWWW# W
W# @WWWWWWWWWWWWWWWWW W
WW WWWWWWWWWWWWWWWWWW W
WW WWWWWWWWWWWWWWWWWW@W#
,WW.WWWWWWWWWWWWWWWWWWWWW
WW@WWWWWWWWWWWWWWWWWWWWW
: WWWWWWWWWWWWWWWWWWWWWWWW :
@ WWWWWWWW@WWWWWWW@@WWWWWW.
W*WWWWWW::::@WWW:::::#WWWWW
WWWWWW@:: :+*:. ::@WWWW
WWWWW@:*:.:: .,.:.:WWWW
@WWWW#:.:::. .:: #:@WWW
:WWW@:#. :: :WWWW:@WWWW
WWW#*:W@*@W . W:#WWW
#WWWW:@ :: :: *WWWW
W@WW*W .::,.::::,:+ @@WW#,
WWWW## ,,.: .:::.: . .WWW:,
@WWW@: W..::::: #. :WWWW
WWWW:: *..:. ::.,. :WWWW
WWWW:: :.:.: : :: ,@WW@
WWWW: .:, : ,, :WW,
.: # : , : *
W + ., ::: ., : @
W :: .: W
@,,,W:. ,, ::*@*:, . :@W.,,@
+.....*: : : .#WWWWW: : .#:....+,
@...:::*:,, : :WWWWWWW, , *::::..,#
:...::::::W:, @W::::*W. :W:::::...#
@@@@@@@@@@@W@@@@@W@@@@@@W@@@@@W@@@@@@@@@@:
最佳答案
大小。假设一个字符的平均高度是其宽度的两倍,为了保持相同的比例,我们必须缩小原始图像。因此,使用等宽字体是有意义的。
颜色。我们可以根据字符的密度将像素的亮度转换为字符。因此,转换高对比度的灰度或黑白图像更准确。
原图:
缩放后的 ASCII 图片:
class ImageToASCIIArt {
public static void main(String[] args) throws IOException {
char[][] chars = readImage("/tmp/image.jpg", 16, 8);
writeToFile("/tmp/image.txt", chars);
}
static char[][] readImage(String path, int scH, int scW) throws IOException {
BufferedImage image = ImageIO.read(new File(path));
int height = image.getHeight() / scH;
int width = image.getWidth() / scW;
char[][] chars = new char[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
// scaling image and accumulating colors
int colorRGB = 0;
for (int k = 0; k < scH; k++)
for (int p = 0; p < scW; p++)
colorRGB += image.getRGB(j * scW + p, i * scH + k);
// get the average color
Color color = new Color(colorRGB / (scH * scW));
// read the R, G, B values of the color and get the average brightness
int brightness = (color.getRed()+color.getGreen()+color.getBlue()) / 3;
// get a character depending on the brightness value
chars[i][j] = getDensity(brightness);
}
}
return chars;
}
static final String DENSITY =
"@QB#NgWM8RDHdOKq9$6khEPXwmeZaoS2yjufF]}{tx1zv7lciL/\\|?*>r^;:_\"~,'.-`";
static char getDensity(int value) {
// Since we don't have 255 characters, we have to use percentages
int charValue = (int) Math.round(DENSITY.length() / 255.0 * value);
charValue = Math.max(charValue, 0);
charValue = Math.min(charValue, DENSITY.length() - 1);
return DENSITY.charAt(charValue);
}
static void writeToFile(String path, char[][] chars) throws IOException {
FileWriter writer = new FileWriter(path);
for (char[] row : chars) {
String str = String.valueOf(row);
writer.append(str).write("\n");
System.out.println(str);
}
writer.flush();
writer.close();
}
}
另请参阅:Restore an image from an ASCII art • Draw an ASCII art from an image
关于java - 将位图转换为 ASCII 艺术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31536707/
无论是谁读到这篇文章,我都感谢您花时间阅读。 本质上,我试图通过调用 x 个方法来生成这个精确的 ASCII 艺术,我个人考虑了 3 个: * * ** ** ***
这太琐碎了,听起来很傻,但请多多包涵。 我以前会画ASCII艺术笑脸similar to this post通过做类似的事情: Console.Write((char)1); 当我在 Windows
这样的图像转换算法是如何工作的? 我想将位图转换为 ASCII 艺术。谁能帮我看看我应该使用哪种算法? . W ,
我正在尝试为 android 制作游戏。我目前已将所有美术资源加载到 drawables 文件夹中,但我的问题是如何实际引用特定资源来渲染它? 我知道每个文件都有一个唯一的@id,我可能必须在onDr
我试图垂直反射(reflect)这门艺术。 示例: 但这是我最终得到的: 代码如下: String reverse; for(int i=1;i=0;j--) // probably the wors
我正在尝试使用 JavaScript 函数在我的网站上获取一些 ASCII 艺术作品,但结果并不是我现在想要的...... 它应该是这样的: 这是我用来尝试实现该目标的代码: function log
我正在尝试创建一个脚本,该脚本将接受用户输入(仅字母)并向用户输出 ASCII 艺术。两种输出方法是水平或垂直。我遇到的问题是如何水平打印字典值。 这是我到目前为止所拥有的: def convert(
下面的代码(用 patorjk.com Text to ASCII Art Generator 生成)给出了预期的结果(“TEST”ASCII 艺术文本): Windows:Firefox、Chrom
我正在尝试像这样打印 ascii 艺术: print(("""\ ._ o o
所以..这是(无论如何对我来说)这个程序最重要的功能。我需要这个才能工作。请不要笑..(好吧你可以笑)但是当我的程序出错时,我希望它显示这个: _ _,---._ ,
我正在尝试输入 ASCII art在 C++ 程序中,并通过手动打印每一行来实现,但结果与 ASCII 艺术完全不同。就像: 出现这种情况是因为提示无法识别字符还是我没有正确操作? 这是我正在尝试做的
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
我有一个很长的多行 ascii 艺术字符串,我想使用 Python curses 模块将其呈现给用户。我对此有点困惑,因为在 curses 中打印字符串的唯一方法是 addstr(y,x,string
目前我正在尝试读取可能包含 ASCII 艺术的 .txt 文件并使用 JQuery 在我的 HTML 文档中显示它们。简单的文本显示正确,但我有艺术格式问题。 我该如何解决这个问题? $(docume
我正在用 optparse 模块制作一个 shell 脚本,只是为了好玩,所以我想打印一个漂亮的 ascii 图来代替描述。 原来这段代码: parser = optparse.OptionParse
我是 python 的新手,把它作为一种兴趣爱好,通过一些搜索,我自己找到了一堆来自“计算实践”的练习,其中一个是关于写作的一个 ASCII 数字,如下所示。 这似乎是一个足够简单的练习,但我似乎无法
这个问题在这里已经有了答案: Render a string in HTML and preserve spaces and linebreaks (7 个答案) 关闭 9 个月前。 我该怎么做才能
我曾经为一项工作做过编程测试,其中涉及用 C# 制作 ASCii 艺术。我在这方面做得并不好,因为我对在 C#(或任何编程知识)中这样做的想法或经验知之甚少。 .NET 中是否有任何值得了解/练习的资
我正在编写一个小程序,想知道是否有办法在 R 中包含 ASCII 艺术。我在 python 中寻找等效的三个引号( """ 或 ''' )。 我尝试使用 cat或 print没有成功。 最佳答案 不幸
因此,对于我的 Java 入门类(class),我想用 ASCII 创建一个 Gingerbread 人。这是第一个作业,所以到目前为止,该类(class)仅涵盖了 println 语句。我在 OSX
我是一名优秀的程序员,十分优秀!