py-img-processor


Namepy-img-processor JSON
Version 1.0.3 PyPI version JSON
download
home_pagehttps://github.com/SkylerHu/py-img-processor.git
SummaryImage editor using Python and Pillow.
upload_time2024-10-23 15:35:28
maintainerNone
docs_urlNone
authorSkylerHu
requires_python>=3.9
licenseMIT Licence
keywords image img-processor image-processor imgprocessor img-editor image-editor
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # py-img-processor

[![PyPI - Version](https://img.shields.io/pypi/v/py-img-processor)](https://github.com/SkylerHu/py-img-processor)
[![GitHub Actions Workflow Status](https://github.com/SkylerHu/py-img-processor/actions/workflows/pre-commit.yml/badge.svg?branch=master)](https://github.com/SkylerHu/py-img-processor)
[![GitHub Actions Workflow Status](https://github.com/SkylerHu/py-img-processor/actions/workflows/test-py3.yml/badge.svg?branch=master)](https://github.com/SkylerHu/py-img-processor)
[![Coveralls](https://img.shields.io/coverallsCoverage/github/SkylerHu/py-img-processor?branch=master)](https://github.com/SkylerHu/py-img-processor)
[![PyPI - Wheel](https://img.shields.io/pypi/wheel/py-img-processor)](https://github.com/SkylerHu/py-img-processor)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/py-img-processor)](https://github.com/SkylerHu/py-img-processor)
[![PyPI - Implementation](https://img.shields.io/pypi/implementation/py-img-processor)](https://github.com/SkylerHu/py-img-processor)
[![GitHub License](https://img.shields.io/github/license/SkylerHu/py-img-processor)](https://github.com/SkylerHu/py-img-processor)


Image editor using Python and Pillow.

依赖Pillow开发的Python库,用于图像编辑处理。


## 1. 安装

	pip install py-img-processor

依赖:

- `Python >= 3.9`
- `Pillow >= 8.0.0`

可查看版本变更记录 [ChangeLog](https://github.com/SkylerHu/py-img-processor/blob/master/docs/CHANGELOG-1.x.md)

## 2. 使用(Usage)

具体使用说明查看 [readthedocs](https://py-img-processor.readthedocs.io) 。

## 2.1 运行配置
可以通过指定环境变量`PY_SETTINGS_MODULE`加载配置文件:

    export PY_SETTINGS_MODULE=${your_project.settings_file.py}

支持的配置项有:

| 配置项 | 类型 | 说明 | 默认值 |
| - | - | - | - |
| DEBUG | bool | 是否debug开发模式 | False |
| PROCESSOR_MAX_FILE_SIZE | int | 处理原图的大小限制, 单位 MB | 20 |
| PROCESSOR_MAX_W_H | int | 处理图像,原图宽高像素限制 | 30000 |
| PROCESSOR_MAX_PIXEL | int | width x height总像素3亿,处理前后的值都被此配置限制 | 300000000 |
| PROCESSOR_DEFAULT_QUALITY | int | 图像处理后的默认质量 | 75 |
| PROCESSOR_TEXT_FONT | str | 默认字体文件,默认从系统中寻找;也可以直接传递字体文件路径 | Arial Unicode.ttf |

> `注意`:`PROCESSOR_TEXT_FONT` 字体的设置是文字水印必要参数,需保证系统已安装该字体。默认值 `Arial Unicode.ttf` 是MacOS系统存在的字体,建议设置字体文件路径。

## 2.2 图像处理

测试图片 `lenna-400x225.jpg` (像素400x225)

![](https://github.com/SkylerHu/py-img-processor/blob/master/docs/imgs/lenna-400x225.jpg)


### 处理函数
`process_image_by_path(input_path, out_path, params)`

参数说明:

- `input_path` str,输入图像文件路径
- `out_path` str, 输出图像保存路径
- `params` str or json,图像处理参数,参数说明详见 [Reference.md](https://github.com/SkylerHu/py-img-processor/blob/master/docs/Reference.md)


### 图像处理参数为字符串

- 斜线 `/` 隔开,区分不同的操作;
- 逗号 `,` 隔开,区分操作中不同的参数;
- 下划线 `_` 隔开,`key_value` 的形式,区分参数的Key和Value;
- `value`是复杂参数时,需要进行`base64url_encode`,是否需要encode查看文档参数详细说明;

```python
from imgprocessor.utils import base64url_encode
from imgprocessor.processor import process_image_by_path

process_image_by_path(
    "docs/imgs/lenna-400x225.jpg",
    "/tmp/output.png",
    # 对图片缩放、裁剪、生成圆角、并转成png存储
    f"resize,s_200/crop,w_200,h_200,g_center/watermark,text_{base64url_encode('Hello 世界')},color_FFF,size_20/circle,r_10/format,png",
)
```

输出图像 (像素200x200):

![](https://github.com/SkylerHu/py-img-processor/blob/master/docs/imgs/lenna-edit.png)

### 图像处理参数为JSON
- 只是形式不同,参数和字符串形式无本质区别;
- `format`、`quality`、`interlace`三个值在JSON第一层,直接按照`key: value`的形式设置;
- 其他参数都放在 `actions` 数组中;

```python
process_image_by_path(
    "docs/imgs/lenna-400x225.jpg",
    "/tmp/output.png",
    {
        "actions": [
            {"key": "resize", "s": 200},
            {"key": "crop", "w": 200, "h": 200, "g": "center"},
            # JSON形式参数, text无需encode
            {"key": "watermark", "text": "Hello 世界", "color": "FFF", "size": 20},
            {"key": "circle", "r": 10},
        ],
        "format": "png",
    },
)
```
该操作与上述字符串示例参数等效。

## 命令行
安装库后 有可执行命令 `img-processor` 可以使用,通过 `img-processor -h` 查看参数说明。

```shell
usage: img-processor [-h] [-V] -P PATH [--action ACTION [ACTION ...]] -O OUTPUT [--overwrite]

图像处理

optional arguments:
  -h, --help            show this help message and exit
  -V, --version         show program's version number and exit
  -P PATH, --path PATH  输入图像的文件路径/目录,若是目录则批量处理目录下所有图像
  --action ACTION [ACTION ...]
                        操作参数,可对同一个文件多组操作
  -O OUTPUT, --output OUTPUT
                        输出图像路径,多个图像或多个操作时请设置已存在的目录
  --overwrite           是否覆盖输出路径中已有文件
```

示例:
```shell
# 对单个图像进行多个操作,actions有2个参数,会输出2个图像文件
img-processor -P docs/imgs/lenna-400x225.jpg -O /tmp/ --action resize,s_200/format,webp resize,s_225/crop,w_225,h_225,g_center/circle/format,png --overwrite
```

> 注意:action参数仅支持字符串表达形式。

会输出2个图像文件:

`/tmp/lenna-400x225-0.webp` (像素355x200)

![](https://github.com/SkylerHu/py-img-processor/blob/master/docs/imgs/lenna-400x225-0.webp)

`/tmp/lenna-400x225-1.png` (像素225x225)

![](https://github.com/SkylerHu/py-img-processor/blob/master/docs/imgs/lenna-400x225-1.png)


## 提取图像主色调
```python
from imgprocessor.processor import extract_main_color

extract_main_color("docs/imgs/lenna-400x225.jpg")
# 输出: "905C4C"
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/SkylerHu/py-img-processor.git",
    "name": "py-img-processor",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "image, img-processor, image-processor, imgprocessor, img-editor, image-editor",
    "author": "SkylerHu",
    "author_email": "skylerhu@qq.com",
    "download_url": "https://files.pythonhosted.org/packages/a6/95/8b5eac971cda3314c572595dd379b5339f81d984717029d258a0ed004195/py_img_processor-1.0.3.tar.gz",
    "platform": "any",
    "description": "# py-img-processor\n\n[![PyPI - Version](https://img.shields.io/pypi/v/py-img-processor)](https://github.com/SkylerHu/py-img-processor)\n[![GitHub Actions Workflow Status](https://github.com/SkylerHu/py-img-processor/actions/workflows/pre-commit.yml/badge.svg?branch=master)](https://github.com/SkylerHu/py-img-processor)\n[![GitHub Actions Workflow Status](https://github.com/SkylerHu/py-img-processor/actions/workflows/test-py3.yml/badge.svg?branch=master)](https://github.com/SkylerHu/py-img-processor)\n[![Coveralls](https://img.shields.io/coverallsCoverage/github/SkylerHu/py-img-processor?branch=master)](https://github.com/SkylerHu/py-img-processor)\n[![PyPI - Wheel](https://img.shields.io/pypi/wheel/py-img-processor)](https://github.com/SkylerHu/py-img-processor)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/py-img-processor)](https://github.com/SkylerHu/py-img-processor)\n[![PyPI - Implementation](https://img.shields.io/pypi/implementation/py-img-processor)](https://github.com/SkylerHu/py-img-processor)\n[![GitHub License](https://img.shields.io/github/license/SkylerHu/py-img-processor)](https://github.com/SkylerHu/py-img-processor)\n\n\nImage editor using Python and Pillow.\n\n\u4f9d\u8d56Pillow\u5f00\u53d1\u7684Python\u5e93\uff0c\u7528\u4e8e\u56fe\u50cf\u7f16\u8f91\u5904\u7406\u3002\n\n\n## 1. \u5b89\u88c5\n\n\tpip install py-img-processor\n\n\u4f9d\u8d56\uff1a\n\n- `Python >= 3.9`\n- `Pillow >= 8.0.0`\n\n\u53ef\u67e5\u770b\u7248\u672c\u53d8\u66f4\u8bb0\u5f55 [ChangeLog](https://github.com/SkylerHu/py-img-processor/blob/master/docs/CHANGELOG-1.x.md)\n\n## 2. \u4f7f\u7528(Usage)\n\n\u5177\u4f53\u4f7f\u7528\u8bf4\u660e\u67e5\u770b [readthedocs](https://py-img-processor.readthedocs.io) \u3002\n\n## 2.1 \u8fd0\u884c\u914d\u7f6e\n\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u73af\u5883\u53d8\u91cf`PY_SETTINGS_MODULE`\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\uff1a\n\n    export PY_SETTINGS_MODULE=${your_project.settings_file.py}\n\n\u652f\u6301\u7684\u914d\u7f6e\u9879\u6709\uff1a\n\n| \u914d\u7f6e\u9879 | \u7c7b\u578b | \u8bf4\u660e | \u9ed8\u8ba4\u503c |\n| - | - | - | - |\n| DEBUG | bool | \u662f\u5426debug\u5f00\u53d1\u6a21\u5f0f | False |\n| PROCESSOR_MAX_FILE_SIZE | int | \u5904\u7406\u539f\u56fe\u7684\u5927\u5c0f\u9650\u5236\uff0c \u5355\u4f4d MB | 20 |\n| PROCESSOR_MAX_W_H | int | \u5904\u7406\u56fe\u50cf\uff0c\u539f\u56fe\u5bbd\u9ad8\u50cf\u7d20\u9650\u5236 | 30000 |\n| PROCESSOR_MAX_PIXEL | int | width x height\u603b\u50cf\u7d203\u4ebf\uff0c\u5904\u7406\u524d\u540e\u7684\u503c\u90fd\u88ab\u6b64\u914d\u7f6e\u9650\u5236 | 300000000 |\n| PROCESSOR_DEFAULT_QUALITY | int | \u56fe\u50cf\u5904\u7406\u540e\u7684\u9ed8\u8ba4\u8d28\u91cf | 75 |\n| PROCESSOR_TEXT_FONT | str | \u9ed8\u8ba4\u5b57\u4f53\u6587\u4ef6\uff0c\u9ed8\u8ba4\u4ece\u7cfb\u7edf\u4e2d\u5bfb\u627e\uff1b\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f20\u9012\u5b57\u4f53\u6587\u4ef6\u8def\u5f84 | Arial Unicode.ttf |\n\n> `\u6ce8\u610f`\uff1a`PROCESSOR_TEXT_FONT` \u5b57\u4f53\u7684\u8bbe\u7f6e\u662f\u6587\u5b57\u6c34\u5370\u5fc5\u8981\u53c2\u6570\uff0c\u9700\u4fdd\u8bc1\u7cfb\u7edf\u5df2\u5b89\u88c5\u8be5\u5b57\u4f53\u3002\u9ed8\u8ba4\u503c `Arial Unicode.ttf` \u662fMacOS\u7cfb\u7edf\u5b58\u5728\u7684\u5b57\u4f53\uff0c\u5efa\u8bae\u8bbe\u7f6e\u5b57\u4f53\u6587\u4ef6\u8def\u5f84\u3002\n\n## 2.2 \u56fe\u50cf\u5904\u7406\n\n\u6d4b\u8bd5\u56fe\u7247 `lenna-400x225.jpg` (\u50cf\u7d20400x225)\n\n![](https://github.com/SkylerHu/py-img-processor/blob/master/docs/imgs/lenna-400x225.jpg)\n\n\n### \u5904\u7406\u51fd\u6570\n`process_image_by_path(input_path, out_path, params)`\n\n\u53c2\u6570\u8bf4\u660e\uff1a\n\n- `input_path` str\uff0c\u8f93\u5165\u56fe\u50cf\u6587\u4ef6\u8def\u5f84\n- `out_path` str, \u8f93\u51fa\u56fe\u50cf\u4fdd\u5b58\u8def\u5f84\n- `params` str or json\uff0c\u56fe\u50cf\u5904\u7406\u53c2\u6570\uff0c\u53c2\u6570\u8bf4\u660e\u8be6\u89c1 [Reference.md](https://github.com/SkylerHu/py-img-processor/blob/master/docs/Reference.md)\n\n\n### \u56fe\u50cf\u5904\u7406\u53c2\u6570\u4e3a\u5b57\u7b26\u4e32\n\n- \u659c\u7ebf `/` \u9694\u5f00\uff0c\u533a\u5206\u4e0d\u540c\u7684\u64cd\u4f5c\uff1b\n- \u9017\u53f7 `,` \u9694\u5f00\uff0c\u533a\u5206\u64cd\u4f5c\u4e2d\u4e0d\u540c\u7684\u53c2\u6570\uff1b\n- \u4e0b\u5212\u7ebf `_` \u9694\u5f00\uff0c`key_value` \u7684\u5f62\u5f0f\uff0c\u533a\u5206\u53c2\u6570\u7684Key\u548cValue\uff1b\n- `value`\u662f\u590d\u6742\u53c2\u6570\u65f6\uff0c\u9700\u8981\u8fdb\u884c`base64url_encode`\uff0c\u662f\u5426\u9700\u8981encode\u67e5\u770b\u6587\u6863\u53c2\u6570\u8be6\u7ec6\u8bf4\u660e\uff1b\n\n```python\nfrom imgprocessor.utils import base64url_encode\nfrom imgprocessor.processor import process_image_by_path\n\nprocess_image_by_path(\n    \"docs/imgs/lenna-400x225.jpg\",\n    \"/tmp/output.png\",\n    # \u5bf9\u56fe\u7247\u7f29\u653e\u3001\u88c1\u526a\u3001\u751f\u6210\u5706\u89d2\u3001\u5e76\u8f6c\u6210png\u5b58\u50a8\n    f\"resize,s_200/crop,w_200,h_200,g_center/watermark,text_{base64url_encode('Hello \u4e16\u754c')},color_FFF,size_20/circle,r_10/format,png\",\n)\n```\n\n\u8f93\u51fa\u56fe\u50cf (\u50cf\u7d20200x200)\uff1a\n\n![](https://github.com/SkylerHu/py-img-processor/blob/master/docs/imgs/lenna-edit.png)\n\n### \u56fe\u50cf\u5904\u7406\u53c2\u6570\u4e3aJSON\n- \u53ea\u662f\u5f62\u5f0f\u4e0d\u540c\uff0c\u53c2\u6570\u548c\u5b57\u7b26\u4e32\u5f62\u5f0f\u65e0\u672c\u8d28\u533a\u522b\uff1b\n- `format`\u3001`quality`\u3001`interlace`\u4e09\u4e2a\u503c\u5728JSON\u7b2c\u4e00\u5c42\uff0c\u76f4\u63a5\u6309\u7167`key: value`\u7684\u5f62\u5f0f\u8bbe\u7f6e\uff1b\n- \u5176\u4ed6\u53c2\u6570\u90fd\u653e\u5728 `actions` \u6570\u7ec4\u4e2d\uff1b\n\n```python\nprocess_image_by_path(\n    \"docs/imgs/lenna-400x225.jpg\",\n    \"/tmp/output.png\",\n    {\n        \"actions\": [\n            {\"key\": \"resize\", \"s\": 200},\n            {\"key\": \"crop\", \"w\": 200, \"h\": 200, \"g\": \"center\"},\n            # JSON\u5f62\u5f0f\u53c2\u6570, text\u65e0\u9700encode\n            {\"key\": \"watermark\", \"text\": \"Hello \u4e16\u754c\", \"color\": \"FFF\", \"size\": 20},\n            {\"key\": \"circle\", \"r\": 10},\n        ],\n        \"format\": \"png\",\n    },\n)\n```\n\u8be5\u64cd\u4f5c\u4e0e\u4e0a\u8ff0\u5b57\u7b26\u4e32\u793a\u4f8b\u53c2\u6570\u7b49\u6548\u3002\n\n## \u547d\u4ee4\u884c\n\u5b89\u88c5\u5e93\u540e \u6709\u53ef\u6267\u884c\u547d\u4ee4 `img-processor` \u53ef\u4ee5\u4f7f\u7528\uff0c\u901a\u8fc7 `img-processor -h` \u67e5\u770b\u53c2\u6570\u8bf4\u660e\u3002\n\n```shell\nusage: img-processor [-h] [-V] -P PATH [--action ACTION [ACTION ...]] -O OUTPUT [--overwrite]\n\n\u56fe\u50cf\u5904\u7406\n\noptional arguments:\n  -h, --help            show this help message and exit\n  -V, --version         show program's version number and exit\n  -P PATH, --path PATH  \u8f93\u5165\u56fe\u50cf\u7684\u6587\u4ef6\u8def\u5f84/\u76ee\u5f55\uff0c\u82e5\u662f\u76ee\u5f55\u5219\u6279\u91cf\u5904\u7406\u76ee\u5f55\u4e0b\u6240\u6709\u56fe\u50cf\n  --action ACTION [ACTION ...]\n                        \u64cd\u4f5c\u53c2\u6570\uff0c\u53ef\u5bf9\u540c\u4e00\u4e2a\u6587\u4ef6\u591a\u7ec4\u64cd\u4f5c\n  -O OUTPUT, --output OUTPUT\n                        \u8f93\u51fa\u56fe\u50cf\u8def\u5f84\uff0c\u591a\u4e2a\u56fe\u50cf\u6216\u591a\u4e2a\u64cd\u4f5c\u65f6\u8bf7\u8bbe\u7f6e\u5df2\u5b58\u5728\u7684\u76ee\u5f55\n  --overwrite           \u662f\u5426\u8986\u76d6\u8f93\u51fa\u8def\u5f84\u4e2d\u5df2\u6709\u6587\u4ef6\n```\n\n\u793a\u4f8b\uff1a\n```shell\n# \u5bf9\u5355\u4e2a\u56fe\u50cf\u8fdb\u884c\u591a\u4e2a\u64cd\u4f5c\uff0cactions\u67092\u4e2a\u53c2\u6570\uff0c\u4f1a\u8f93\u51fa2\u4e2a\u56fe\u50cf\u6587\u4ef6\nimg-processor -P docs/imgs/lenna-400x225.jpg -O /tmp/ --action resize,s_200/format,webp resize,s_225/crop,w_225,h_225,g_center/circle/format,png --overwrite\n```\n\n> \u6ce8\u610f\uff1aaction\u53c2\u6570\u4ec5\u652f\u6301\u5b57\u7b26\u4e32\u8868\u8fbe\u5f62\u5f0f\u3002\n\n\u4f1a\u8f93\u51fa2\u4e2a\u56fe\u50cf\u6587\u4ef6\uff1a\n\n`/tmp/lenna-400x225-0.webp` (\u50cf\u7d20355x200)\n\n![](https://github.com/SkylerHu/py-img-processor/blob/master/docs/imgs/lenna-400x225-0.webp)\n\n`/tmp/lenna-400x225-1.png` (\u50cf\u7d20225x225)\n\n![](https://github.com/SkylerHu/py-img-processor/blob/master/docs/imgs/lenna-400x225-1.png)\n\n\n## \u63d0\u53d6\u56fe\u50cf\u4e3b\u8272\u8c03\n```python\nfrom imgprocessor.processor import extract_main_color\n\nextract_main_color(\"docs/imgs/lenna-400x225.jpg\")\n# \u8f93\u51fa\uff1a \"905C4C\"\n```\n",
    "bugtrack_url": null,
    "license": "MIT Licence",
    "summary": "Image editor using Python and Pillow.",
    "version": "1.0.3",
    "project_urls": {
        "Homepage": "https://github.com/SkylerHu/py-img-processor.git"
    },
    "split_keywords": [
        "image",
        " img-processor",
        " image-processor",
        " imgprocessor",
        " img-editor",
        " image-editor"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b3c01be12550a810aa145ebb7286bcce429e650512932bfd640d7bd01df3c682",
                "md5": "e455c00326cf4e760c789928ea4f2b0a",
                "sha256": "79e2772a4f967cc2c3dbf9f80589700460790f89b50508bab15dab8095b7f945"
            },
            "downloads": -1,
            "filename": "py_img_processor-1.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e455c00326cf4e760c789928ea4f2b0a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 28748,
            "upload_time": "2024-10-23T15:35:25",
            "upload_time_iso_8601": "2024-10-23T15:35:25.016311Z",
            "url": "https://files.pythonhosted.org/packages/b3/c0/1be12550a810aa145ebb7286bcce429e650512932bfd640d7bd01df3c682/py_img_processor-1.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a6958b5eac971cda3314c572595dd379b5339f81d984717029d258a0ed004195",
                "md5": "bc25e471f35e6bfde991dabe720b377d",
                "sha256": "2c6db3070517e54c5b61a246b1df61fc285506788caaea6a6a89a4cc8eb7e238"
            },
            "downloads": -1,
            "filename": "py_img_processor-1.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "bc25e471f35e6bfde991dabe720b377d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 23076,
            "upload_time": "2024-10-23T15:35:28",
            "upload_time_iso_8601": "2024-10-23T15:35:28.564792Z",
            "url": "https://files.pythonhosted.org/packages/a6/95/8b5eac971cda3314c572595dd379b5339f81d984717029d258a0ed004195/py_img_processor-1.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-23 15:35:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "SkylerHu",
    "github_project": "py-img-processor",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "tox": true,
    "lcname": "py-img-processor"
}
        
Elapsed time: 0.66518s