ValueError:没有足够的值来解包(预计为 3 个,实际为 2 个),因为缩略图大小 = ((100, 100), PIL.Image.Resampling.LANCZOS))

问题描述 投票:0回答:1

在 Flask Admin 上上传图像时,出现 AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS' 错误。我将其更正为thumbnail_size = ((100, 100), PIL.Image.Resampling.LANCZOS))。现在该行给出了 ValueError: 没有足够的值来解压(预期为 3,得到了 2)。 代码是;

import os
import os.path as op
from flask import Flask, url_for
from flask_sqlalchemy import SQLAlchemy
from markupsafe import Markup
from flask_admin import Admin, form
from flask_admin.contrib import sqla, rediscli
from PIL import Image
import PIL

app = Flask(__name__, static_folder='files')

# see http://bootswatch.com/3/ for available swatches themes
app.config['FLASK_ADMIN_SWATCH'] = 'cerulean'

# Create dummy secrey key so we can use sessions
app.config['SECRET_KEY'] = '123456790'

# Create in-memory database
app.config['DATABASE_FILE'] = 'sample_db.sqlite'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + app.config['DATABASE_FILE']
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)

# Create directory for file fields to use
file_path = op.join(op.dirname(__file__), 'files')
try:
    os.mkdir(file_path)
except OSError:
    pass

class Image(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode(64))
    path = db.Column(db.Unicode(128))

    def __unicode__(self):
        return self.name

class ImageView(sqla.ModelView):
    def _list_thumbnail(view, context, model, name):
        if not model.path:
            return ''

        return Markup('<img src="%s">' % url_for('static',
                                                 filename=form.thumbgen_filename(model.path)))

    column_formatters = {
        'path': _list_thumbnail
    }

    # Alternative way to contribute field is to override it completely.
    # In this case, Flask-Admin won't attempt to merge various parameters for the field.
    form_extra_fields = {
        'path': form.ImageUploadField('Image',
                                      base_path=file_path,
                                      #thumbnail_size=(100, 100, True))
                                      thumbnail_size = ((100, 100), PIL.Image.Resampling.LANCZOS))
    }

# Flask views
@app.route('/')
def index():
    return '<a href="/admin/">Click me to get to Admin!</a>'

# Create admin
admin = Admin(app, 'Example: Forms', template_mode='bootstrap4')

# Add views
admin.add_view(ImageView(Image, db.session))


def build_sample_db():
    db.create_all()
    db.session.commit()
    return

if __name__ == '__main__':
    # Build a sample db on the fly, if one does not exist yet.
    app_dir = op.realpath(os.path.dirname(__file__))
    database_path = op.join(app_dir, app.config['DATABASE_FILE'])
    if not os.path.exists(database_path):
        with app.app_context():
            build_sample_db()
    # Start app
    app.run(debug=True)

我期望能够使用指定的路径从 Flask Admin 上传图像

python html flask sqlalchemy flask-admin
1个回答
0
投票

第一个问题与this问题相关,已在master分支上修复,但尚未推送到PyPI。

thumbnail_size=(100, 100, True)
是正确的,如官方文档中所述。 简而言之,您对此问题无能为力。这里有 2 种可能的解决方案:

  1. Pillow
    降级为 <10, which has
    ANTIALIAS
    属性
  2. 使用
    https://github.com/flask-admin/flask-admin
    GitHub 存储库代替 PyPI 进行 Flask-admin,如 README 文件中所述
© www.soinside.com 2019 - 2024. All rights reserved.