最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Flask中去压缩并显示图片

Flask crifan 3765浏览 0评论

此处已经在Flaks从别处下载了个640×640的大图:

#download avatar image
avatarUrl = respUserInfoDict[‘headimgurl’]
app.logger.debug("avatarUrl=%s", avatarUrl)
avatarReq = requests.get(avatarUrl)
app.logger.debug("avatarReq=%s", avatarReq)
staticFolder = app.static_folder
app.logger.debug("staticFolder=%s", staticFolder)
avatartName = openid + ".png"
app.logger.debug("avatartName=%s", avatartName)
avatarStaticPath = os.path.join("img/avatar", avatartName)
app.logger.debug("avatarStaticPath=%s", avatarStaticPath)
avatarFullPath = os.path.join(staticFolder, avatarStaticPath)
app.logger.debug("avatarFullPath=%s", avatarFullPath)
with open(avatarFullPath, ‘wb’) as f:
    f.write(avatarReq.content)
    f.close()
    app.logger.debug("downloaded avatar=%s ok", avatarUrl)

现在希望压缩成60×60的小图片。

Flask python image resize

jmagnusson/Flask-Resize: Flask extension for resizing, cropping and caching images.

Flask-Images — Flask-Images 2.1.1 documentation

Flask-Resize — Flask-Resize 0.6.0 documentation

Configuration — Flask-Resize 0.6.0 documentation

Flask-Images 1.1.3 : Python Package Index

Flask-Resize 0.6.0 : Python Package Index

Flask-Images 2.1.2 : Python Package Index

API Documentation — Flask-Resize 0.6.0 documentation

python – Flask-Images is not working – Stack Overflow

去试试:

Flask-Images

(SIPEvents) ➜  SIPEvents pip install Flask-Images
Collecting Flask-Images
  Downloading Flask-Images-2.1.2.tar.gz
Requirement already satisfied (use –upgrade to upgrade): Flask>=0.9 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask-Images)
Requirement already satisfied (use –upgrade to upgrade): itsdangerous in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask-Images)
Collecting PillowCase (from Flask-Images)
  Downloading pillowcase-2.0.0.tar.gz
Requirement already satisfied (use –upgrade to upgrade): Jinja2>=2.4 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask>=0.9->Flask-Images)
Requirement already satisfied (use –upgrade to upgrade): Werkzeug>=0.7 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask>=0.9->Flask-Images)
Requirement already satisfied (use –upgrade to upgrade): click>=2.0 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask>=0.9->Flask-Images)
Collecting pillow (from PillowCase->Flask-Images)
  Downloading Pillow-3.3.1-cp27-cp27m-manylinux1_x86_64.whl (5.6MB)
    100% |████████████████████████████████| 5.6MB 58kB/s 
Requirement already satisfied (use –upgrade to upgrade): MarkupSafe in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Jinja2>=2.4->Flask>=0.9->Flask-Images)
Building wheels for collected packages: Flask-Images, PillowCase
  Running setup.py bdist_wheel for Flask-Images … done
  Stored in directory: /root/.cache/pip/wheels/c8/4c/c4/18ba1b7e9aebf7aec7059a3a10921aa551b89106f81a95aff7
  Running setup.py bdist_wheel for PillowCase … done
  Stored in directory: /root/.cache/pip/wheels/57/c6/7b/c62d4c593151f44d9c12f46cf06d1a1a62a8d30af97c9aad2c
Successfully built Flask-Images PillowCase
Installing collected packages: pillow, PillowCase, Flask-Images
Successfully installed Flask-Images-2.1.2 PillowCase-2.0.0 pillow-3.3.1

然后需要去设置:

app的secret_key

所以先要去搞清楚:

[已解决]Flask的app的secret_key的含义和作用

然后接着去试试

期间出现:

[已解决]Flask中导入images出现警告: ExtDeprecationWarning: Importing flask.ext.images is deprecated, use flask_images instead

结果代码:

<img class="initiator-avatar" src="{{ url_for(‘images’, width=64, height=64, quality=95, filename = todayEvent.user.avatar_static_path ) }}">

没有起效果

图片没有加载出来

flask-images not work

python – Flask not serving pictures – Stack Overflow

Quickstart — Flask Documentation (0.11)

python 2.7 – Flask-Images does not work – Stack Overflow

Python flask jinja image file not found – Stack Overflow

python – Cannot show image from STATIC_FOLDER in Flask template – Stack Overflow

python – Flask-webage background image doesnt work – Stack Overflow

how to set IMAGES_PATH flask-images

flask-images IMAGES_PATH

flask flask-images

flask url_for(‘images’

Warn when IMAGES_PATH is a string · Issue #29 · mikeboers/Flask-Images

Flask Configuration  

flask images IMAGES_PATH

Flask-Images 2.0.0 : Python Package Index

Flask-Images/core.py at master · mikeboers/Flask-Images

app.config.setdefault(‘IMAGES_PATH’, [‘static’])

-》好像默认的Flask的app的config中的IMAGES_PATH默认已经设置为我此处想要设置的:

static

了。

URL normalization on Windows · Issue #25 · mikeboers/Flask-Images

Improve docs re: configuration and examples · Issue #22 · mikeboers/Flask-Images

-》提到了:IMAGES_PATH其实是一个list

-》才注意到:

官网文档:

Flask-Images — Flask-Images 2.1.1 documentation

中的值是:

“IMAGES_PATH

A list of paths to search for images (relative to app.root_path); e.g. [‘static/uploads’]”

其中提到了:

A list of paths

以及,比如:

[‘static/uploads’]

-》也看到了此处作者的代码:

Flask-Images/core.py at master · mikeboers/Flask-Images

是:

    def find_img(self, local_path):
        local_path = os.path.normpath(local_path.lstrip(‘/’))
        for path_base in current_app.config[‘IMAGES_PATH’]:
            path = os.path.join(current_app.root_path, path_base, local_path)
            if os.path.exists(path):
                return path

-》很明显,IMAGES_PATH是个list,而不是单个的path的string

而我此处,却由于自己的

IMAGES_PATH = "static"

而导致无法正确的搜索路径,所以找不到图片,所以无法显示缩放后的图了。。。

所以去改为默认的:

IMAGES_PATH = ["static"]

然后再去试试

最后,终于可以正常显示压缩后的图片了:

[总结]

此处,使用Flask-Images这个Flask插件,来完成图片等缩放显示

具体步骤是:

1.安装

pip install Flask-Images

2.设置app的secret_key

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/instance/config.py

FLASK_SECRET_KEY = ‘^\x90\xcd-N\xc2:z\xee\xfckHOUjy\xe0\x83b\x12\x1f\xe3Wb’

其中此处的secret_key是用Python的os.urandom(24)去生成的:

➜  ~ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.urandom(24)
‘^\x90\xcd-N\xc2:z\xee\xfckHOUjy\xe0\x83b\x12\x1f\xe3Wb’
>>>

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/__init__.py

app.secret_key = app.config[‘FLASK_SECRET_KEY’]

3.使用Flask-Images

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/__init__.py

from flask_images import Images
images = Images(app)

html中:

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/templates/index.html

<img class="initiator-avatar" src="{{ url_for(‘images’, width=64, height=64, quality=95, filename = todayEvent.user.avatar_static_path ) }}">

即可,自动压缩显示出对应的图片了。

4.如果想要测试图片被不同模式压缩后的显示效果,可以参考官网的示例:

Flask-Images Demo

转载请注明:在路上 » [已解决]Flask中去压缩并显示图片

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.198 seconds, using 22.15MB memory