切片和缩放9块图像
ninepatch的Python项目详细描述
切片Android风格的9补丁图片,调整大小并交互式预览。
请参阅https://developer.android.com/tools/help/draw9patch.html以获取9个修补程序的说明。
安装
如果要使用交互式查看器,请阅读“交互式查看器”下的附加安装说明。
$ pip install ninepatch
python用法
fromninepatchimportNinepatchninepatch=Ninepatch('ninepatch_bubble.9.png')print(ninepatch.content_area)# content_area(left=23, top=20, right=27, bottom=59)# render the image to a specific sizescaled_image=ninepatch.render(500,400)# creates a new PIL image# render the image so it's content area fits (width, height)image_fit=ninepatch.render_fit(300,200)# render the image so it wraps another PIL imageimage_to_wrap=Image.open('image_to_wrap.png')wrapped_image=ninepatch.render_wrap(image_to_wrap)
命令行用法
您的图像必须是具有透明背景的PNG图像。 刻度和填充向导颜色必须为100%不透明黑色。
在查看器中缩放并打开图像(pil image.show()):
$ ninepatch render ninepatch_bubble.9.png 300 300
将缩放后的图像保存到新文件:
$ ninepatch render ninepatch_bubble.9.png 300 300 scaled.png
渲染图像,使其内容区域适合给定的宽度和高度
$ ninepatch fit ninepatch_bubble.9.png 150 150 fit.png
渲染图像以包含另一个图像
$ ninepatch wrap ninepatch_bubble.9.png image_to_wrap.png wrapped.png
将9块贴片切成瓷砖:
$ ninepatch slice ninepatch_bubble.9.png ./outputdir
交互式查看器
在Tkinter查看器中以交互方式调整和预览图像:
$ ninepatch_viewer ninepatch_bubble.9.png or just: $ ninepatch_viewer without arguments to see the demo image
如果要使用查看器,则必须安装python-pil.imagetk。
在ubuntu上做:
$ sudo apt-get install python-pil.imagetk
如果您想安装到virtualenv中,pip需要以下软件包才能使用tkinter支持编译pil:
$ sudo apt-get install python-tk tk8.6-dev
(您可以使用以下命令触发pil的重新编译:“p i p install-i ninepatch”)
更改日志
- 0.1.20
- 新命令fit和wrap由nicolas laurance提供
- 0.1.19
- 修复了缓存中的错误
- 0.1.18
- slice()和render()的可选缓存
- 0.1.17
- 新方法export_slices()
- 更改的命令行参数(渲染/切片)
…