diff --git a/README.md b/README.md index 411f4c2..c8e1723 100644 --- a/README.md +++ b/README.md @@ -18,18 +18,15 @@ ### 示例 -转换后的 HTML 页面: [https://xlzd.me/hide/img2html/](https://xlzd.me/hide/img2html/) - - 原始图片 | 转换后 :-------------------------:|:-------------------------: -![](https://raw.githubusercontent.com/xlzd/img2html/master/demo/before2.jpg) | ![](https://raw.githubusercontent.com/xlzd/img2html/master/demo/after2.png) -![](https://raw.githubusercontent.com/xlzd/img2html/master/demo/before.png) | ![](https://raw.githubusercontent.com/xlzd/img2html/master/demo/after.png) - +![在这里插入图片描述](https://img-blog.csdnimg.cn/20200822122243148.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NPQ081Ng==,size_16,color_FFFFFF,t_70#pic_center)|![在这里插入图片描述](https://img-blog.csdnimg.cn/20200822122129328.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NPQ081Ng==,size_16,color_FFFFFF,t_70#pic_center) +![在这里插入图片描述](https://img-blog.csdnimg.cn/20200822122255159.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NPQ081Ng==,size_16,color_FFFFFF,t_70#pic_center)|![在这里插入图片描述](https://img-blog.csdnimg.cn/20200822122141183.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NPQ081Ng==,size_16,color_FFFFFF,t_70#pic_center) ### 使用方式 --- #### 命令行 +使用帮助: ``` usage: img2html [-h] [-b #RRGGBB] [-s 4~30] [-c CHAR] [-t TITLE] [-f FONT] -i IN [-o OUT] @@ -46,44 +43,57 @@ optional arguments: -o OUT, --out OUT output file ``` +使用实例: +直接在命令行中输入`img2html -i 图片路径 -o 网页路径` + +如:`img2html -i D:\before.png -o D:\index.html` + +![在这里插入图片描述](https://img-blog.csdnimg.cn/20200822123102209.png#pic_center) + #### 代码调用 ```Python from img2html.converter import Img2HTMLConverter +import codecs -converter = Img2HTMLConverter(*some config here*) -html = converter.convert(*image_path*) - -# done, so easy. +converter = Img2HTMLConverter() +html = converter.convert(r'D:\before.png') +with codecs.open(r'D:\index.html', 'wb', encoding='utf-8') as fp: + fp.write(html) ``` ### 安装 --- +对于Python3.x来说,由于作者没有适配Python3.x,因此应通过我写的源码安装: + +``` +git clone https://github.com/cocos56/img2html.git +cd img2html +python setup.py install +``` + + +对于Python2.x来说 `img2html` 已经上传到了 [PYPI](https://pypi.python.org/pypi/img2html),所以最简单的安装方式就是使用 pip: ``` -$ pip install img2html +pip install img2html ``` 更新: ``` -$ pip install img2html --upgrade +pip install img2html --upgrade ``` - -当然,你也可以通过源码安装: - +卸载: ``` -$ git clone https://github.com/xlzd/img2html.git -$ cd img2html -$ python setup.py install +pip uninstall img2html ``` - ### License --- diff --git a/img2html/converter.py b/img2html/converter.py index 046758c..a03aee4 100644 --- a/img2html/converter.py +++ b/img2html/converter.py @@ -53,14 +53,14 @@ def _progress_callback(percent): lca = getattr(_progress_callback, '_last_call_at', 0) if time.time() - lca > 0.1: _progress_callback._last_call_at = time.time() - sys.stdout.write('\r{} progress: {:.2f}%'.format(_c.next(), percent)) + sys.stdout.write('\r{} progress: {:.2f}%'.format(_c.__next__(), percent)) sys.stdout.flush() class Img2HTMLConverter(object): def __init__(self, font_size=10, - char='䦗', + char='爱', background='#000000', title='img2html by xlzd', font_family='monospace', @@ -69,8 +69,6 @@ def __init__(self, self.background = background self.title = title self.font_family = font_family - if isinstance(char, str): - char = char.decode('utf-8') self.char = cycle(char) self._prg_cb = progress_callback or _progress_callback @@ -85,19 +83,19 @@ def convert(self, source): progress = 0.0 step = 1. / (col_blocks * row_blocks) - for col in xrange(col_blocks): + for col in range(col_blocks): render_group = RenderGroup() - for row in xrange(row_blocks): + for row in range(row_blocks): pixels = [] - for y in xrange(self.font_size): - for x in xrange(self.font_size): + for y in range(self.font_size): + for x in range(self.font_size): point = Point(row * self.font_size + x, col * self.font_size + y) if point.x >= width or point.y >= height: continue pixels.append(Pixel(*image.getpixel(point)[:3])) average = self.get_average(pixels=pixels) color = self.rgb2hex(average) - render_item = RenderItem(color=color, char=self.char.next()) + render_item = RenderItem(color=color, char=self.char.__next__()) render_group.append(render_item) progress += step