为网络优化图像的小程序包。
web-mage的Python项目详细描述
网页图像
web mage是一个简单的python包,用于自动化流程 为网络优化图像文件。可以导入和定制 在python模块中,或者作为命令行实用程序运行。
该项目是功能性的,但仍处于早期开发阶段。
安装
使用pip
:
pip install web-mage
用法
python3 -m web_mage SOURCE_DIR DEST_DIR
这将在不调整图像大小的情况下,将SOURCE_DIR
中的所有文件优化到60%的质量,
并用文件名后缀将它们输出到DEST_DIR
。
代码中的用法
这是首选的使用方法。您可以将web mage导入 如果要在脚本或程序中运行作业,或自定义 图像优化的方式。
fromweb_mage.web_mageimportJobfromweb_mage.formatsimportIMG_FORMAT_CONTENT_LARGEformats=[IMG_FORMAT_CONTENT_LARGE]optim_job=Job(source="/path/to/source/directory",dest="/path/to/output/directory",formats=formats)optim_job.run()
如果要优化图像,可以使用多种格式 多个不同的设备或布局。你也可以定义自己的 基于几个不同参数的图像格式。
fromweb_mage.formatsimport(ImageFormat,IMG_FORMAT_CONTENT_LARGE,IMG_FORMAT_CONTENT_MEDIUM,IMG_FORMAT_CONTENT_SMALL)my_format=ImageFormat(max_width=1024,# Image will be resized based on dimensions.max_height=768,min_quality=85,# This will optimize to 85% quality.tag="my_format")# Tags are appended to the filename of the# resulting file so multiple output formats# can be distinguished.my_other_format=ImageFormat(max_dimension=1280,# This will ensure that neither height# nor width will exceed 1280 pixels.tag="my_other_format")formats=[IMG_FORMAT_CONTENT_LARGE,IMG_FORMAT_CONTENT_MEDIUM,IMG_FORMAT_CONTENT_SMALL,my_format,my_other_format]