Thumbor是基于Python的开源的On-Demand图片处理服务,可以实现图片裁剪crop、缩放resize、翻转flip、滤镜filter,甚至是人脸识别。
官网:[url=https://github.com/thumbor/thumbor]https://github.com/thumbor/thumbor[/url]
目前版本: 6.3.2
图像处理是系统开发中的必备组件,各种开发语言都内置图像处理API,也有大量的开源图像处理库做补充。
[1] 图像变换引擎([url=https://www.imagemagick.org/]ImageMagick[/url]、[url=http://www.graphicsmagick.org/]Graphicsmagick[/url]、[url=http://opencv.org/]OpenCV[/url])
[2] 图像优化工具([url=http://www.jpegmini.com/]JPEGMini[/url]、[url=https://tinypng.com/]TinyPNG[/url]、[url=https://imageoptim.com/]ImageOptim[/url]、[url=https://pngmini.com/]ImageAlpha[/url]、[url=https://pngquant.org/]pngquant[/url]、[url=https://github.com/mozilla/mozjpeg]MozJPEG[/url])
[3] 图像处理系统([url=https://github.com/agschwender/pilbox]Pilbox[/url]、[url=https://github.com/thumbor/thumbor]Thumbor[/url])
Thumbor通过特殊构成的URL来创建缩略图,比如:
[quote]http://thumbor.example.com/320x240/http://images.example.com/llamas.jpg[/quote]
含义是:Thumbor服务器thumbor.example.com通过HTTP从images.example.com获取图像llamas.jpg,将其缩放到320x240之后返回图像数据。
Thumbor内部支持以下三种图像变换引擎:
PIL (Python Image Library)、GraphicsMagick (pgmagick)、OpenCV
Thumbor存储图片支持的方式有File、Memcached、MongoDB、Redis。根据实际的使用情况选择存储方式。默认选用文件系统的存储方式。
具体详细的使用方法可以参考官方文档: [url=http://thumbor.readthedocs.io/en/latest/index.html]http://thumbor.readthedocs.io/en/latest/index.html[/url]
需要注意的是:Thumbor目前还不支持Python3!
[quote] File "/usr/local/tensorflow/lib/python3.5/site-packages/thumbor/context.py", line 293
print "Joining threads...."
SyntaxError: Missing parentheses in call to 'print'[/quote]
print "Hello world" # python 2.x
print("Hello world") # python 3.x
[b](1)安装[/b]
# 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
[b](2)启动[/b]
# 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。
[quote]2017-08-15 15:34:02 thumbor:WARNING Module thumbor.filters.distributed_collage could not be imported: No module named cv2[/quote]
[b](3)测试[/b]
- 原图
[color=blue]http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5503/ff4063e6-9541-3d93-9066-448e92e0d932.jpg[/img]
[color=blue]http://thumbor-server:8888/unsafe/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5505/893bae3e-3a18-3481-921f-1401d8c74c7f.jpg[/img]
- 保持横纵比,宽度固定300px
[color=blue]http://thumbor-server:8888/unsafe/300x0/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5507/19458cb3-c98e-3218-83cd-93bb97d92256.jpg[/img]
- 保持横纵比,高度固定300px
[color=blue]http://thumbor-server:8888/unsafe/0x300/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5509/02830e85-56bb-3f21-826d-30869ddab73b.jpg[/img]
- 指定大小300×300
[color=blue]http://thumbor-server:8888/unsafe/300x300/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5511/eb7b619d-0e3d-3eb4-9d01-943ecf4de05a.jpg[/img]
- 裁剪指定位置
[color=blue]http://thumbor-server:8888/unsafe/528x111:1101x657/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5513/4ef58962-95f5-3e7b-b7a8-f857b4f22821.jpg[/img]
- 图像滤镜
[color=blue]http://thumbor-server:8888/unsafe/400x200/filters:grayscale()/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5515/051b9571-9cfb-3cee-92f0-b58dc62a9454.jpg[/img]
[color=blue]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[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5517/f9a1b838-660e-3336-a159-12644015b25b.jpg[/img]
[color=blue]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[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5519/045f0a03-c1ae-3cf8-b396-d5d5f1cee16c.jpg[/img]
[color=blue]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[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5521/2e385fbe-fceb-3c20-982a-11c3c8cc4a1b.jpg[/img]
[b](4)安全模式[/b]
以上都是测试用URL,可以看到URL里包含[b]/unsafe/[/b]。
限定来源
# vi /usr/local/thumbor/thumbor.conf
ALLOWED_SOURCES = [
'desk.fd.zol-img.com.cn'
]
访问以下URL会返回 200 OK
[color=blue]http://thumbor-server:8888/unsafe/300x100/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5523/ac2945de-8e86-3c68-bb19-b55113e44295.jpg[/img]
访问以下URL会返回 400 Bad Request
[color=blue]http://thumbor-server:8888/unsafe/300x100/https://http.cat/405.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5527/d5721da7-7942-3963-adad-4bff3100a1ef.jpg[/img]
关闭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也无效了。
[color=blue]http://thumbor-server:8888/unsafe/300x100/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5527/d5721da7-7942-3963-adad-4bff3100a1ef.jpg[/img]
[b](5)URL做成[/b]
查看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
[color=blue]http://thumbor-server:8888/UJWGMA3XL6X29zF2qgQgBKqz_QM=/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5529/a2a95913-7fed-325d-88a9-6e264f0ec98d.jpg[/img]
缩放
# 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
[color=blue]http://thumbor-server:8888/byeRaXKHyMqfrsWmRZb558J_8iI=/300x200/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5531/47bb986d-16d6-315f-bae3-490822e41c97.jpg[/img]
[b](6)智能裁剪[/b]
安装
# 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
[color=blue]http://thumbor-server:8888/twOvaCpM5nAUv3JCdDnNP76Iqz0=/300x80/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5533/04e38a85-2ebb-35f6-b8e7-36af5eaa80a7.jpg[/img]
[color=blue]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[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5535/35ca7337-6958-3827-8e05-fc207e480217.jpg[/img]
[b](7)图像上传[/b]
可以通过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
[img]http://dl2.iteye.com/upload/attachment/0126/5537/370946a4-3faf-3ce1-8ff2-187fafa7bba8.jpg[/img]
访问以下URL会返回 200 OK。
[color=blue]http://thumbor-server:8888/image/14ffd48c8baa4301b476c4b837568af8/rensanning.jpg[/color]
[img]http://dl2.iteye.com/upload/attachment/0126/5539/e234d821-81a5-375e-9240-9f9815bfe5a5.jpg[/img]
同理可以通过PUT、DELETE、GET来操作已有图像。当然默认是不允许的需要修改配置。
[quote]#UPLOAD_DELETE_ALLOWED = False
#UPLOAD_PUT_ALLOWED = False[/quote]
[b](8)其他设置[/b]
默认采用的HTTP加载图像,也就是需要提供一个图像的绝对URL,也可以采用加载本地图像,把图像放入root文件夹即可访问。
LOADER = 'thumbor.loaders.file_loader'
[quote]## 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'[/quote]
默认是不存储转换后的图像的,需要的话需要设置。
RESULT_STORAGE = 'thumbor.result_storages.file_storage'
[quote]## 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'[/quote]
参考:
https://segmentfault.com/a/1190000008656825
https://ingramchen.io/blog/2015/12/thumbor-tutorial.html
https://blog.1q77.com/2015/05/using-thumbor-part1