Python 如何显示多个图像?

Python 如何显示多个图像?,python,sqlalchemy,pylons,Python,Sqlalchemy,Pylons,我试图从我的数据库中获取多个图像路径以显示它们,但它目前不起作用 以下是我正在使用的: 我得到一个关于这部分的错误: 我想让挂架在一页上显示几个jpg文件。你知道我如何做到这一点吗?你使用了两次image.image\u path,但有一次(你告诉我们,你犯了一个错误)你使用了image[id]。image\u path。您认为哪些id可以作为image的适当索引,为什么代码的不同部分在使用上存在差异 如果您想要一定数量的图像,为什么不使用切片语法呢?例如,您可以获得前10幅图像(确保包含一个顺

我试图从我的数据库中获取多个图像路径以显示它们,但它目前不起作用

以下是我正在使用的:

我得到一个关于这部分的错误:


我想让挂架在一页上显示几个jpg文件。你知道我如何做到这一点吗?

你使用了两次
image.image\u path
,但有一次(你告诉我们,你犯了一个错误)你使用了
image[id]。image\u path
。您认为哪些
id
可以作为
image
的适当索引,为什么代码的不同部分在使用上存在差异


如果您想要一定数量的图像,为什么不使用切片语法呢?例如,您可以获得前10幅图像(确保包含一个
顺序,以获得可预测、可重复的结果),您可以通过
[0:10]
过滤器的结果进行切片;第二个10幅图像,
[10:20]
;依此类推。

我猜您假设/希望的是,filter_by查询的结果包含一个字典,将检索到的图像映射到它们的ID。相反,它持有一个查询对象,该对象表示当访问像Alex提到的切片表达式或迭代操作迫使它返回一个iterable结果集时,它承诺返回一个iterable结果集

这可能不是解决此问题的最佳方法,但我猜修改代码使其看起来像这样可能会实现您想要的:

def get_image(self, userid, id):
    image = meta.Session.query(Image).filter_by(userid=userid)
    image = dict((img.id, img) for img in image)
    permanent_file = open(image[id].image_path, 'rb')
    if not os.path.exists(image.image_path):
        return 'No such file'
    data = permanent_file.read()
    permanent_file.close()
    response.content_type = guess_type(image.image_path)[0] or 'text/plain'
    return data
更明智的方法是这样的:

def get_image(self, userid, id):
    image = meta.Session.query(Image).filter_by(userid=userid).filter_by(id=id).first()
    # TODO: Handle the case when image is None
    permanent_file = open(image[id].image_path, 'rb')
    if not os.path.exists(image.image_path):
        return 'No such file'
    data = permanent_file.read()
    permanent_file.close()
    response.content_type = guess_type(image.image_path)[0] or 'text/plain'
    return data
当然,您假设存在一个与查询匹配的图像,但它可能不匹配,因此您可能需要对我留下TODO注释的地方进行一些错误处理

当然,任何这些更改都只会返回单个图像的数据。如果需要多个图像,则必须为每个图像调用一次此函数,可能是在某种图像视图的请求处理程序中

如果您确实希望一次返回多个图像的原始图像数据,那么Alex建议使用切片从数据库一次返回10条记录,这可能是最好的方法,但是,您必须修改代码的其余部分,以迭代N个图像的列表,并从文件系统中检索每个图像的数据,并返回类似原始图像数据块列表的内容。

假设“id”包含从0到图像数量的数字,在为数组编制索引之前,需要将其从字符串转换为int。我会做类似的事情

def get_image(self, userid, id): images = meta.Session.query(Image).filter_by(userid=userid) try: image = images[int(id)] with open(image.image_path, 'rb') as f: data = f.read() except (IndexError, ValueError, IOError): abort(404) response.content_type = guess_type(image.image_path)[0] or 'application/octet-stream' return data def get_映像(self、userid、id): images=meta.Session.query(Image).filter\u by(userid=userid) 尝试: 图像=图像[int(id)] 打开(image.image_路径,'rb')作为f: data=f.read() 除了(索引器、ValueError、IOError): 中止(404) response.content\u type=猜测类型(image.image\u路径)[0]或“应用程序/八位字节流” 返回数据 os.path.exists()检查发生得太晚:如果文件不退出,open()将引发IOError。改用try/except。
def get_image(self, userid, id):
    image = meta.Session.query(Image).filter_by(userid=userid).filter_by(id=id).first()
    # TODO: Handle the case when image is None
    permanent_file = open(image[id].image_path, 'rb')
    if not os.path.exists(image.image_path):
        return 'No such file'
    data = permanent_file.read()
    permanent_file.close()
    response.content_type = guess_type(image.image_path)[0] or 'text/plain'
    return data
def get_image(self, userid, id): images = meta.Session.query(Image).filter_by(userid=userid) try: image = images[int(id)] with open(image.image_path, 'rb') as f: data = f.read() except (IndexError, ValueError, IOError): abort(404) response.content_type = guess_type(image.image_path)[0] or 'application/octet-stream' return data