V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
xrlin
V2EX  ›  Go 编程语言

用 Go 写了一个图片转字符画的小工具

  •  
  •   xrlin · 2017-09-05 20:25:33 +08:00 · 2089 次点击
    这是一个创建于 2396 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看到一些群里发的字符画感觉挺有趣的,然后就想着自己做一个小工具将图片转为字符文本,刚好可以拿 go 来练手, 顺便熟悉下 go 的一些库,在做命令行版本后又做了一个网页版,同时编译打包为一个文件。

    在线测试地址

    GitHub, 提供了 windows、linux、osx 的可执行文件和源码,如果对图片处理或者代码有更好的建议,还请赐教。

    使用方式:

    命令行:

    ./bin/ascii_converter -image-path="./test.png"
    
    # 获取参数帮助信息
    ./bin/ascii_converter -h
     -bg string
            The background of the ascii art image file.(Only useful when image-out i                                                                                            s true, red|gray|green|blue|black are available). (default "black")
      -characters string
            The chars in the value will be used to generate ascii art. (default "M80                                                                                            V1i:*|, ")
      -color string
            The color of the ascii font in image file.(Only useful when image-out is                                                                                             true, red|gray|green|blue|black are available). (default "gray")
      -image-out
            If set to true, will also output the ascii art to an image file
      -image-out-path string
            If imageOut is true, add this option to print the picture to file.
      -image-path string
            The path of the picture file(jpg/png).
      -image-url string
            The url of the picture file(jpg/png).
      -ip string
            The address to bind. (default "127.0.0.1:8080")
      -server
            Set true to enable web ui server
      -sub-height int
            The height of the piece of small rectangle (default 10)
      -sub-width int
            The width of the piece of small rectangle (default 10)
    
    

    使用浏览器:

    # 启动 http 服务
    ./bin/ascii_converter -server=true
    

    启动 http 服务后通过浏览器访问 http://127.0.0.1:8080 可以通过 web 界面进行操作。

    感觉用 go 来写一些小工具挺方便的,跨平台,而且静态资源文件可以通过 go-bindata 生成 go 源文件,可以和源码一起打包成一个可执行文件,直接一个文件就可以发布执行,不需要安装各种依赖,真爽!

    放上一个转换后的例子:
    image

    15 条回复    2017-09-06 12:17:18 +08:00
    zhs227
        1
    zhs227  
       2017-09-05 20:55:21 +08:00
    玩了一下,挺好玩的
    forwind
        2
    forwind  
       2017-09-05 20:59:15 +08:00
    IFoon
        3
    IFoon  
       2017-09-05 21:00:29 +08:00
    @forwind 作大死.
    forwind
        4
    forwind  
       2017-09-05 21:05:20 +08:00
    @IFoon 很酷啊
    xrlin
        5
    xrlin  
    OP
       2017-09-05 21:13:35 +08:00
    @forwind 你这是要搞事情啊( ﹁ ﹁ ) ~→
    kindjeff
        6
    kindjeff  
       2017-09-05 21:19:17 +08:00
    lzhr
        7
    lzhr  
       2017-09-05 21:38:49 +08:00
    @kindjeff #6 真正的粉丝,就算是。。。
    xrlin
        8
    xrlin  
    OP
       2017-09-05 21:40:40 +08:00
    @kindjeff 一颗赛艇
    jakiepaper
        9
    jakiepaper  
       2017-09-05 23:51:33 +08:00 via iPhone
    @kindjeff 还是双马尾的...
    Allianzcortex
        10
    Allianzcortex  
       2017-09-06 00:36:58 +08:00
    star + 1
    Allianzcortex
        11
    Allianzcortex  
       2017-09-06 01:06:48 +08:00
    写的好棒~~计算 grp 这块我稍微测试了一下,用默认的字符集显示效果很好,但如果我随机生成 `dfadfafasfdasfdasdgasdassdlfalgsdjalskd`的话就无法认出来,还是取决于相同 /相近颜色的代码区分度公式啦
    xrlin
        12
    xrlin  
    OP
       2017-09-06 09:03:29 +08:00
    @Allianzcortex 这个确实,并不能很好地将每个范围的像素点映射到一个或几个不相近的字符,如果使用重复的字符就更难处理了。
    Allianzcortex
        13
    Allianzcortex  
       2017-09-06 11:56:50 +08:00
    @xrlin 嗯,字符集不重复但区分度比较低或者太长的话显示效果也不好:D
    Allianzcortex
        14
    Allianzcortex  
       2017-09-06 11:58:00 +08:00
    @xrlin 顺便问一下,这个在线地址的域名是你买的吗还是?
    xrlin
        15
    xrlin  
    OP
       2017-09-06 12:17:18 +08:00 via iPhone
    @Allianzcortex 自己买的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5304 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:58 · PVG 13:58 · LAX 22:58 · JFK 01:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.