`
rensanning
  • 浏览: 3512713 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:37457
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:604220
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:677887
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:87192
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:399741
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:69042
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:90419
社区版块
存档分类
最新评论

搭建独立图像处理服务(Thumbor)

 
阅读更多
Thumbor是基于Python的开源的On-Demand图片处理服务,可以实现图片裁剪crop、缩放resize、翻转flip、滤镜filter,甚至是人脸识别。

官网:https://github.com/thumbor/thumbor

目前版本: 6.3.2

图像处理是系统开发中的必备组件,各种开发语言都内置图像处理API,也有大量的开源图像处理库做补充。

[1] 图像变换引擎(ImageMagickGraphicsmagickOpenCV
[2] 图像优化工具(JPEGMiniTinyPNGImageOptimImageAlphapngquantMozJPEG
[3] 图像处理系统(PilboxThumbor

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
  • 大小: 63.3 KB
  • 大小: 64.3 KB
  • 大小: 34.9 KB
  • 大小: 53.7 KB
  • 大小: 48.7 KB
  • 大小: 31.3 KB
  • 大小: 39.1 KB
  • 大小: 39.1 KB
  • 大小: 32.7 KB
  • 大小: 65.6 KB
  • 大小: 51 KB
  • 大小: 58.5 KB
  • 大小: 49.4 KB
  • 大小: 64.6 KB
  • 大小: 36.6 KB
  • 大小: 21.8 KB
  • 大小: 19.8 KB
  • 大小: 52 KB
  • 大小: 67.9 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics