NodeImage Python CLI Client
Project description
NodeImage
重要说明: 本项目是 NodeImage 图片托管服务的第三方实现 Python 客户端,非官方实现。如需官方支持或了解服务详情,请访问 NodeImage 官网。
NodeImage 是一个便捷的 Python 命令行客户端,用于与 NodeImage 图片托管服务进行交互。它提供了简洁的命令行接口和 Python API,让您能够在终端中轻松管理您的图片。
系统要求
NodeImage 需要 Python 3.9 或更高版本。
安装
使用 PyPI 安装:
$ pip install nodeimage
从 GitHub 安装(最新提交):
$ pip install "git+https://github.com/0x0208v0/nodeimage.git"
指定分支/标签安装:
# 安装 main 分支
$ pip install "git+https://github.com/0x0208v0/nodeimage.git@main"
# 安装指定标签(示例:v0.0.2)
$ pip install "git+https://github.com/0x0208v0/nodeimage.git@v0.0.2"
命令行界面
NodeImage 提供了丰富的命令行界面:
基本命令
# 查看当前版本
$ nodeimage version
# 上传图片
$ nodeimage upload /path/to/image.jpg
# 从URL上传
$ nodeimage upload https://example.com/image.jpg
# 上传多个文件或整个文件夹
$ nodeimage upload img1.jpg img2.png images/
# 列出所有图片(默认只显示ID)
$ nodeimage list
# 列出图片的完整JSON信息
$ nodeimage list -f json
# 导出为CSV或Excel文件
$ nodeimage list -f csv -o images.csv
$ nodeimage list -f xlsx -o images.xlsx
# 下载图片到本地
$ nodeimage download imageid123
# 下载到指定目录
$ nodeimage download imageid123 -o photos/
# 删除图片
$ nodeimage delete imageid123
# 跳过确认直接删除
$ nodeimage delete imageid123 --yes
# 批量操作(支持从文件读取ID列表)
$ nodeimage delete -f image_ids.txt
$ nodeimage download -f image_ids.txt -o downloads/
# 查看调试信息和配置状态
$ nodeimage debug
管道操作
NodeImage 支持通过 Unix 管道进行批量操作,让您能够将 list 命令的输出直接传递给其他命令:
# 下载所有图片到默认目录
$ nodeimage list | nodeimage download --yes
# 下载所有图片到指定目录
$ nodeimage list | nodeimage download --yes -o photos/
# 删除所有图片(危险操作,请谨慎使用)
$ nodeimage list | nodeimage delete --yes
# 结合其他 Unix 工具进行过滤
$ nodeimage list | head -5 | nodeimage download --yes -o recent/
$ nodeimage list | grep "pattern" | nodeimage download --yes
$ nodeimage list | tail -10 | nodeimage delete --yes
# 将图片ID保存到文件,然后批量操作
$ nodeimage list > image_ids.txt
$ cat image_ids.txt | nodeimage download --yes -o backup/
管道操作说明:
- 管道会自动检测,无需额外参数
- 必须使用
--yes参数跳过确认提示(因为管道占用标准输入,无法进行交互确认) - 支持与
head、tail、grep等 Unix 工具组合使用 - 管道输入优先级:命令行参数 > 文件输入 > 管道输入
注意事项:
- 使用管道时如果不加
--yes参数,程序会显示错误提示并退出 - 删除操作不可撤销,使用管道批量删除时请格外小心
Python API 快速开始
首先导入 NodeImage:
>>> import nodeimage
>>> client = nodeimage.Client("your_api_key_here")
上传本地图片:
>>> result = client.upload_image("/path/to/image.jpg")
>>> print(result)
{'id': 'abc123def456', 'url': 'https://...', 'filename': 'image.jpg'}
从网络URL上传图片:
>>> result = client.upload_image("https://example.com/image.jpg")
>>> print(result)
{'id': 'xyz789uvw012', 'url': 'https://...', 'filename': 'image.jpg'}
列出所有图片:
>>> images = client.get_images()
>>> print(images)
[{'id': 'abc123', 'url': 'https://...', 'filename': 'image1.jpg'}, ...]
删除图片:
>>> result = client.delete_image("abc123def456")
>>> print(result)
{'success': True, 'message': 'Image deleted successfully'}
下载图片:
>>> image_info = client.download_image("abc123def456")
>>> with open(f"downloaded{image_info.ext}", "wb") as f:
... f.write(image_info.content)
>>> print(f"图片已保存,格式: {image_info.content_type}")
身份认证
环境变量(推荐)
export NODE_IMAGE_API_KEY=your_api_key_here
.env 文件
在项目根目录创建 .env 文件:
NODE_IMAGE_API_KEY=your_api_key_here
命令行参数
nodeimage --api-key your_api_key_here upload image.jpg
Python 代码
from nodeimage import Client
# 直接初始化
client = Client("your_api_key_here")
# 从环境变量创建
client = Client.from_env()
高级用法
自定义配置
from nodeimage import Client
client = Client(
api_key="your_api_key",
base_api_url="https://your-custom-api-endpoint.com",
base_cdn_url="https://your-custom-cdn-endpoint.com",
timeout=30 # 自定义超时时间(秒)
)
错误处理
from nodeimage import Client
client = Client("your_api_key")
try:
result = client.upload_image("/path/to/image.jpg")
print(f"上传成功: {result}")
except Exception as e:
print(f"上传失败: {e}")
支持的图片格式
NodeImage 支持常见的图片格式:
- JPEG (
.jpg,.jpeg) - PNG (
.png) - GIF (
.gif) - WebP (
.webp) - BMP (
.bmp) - SVG (
.svg)
API 参考
Client 类
class Client:
def __init__(
self,
api_key: str,
base_api_url: str = "https://api.nodeimage.com",
base_cdn_url: str = "https://cdn.nodeimage.com",
timeout: float | Timeout | None = 10,
logger: logging.Logger | None = None
)
def upload_image(self, image_path_or_url: str | Path) -> dict
def get_images(self) -> dict
def delete_image(self, image_id: str) -> dict
def download_image(self, image_id: str, ext: str = '.webp') -> ImageInfo
@classmethod
def from_env(cls, logger: logging.Logger | None = None) -> "Client"
CLI 命令
nodeimage [OPTIONS] COMMAND [ARGS]...
命令:
debug 显示调试信息和配置状态
upload 上传图片文件、URL或文件夹
list 列出已上传的图片,支持多种输出格式
download 下载图片到本地(支持管道输入)
delete 根据ID删除图片(不可撤销,支持管道输入)
选项:
--api-key TEXT NodeImage API 密钥
--help 显示帮助信息并退出
输出格式 (list命令):
id 仅显示图片ID(默认,适合管道操作)
json 显示完整JSON信息
csv 导出为CSV格式
xlsx 导出为Excel格式
管道支持:
download 和 delete 命令支持从标准输入读取图片ID列表
使用方式: nodeimage list | nodeimage download --yes
重要提醒: 管道操作时必须使用 --yes 参数,否则会因无法交互确认而失败
依赖项
NodeImage 依赖以下库:
- httpx - 用于 HTTP 请求(包含 SOCKS 代理支持)
- click - 用于 CLI 界面
- python-dotenv - 用于 .env 文件支持
- typing_extensions - 用于增强类型提示
- openpyxl - 用于 Excel 文件导出功能
获取 API 密钥
- 访问 NodeImage 官网
- 注册账户或登录现有账户
- 在用户面板中获取您的 API 密钥
常见问题
上传失败怎么办?
请检查:
- API 密钥是否正确设置
- 图片文件是否存在且可读
- 网络连接是否正常
- 图片格式是否支持
如何查看详细错误信息?
可以通过 Python 代码捕获异常获取详细信息:
try:
client.upload_image("image.jpg")
except Exception as e:
print(f"详细错误: {e}")
导出Excel文件时提示缺少依赖怎么办?
如果使用 nodeimage list -f xlsx 时提示缺少 openpyxl 依赖,请运行:
pip install openpyxl
有文件大小限制吗?
具体限制请参考 NodeImage 官网 的服务条款。
许可证
本项目采用 MIT 许可证 进行许可。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nodeimage-0.0.2.tar.gz.
File metadata
- Download URL: nodeimage-0.0.2.tar.gz
- Upload date:
- Size: 20.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1a49d7c9918fd2f0dd3c4ef7e83afb0e67bd1e7899a3a209bdcefb44b4dc139
|
|
| MD5 |
039587e1f92882014ea58409cfd545f6
|
|
| BLAKE2b-256 |
a25b0bff1b98cefd8f149b715ae1cd1f0c65501f0cf4c6869d2ec638ed43cfb4
|
File details
Details for the file nodeimage-0.0.2-py3-none-any.whl.
File metadata
- Download URL: nodeimage-0.0.2-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4864637a1a4d4e1b5e9a18884ef72cd1a975e6198c9d67d6640c3c84e30fe1fa
|
|
| MD5 |
2078725096abec3d53b8c78507c8d35f
|
|
| BLAKE2b-256 |
5484181f921823149f0f2c853cca71d97ecc259818382beca468fc6f7c66d4a2
|