Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
imgyaso
提交
2a1be96a
I
imgyaso
项目概览
OpenDocCN
/
imgyaso
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
I
imgyaso
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
2a1be96a
编写于
7月 04, 2020
作者:
W
wizardforcel
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
upgrade quant mode
上级
8d53062b
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
34 addition
and
24 deletion
+34
-24
README.md
README.md
+3
-3
imgyaso/__main__.py
imgyaso/__main__.py
+1
-1
imgyaso/quant.py
imgyaso/quant.py
+28
-20
requirements.txt
requirements.txt
+2
-0
未找到文件。
README.md
浏览文件 @
2a1be96a
...
...
@@ -4,7 +4,7 @@
## 安装
通过pip(推荐):
通过
pip(推荐):
```
pip install imgyaso
...
...
@@ -16,7 +16,7 @@ pip install imgyaso
pip install git+https://github.com/apachecn/imgyaso
```
并且安装
[
ImageMagick
](
https://imagemagick.org/script/download.php
)
和
[
pngquant
](
https://pngquant.org/
)
。
并且安装
[
ImageMagick
](
https://imagemagick.org/script/download.php
)
。
## 使用说明
...
...
@@ -28,7 +28,7 @@ imgyaso [-h] [-v] [-c COLORS] [-m {grid,noise,trunc,quant,thres}]
-c COLORS: 颜色数,只对 trunc, quant 模式有效
-m MODE: 模式名称,值为 grid(灰度网格仿色),noise(灰度扩散仿色),
trunc(灰度截断),quant(颜色缩减),thres(自适应二值化),
默认为
grid
默认为
quant
-o OFNAME: 输出文件名称,默认和输入文件相同
fname: 输入文件名称
```
...
...
imgyaso/__main__.py
浏览文件 @
2a1be96a
...
...
@@ -8,7 +8,7 @@ from .quant import pngquant_bts
from
.trunc
import
trunc_bts
from
.util
import
*
modes
=
[
'
grid'
,
'noise'
,
'trunc'
,
'quant
'
,
'thres'
]
modes
=
[
'
quant'
,
'grid'
,
'noise'
,
'trunc
'
,
'thres'
]
is_img
=
lambda
s
:
re
.
search
(
r
'\.(jpg|jpeg|gif|png|bmp|webp|tiff)$'
,
s
)
...
...
imgyaso/quant.py
浏览文件 @
2a1be96a
import
subprocess
as
subp
import
tempfile
import
uuid
import
os
from
os
import
path
import
sys
import
cv2
import
libimagequant
as
liq
from
PIL
import
Image
from
io
import
BytesIO
from
.util
import
*
def
pngquant
(
img
,
ncolors
=
8
):
...
...
@@ -13,22 +12,31 @@ def pngquant(img, ncolors=8):
def
pngquant_bts
(
img
,
ncolors
=
8
):
img
=
conv2png
(
img
)
fname
=
path
.
join
(
tempfile
.
gettempdir
(),
uuid
.
uuid4
().
hex
+
'.png'
)
with
open
(
fname
,
'wb'
)
as
f
:
f
.
write
(
img
)
subp
.
Popen
(
[
'pngquant'
,
str
(
ncolors
),
fname
,
'-o'
,
fname
,
'-f'
],
stdout
=
subp
.
PIPE
,
stderr
=
subp
.
PIPE
,
).
communicate
()
with
open
(
fname
,
'rb'
)
as
f
:
img
=
f
.
read
()
os
.
unlink
(
fname
)
return
img
img
=
Image
.
open
(
BytesIO
(
img
)).
convert
(
'RGBA'
)
w
,
h
=
img
.
width
,
img
.
height
bytes
=
img
.
tobytes
()
attr
=
liq
.
Attr
()
attr
.
max_colors
=
ncolors
img
=
attr
.
create_rgba
(
bytes
,
w
,
h
,
0
)
res
=
img
.
quantize
(
attr
)
res
.
dithering_level
=
1.0
bytes
=
res
.
remap_image
(
img
)
palette
=
res
.
get_palette
()
img
=
Image
.
frombytes
(
'P'
,
(
w
,
h
),
bytes
)
palette_data
=
[]
for
color
in
palette
:
palette_data
.
append
(
color
.
r
)
palette_data
.
append
(
color
.
g
)
palette_data
.
append
(
color
.
b
)
img
.
putpalette
(
palette_data
)
bio
=
BytesIO
()
img
.
save
(
bio
,
'PNG'
,
optimize
=
True
)
return
bio
.
getvalue
()
def
main
():
fname
=
sys
.
argv
[
1
]
img
=
open
(
fname
,
'rb'
).
read
()
...
...
requirements.txt
浏览文件 @
2a1be96a
numpy
scipy
opencv-python
pillow
libimagequant
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录