Thumbor是基于Python的开源的On-Demand图片处理服务,可以实现图片裁剪crop、缩放resize、翻转flip、滤镜filter,甚至是人脸识别。
官网:
https://github.com/thumbor/thumbor
目前版本: 6.3.2
图像处理是系统开发中的必备组件,各种开发语言都内置图像处理API,也有大量的开源图像处理库做补充。
[1] 图像变换引擎(
ImageMagick、
Graphicsmagick、
OpenCV)
[2] 图像优化工具(
JPEGMini、
TinyPNG、
ImageOptim、
ImageAlpha、
pngquant、
MozJPEG)
[3] 图像处理系统(
Pilbox、
Thumbor)
Thumbor通过特殊构成的URL来创建缩略图,比如:
引用
http://thumbor.example.com/320x240/http://images.example.com/llamas.jpg
含义是:Thumbor服务器thumbor.example.com通过HTTP从images.example.com获取图像llamas.jpg,将其缩放到320x240之后返回图像数据。
Thumbor内部支持以下三种图像变换引擎:
PIL (Python Image Library)、GraphicsMagick (pgmagick)、OpenCV
Thumbor存储图片支持的方式有File、Memcached、MongoDB、Redis。根据实际的使用情况选择存储方式。默认选用文件系统的存储方式。
具体详细的使用方法可以参考官方文档:
http://thumbor.readthedocs.io/en/latest/index.html
需要注意的是:Thumbor目前还不支持Python3!
引用
File "/usr/local/tensorflow/lib/python3.5/site-packages/thumbor/context.py", line 293
print "Joining threads...."
SyntaxError: Missing parentheses in call to 'print'
print "Hello world" # python 2.x
print("Hello world") # python 3.x
(1)安装
# python -V
Python 2.7.5
# pip search thumbor
thumbor (6.3.2) - thumbor is an open-source photo thumbnail service by globo.com
# pip install thumbor
# pip install -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com thumbor
(2)启动
# mkdir /usr/local/thumbor
# thumbor-config > /usr/local/thumbor/thumbor.conf
# cp /usr/local/thumbor/thumbor.conf /usr/local/thumbor/thumbor.conf.default
# thumbor --port=8888 --conf=/usr/local/thumbor/thumbor.conf
以下警告提示是没有安装opencv。
引用
2017-08-15 15:34:02 thumbor:WARNING Module thumbor.filters.distributed_collage could not be imported: No module named cv2
(3)测试
- 原图
http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
http://thumbor-server:8888/unsafe/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
- 保持横纵比,宽度固定300px
http://thumbor-server:8888/unsafe/300x0/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
- 保持横纵比,高度固定300px
http://thumbor-server:8888/unsafe/0x300/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
- 指定大小300×300
http://thumbor-server:8888/unsafe/300x300/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
- 裁剪指定位置
http://thumbor-server:8888/unsafe/528x111:1101x657/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
- 图像滤镜
http://thumbor-server:8888/unsafe/400x200/filters:grayscale()/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
http://thumbor-server:8888/unsafe/200x200/filters:brightness(-20)/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
http://thumbor-server:8888/unsafe/200x200/filters:brightness(40)/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
http://thumbor-server:8888/unsafe/filters:round_corner(40,255,255,255):grayscale()/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
(4)安全模式
以上都是测试用URL,可以看到URL里包含
/unsafe/。
限定来源
# vi /usr/local/thumbor/thumbor.conf
ALLOWED_SOURCES = [
'desk.fd.zol-img.com.cn'
]
访问以下URL会返回 200 OK
http://thumbor-server:8888/unsafe/300x100/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
访问以下URL会返回 400 Bad Request
http://thumbor-server:8888/unsafe/300x100/https://http.cat/405.jpg
关闭unsafe配置密钥
# openssl rand -base64 48
JNj9+RxUKfsgrVN+8cH5DVzNdRURiFzBo6S2lL9Q0zcweHmdkU6q1Rf60XX7LnVc
# vi /usr/local/thumbor/thumbor.conf
ALLOW_UNSAFE_URL = False
SECURITY_KEY = 'JNj9+RxUKfsgrVN+8cH5DVzNdRURiFzBo6S2lL9Q0zcweHmdkU6q1Rf60XX7LnVc'
再次访问以下URL会返回 400 Bad Request,设置的ALLOWED_SOURCES也无效了。
http://thumbor-server:8888/unsafe/300x100/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
(5)URL做成
查看thumbor-url用法
# thumbor-url -h
通过KEY获取URL
# thumbor-url -k JNj9+RxUKfsgrVN+8cH5DVzNdRURiFzBo6S2lL9Q0zcweHmdkU6q1Rf60XX7LnVc http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
URL:
/UJWGMA3XL6X29zF2qgQgBKqz_QM=/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
/UJWGMA3XL6X29zF2qgQgBKqz_QM=/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
通过KEY文件获取URL
# vi /etc/thumbor.key
JNj9+RxUKfsgrVN+8cH5DVzNdRURiFzBo6S2lL9Q0zcweHmdkU6q1Rf60XX7LnVc
# thumbor-url -l /etc/thumbor.key http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
URL:
/UJWGMA3XL6X29zF2qgQgBKqz_QM=/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
/UJWGMA3XL6X29zF2qgQgBKqz_QM=/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
访问以下URL会返回 200 OK
http://thumbor-server:8888/UJWGMA3XL6X29zF2qgQgBKqz_QM=/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
缩放
# thumbor-url -l /etc/thumbor.key -w 300 -e 200 http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
URL:
/byeRaXKHyMqfrsWmRZb558J_8iI=/300x200/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
/byeRaXKHyMqfrsWmRZb558J_8iI=/300x200/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
访问以下URL会返回 200 OK
http://thumbor-server:8888/byeRaXKHyMqfrsWmRZb558J_8iI=/300x200/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
(6)智能裁剪
安装
# yum install -y opencv GraphicsMagick openssl-devel curl-devel GraphicsMagick-c++-devel boost boost-devel boost-python
# pip install pgmagick opencv-python graphicsmagick-engine
# pip install -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com pgmagick opencv-python graphicsmagick-engine
设置
# vi /usr/local/thumbor/thumbor.conf
DETECTORS = [
'thumbor.detectors.face_detector',
'thumbor.detectors.feature_detector'
]
有无/smart/前后效果对比:
# thumbor-url -l /etc/thumbor.key -w 300 -e 80 http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
URL:
/twOvaCpM5nAUv3JCdDnNP76Iqz0=/300x80/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
/twOvaCpM5nAUv3JCdDnNP76Iqz0=/300x80/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
# thumbor-url -l /etc/thumbor.key -w 300 -e 80 -s http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
URL:
/swDVsG0g_nYBE8zHDcjzfyH561w=/300x80/smart/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
/swDVsG0g_nYBE8zHDcjzfyH561w=/300x80/smart/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
http://thumbor-server:8888/twOvaCpM5nAUv3JCdDnNP76Iqz0=/300x80/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
http://thumbor-server:8888/swDVsG0g_nYBE8zHDcjzfyH561w=/300x80/smart/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
(7)图像上传
可以通过REST的方式上传图像:POST http://thumbor-server:8888/image
默认未开启上传功能,会返回‘405: Method Not Allowed’。
# vi /usr/local/thumbor/thumbor.conf
UPLOAD_ENABLED = True
# curl -i -H "Content-Type: image/jpeg" -H "Slug: rensanning.jpg" \
-XPOST http://thumbor-server:8888/image --data-binary "@/tmp/1348814002177.jpg"
HTTP/1.1 201 Created
Date: Wed, 16 Aug 2017 02:19:59 GMT
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Location: /image/14ffd48c8baa4301b476c4b837568af8/rensanning.jpg
Server: TornadoServer/4.5.1
访问以下URL会返回 200 OK。
http://thumbor-server:8888/image/14ffd48c8baa4301b476c4b837568af8/rensanning.jpg
同理可以通过PUT、DELETE、GET来操作已有图像。当然默认是不允许的需要修改配置。
引用
#UPLOAD_DELETE_ALLOWED = False
#UPLOAD_PUT_ALLOWED = False
(8)其他设置
默认采用的HTTP加载图像,也就是需要提供一个图像的绝对URL,也可以采用加载本地图像,把图像放入root文件夹即可访问。
LOADER = 'thumbor.loaders.file_loader'
引用
## The loader thumbor should use to load the original image. This must be the
## full name of a python module (python must be able to import it)
## Defaults to: 'thumbor.loaders.http_loader'
#LOADER = 'thumbor.loaders.http_loader'
## The root path where the File Loader will try to find images
## Defaults to: '/root'
#FILE_LOADER_ROOT_PATH = '/root'
默认是不存储转换后的图像的,需要的话需要设置。
RESULT_STORAGE = 'thumbor.result_storages.file_storage'
引用
## The result storage thumbor should use to store generated images. This must be
## the full name of a python module (python must be able to import it)
## Defaults to: None
#RESULT_STORAGE = None
## Path where the Result storage will store generated images
## Defaults to: '/tmp/thumbor/result_storage'
#RESULT_STORAGE_FILE_STORAGE_ROOT_PATH = '/tmp/thumbor/result_storage'
参考:
https://segmentfault.com/a/1190000008656825
https://ingramchen.io/blog/2015/12/thumbor-tutorial.html
https://blog.1q77.com/2015/05/using-thumbor-part1
已有 0 人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐