From 888f4954af59e45943bf658685435b8228487a23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8F=AF=E5=8F=AF?= Date: Sat, 22 Aug 2020 12:13:03 +0800 Subject: [PATCH 01/13] Update converter.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 适配Python3.8.5(理论上其他Python3.x的版本也能用) --- img2html/converter.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/img2html/converter.py b/img2html/converter.py index 046758c..956ce89 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,8 @@ def __init__(self, self.background = background self.title = title self.font_family = font_family - if isinstance(char, str): - char = char.decode('utf-8') + # if isinstance(char, str): + # char = char.decode('utf-8') self.char = cycle(char) self._prg_cb = progress_callback or _progress_callback @@ -85,19 +85,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 From 8592d0881ad4208f24712cfcc2d5028621bbaf0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8F=AF=E5=8F=AF?= Date: Sat, 22 Aug 2020 12:14:31 +0800 Subject: [PATCH 02/13] =?UTF-8?q?=E9=80=82=E9=85=8DPython3.8.5=EF=BC=88?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E4=B8=8A=E5=85=B6=E4=BB=96Python3.x=E4=B9=9F?= =?UTF-8?q?=E9=80=9A=E7=94=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 适配Python3.8.5(理论上其他Python3.x也通用) --- img2html/converter.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/img2html/converter.py b/img2html/converter.py index 956ce89..a03aee4 100644 --- a/img2html/converter.py +++ b/img2html/converter.py @@ -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 From d3f753d63426c6e6d5a87ba3f4b61982552e223b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8F=AF=E5=8F=AF?= Date: Sat, 22 Aug 2020 12:18:02 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=E9=80=82=E9=85=8DPython3.8.5=EF=BC=88?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E4=B8=8A=E5=85=B6=E4=BB=96Python3.x=E4=B9=9F?= =?UTF-8?q?=E9=80=9A=E7=94=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 适配Python3.8.5(理论上其他Python3.x也通用) --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 411f4c2..0179873 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ html = converter.convert(*image_path*) ### 安装 --- +对于Python2.x来说 + `img2html` 已经上传到了 [PYPI](https://pypi.python.org/pypi/img2html),所以最简单的安装方式就是使用 pip: ``` @@ -74,11 +76,10 @@ $ pip install img2html $ pip install img2html --upgrade ``` - -当然,你也可以通过源码安装: +对于Python3.x来说,由于作者没有适配Python3.x,因此应通过我写的源码安装: ``` -$ git clone https://github.com/xlzd/img2html.git +$ git clone https://github.com/cocos56/img2html.git $ cd img2html $ python setup.py install ``` From c8ba728f94df17f0c3da7c8bb644727f5e36b99a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8F=AF=E5=8F=AF?= Date: Sat, 22 Aug 2020 12:21:09 +0800 Subject: [PATCH 04/13] Update README.md --- README.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0179873..8db8327 100644 --- a/README.md +++ b/README.md @@ -18,14 +18,10 @@ ### 示例 -转换后的 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://github.com/cocos56/img2html/blob/master/demo/before.png) | ![](https://github.com/cocos56/img2html/blob/master/demo/after.png) +![](https://github.com/cocos56/img2html/blob/master/demo/before2.png) | ![](https://github.com/cocos56/img2html/blob/master/demo/after2.png) ### 使用方式 --- From 98d175640f605a4a3847bf99f912135c9fedfb07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8F=AF=E5=8F=AF?= Date: Sat, 22 Aug 2020 12:22:07 +0800 Subject: [PATCH 05/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8db8327..a4d636f 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ 原始图片 | 转换后 :-------------------------:|:-------------------------: -![](https://github.com/cocos56/img2html/blob/master/demo/before.png) | ![](https://github.com/cocos56/img2html/blob/master/demo/after.png) +![](https://github.com/cocos56/img2html/blob/master/demo/before.png) | ![在这里插入图片描述](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://github.com/cocos56/img2html/blob/master/demo/before2.png) | ![](https://github.com/cocos56/img2html/blob/master/demo/after2.png) ### 使用方式 --- From e9ab4b84278d1f406f1216d166e75151f39a2e66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8F=AF=E5=8F=AF?= Date: Sat, 22 Aug 2020 12:24:09 +0800 Subject: [PATCH 06/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=A4=BA=E4=BE=8B?= =?UTF-8?q?=E5=92=8C=E5=AE=89=E8=A3=85=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新示例和安装说明 --- README.md | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a4d636f..54c1e26 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ 原始图片 | 转换后 :-------------------------:|:-------------------------: -![](https://github.com/cocos56/img2html/blob/master/demo/before.png) | ![在这里插入图片描述](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://github.com/cocos56/img2html/blob/master/demo/before2.png) | ![](https://github.com/cocos56/img2html/blob/master/demo/after2.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) ### 使用方式 --- @@ -57,6 +57,14 @@ html = converter.convert(*image_path*) ### 安装 --- +对于Python3.x来说,由于作者没有适配Python3.x,因此应通过我写的源码安装: + +``` +$ git clone https://github.com/cocos56/img2html.git +$ cd img2html +$ python setup.py install +``` + 对于Python2.x来说 @@ -72,15 +80,6 @@ $ pip install img2html $ pip install img2html --upgrade ``` -对于Python3.x来说,由于作者没有适配Python3.x,因此应通过我写的源码安装: - -``` -$ git clone https://github.com/cocos56/img2html.git -$ cd img2html -$ python setup.py install -``` - - ### License --- From 7363109f8304e7a19c1451fd65a4e5dec21d20a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8F=AF=E5=8F=AF?= Date: Sat, 22 Aug 2020 12:30:10 +0800 Subject: [PATCH 07/13] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=E4=BD=BF=E7=94=A8=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加命令行使用实例 --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 54c1e26..7caf8ef 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ --- #### 命令行 +使用帮助: ``` usage: img2html [-h] [-b #RRGGBB] [-s 4~30] [-c CHAR] [-t TITLE] [-f FONT] -i IN [-o OUT] @@ -42,6 +43,10 @@ optional arguments: -o OUT, --out OUT output file ``` +使用实例: +直接在命令行中输入`img2html -i 图片路径 -o 网页路径` +如:`img2html -i D:\before4.png -o D:\index.html` + #### 代码调用 From 1d8b055b18fce19c792623e2f2872816d3007d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8F=AF=E5=8F=AF?= Date: Sat, 22 Aug 2020 12:31:31 +0800 Subject: [PATCH 08/13] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 7caf8ef..06c32d4 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,9 @@ optional arguments: 使用实例: 直接在命令行中输入`img2html -i 图片路径 -o 网页路径` + 如:`img2html -i D:\before4.png -o D:\index.html` +![在这里插入图片描述](https://img-blog.csdnimg.cn/20200822123102209.png#pic_center) #### 代码调用 From 39f177f8520160c84e6975b19752b3e4423a2d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8F=AF=E5=8F=AF?= Date: Sat, 22 Aug 2020 12:32:11 +0800 Subject: [PATCH 09/13] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=E4=BD=BF=E7=94=A8=E5=AE=9E=E4=BE=8B=E6=BC=94=E7=A4=BA?= =?UTF-8?q?=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加命令行使用实例演示图 --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 06c32d4..5374c3a 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ optional arguments: 直接在命令行中输入`img2html -i 图片路径 -o 网页路径` 如:`img2html -i D:\before4.png -o D:\index.html` + ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200822123102209.png#pic_center) From e00a50448bfd31cfd83d632309b8f706ac43a51e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8F=AF=E5=8F=AF?= Date: Sat, 22 Aug 2020 12:35:09 +0800 Subject: [PATCH 10/13] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=B8=E8=BD=BD?= =?UTF-8?q?=E5=BA=93=E7=9A=84=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加卸载库的说明 --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 5374c3a..5c6d926 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,11 @@ $ pip install img2html $ pip install img2html --upgrade ``` +卸载: +``` +pip uninstall img2html +``` + ### License --- From 3ca3973f784bd77bd9fdecf7bc90a398c7fd6732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8F=AF=E5=8F=AF?= Date: Sat, 22 Aug 2020 12:37:02 +0800 Subject: [PATCH 11/13] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=E5=91=BD=E4=BB=A4=E4=B8=AD=E7=9A=84=E4=B8=8D=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E5=AD=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 删除命令行命令中的不必要字符 --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5c6d926..c5b2b65 100644 --- a/README.md +++ b/README.md @@ -68,9 +68,9 @@ html = converter.convert(*image_path*) 对于Python3.x来说,由于作者没有适配Python3.x,因此应通过我写的源码安装: ``` -$ git clone https://github.com/cocos56/img2html.git -$ cd img2html -$ python setup.py install +git clone https://github.com/cocos56/img2html.git +cd img2html +python setup.py install ``` @@ -79,13 +79,13 @@ $ python setup.py install `img2html` 已经上传到了 [PYPI](https://pypi.python.org/pypi/img2html),所以最简单的安装方式就是使用 pip: ``` -$ pip install img2html +pip install img2html ``` 更新: ``` -$ pip install img2html --upgrade +pip install img2html --upgrade ``` 卸载: From a4213e76c6173788b65bccfabd9321eb61bf76fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8F=AF=E5=8F=AF?= Date: Sat, 22 Aug 2020 12:40:31 +0800 Subject: [PATCH 12/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=E4=BD=BF=E7=94=A8=E5=AE=9E=E4=BE=8B=E7=9A=84=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新命令行使用实例的图片文件名 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c5b2b65..e44161f 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ optional arguments: 使用实例: 直接在命令行中输入`img2html -i 图片路径 -o 网页路径` -如:`img2html -i D:\before4.png -o D:\index.html` +如:`img2html -i D:\before.png -o D:\index.html` ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200822123102209.png#pic_center) From 43742e29aa403cb280137c674718ffcc85ba634e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8F=AF=E5=8F=AF?= Date: Sat, 22 Aug 2020 13:09:55 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新代码调用示例 --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e44161f..c8e1723 100644 --- a/README.md +++ b/README.md @@ -55,11 +55,12 @@ optional arguments: ```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) ```