使用 Python 从数据库中存储和检索图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2477045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing and Retrieving Images from Database using Python
提问by tamizhgeek
I want to store the images related to a each person's profile in the DB and retrieve them when requested and save it as .jpg file - and display it to the users.
我想将与每个人的个人资料相关的图像存储在数据库中,并在请求时检索它们并将其保存为 .jpg 文件 - 并将其显示给用户。
How could I render the image data stored in the DB as an image and store it locally??
如何将存储在数据库中的图像数据呈现为图像并将其存储在本地?
回答by Lou Franco
This StackOverflow Question is a good one to start with when figuring out whether to store images in a DB or on a filesystem
在确定是将图像存储在数据库中还是文件系统中时,这个 StackOverflow 问题是一个很好的开始
Storing Images in DB - Yea or Nay?
I have also written about the issues on my blog
我也在我的博客上写了关于这些问题的文章
The short answer is: if the images are small and there aren't a lot of them -- a blob in the DB is probably fine (easier to backup, control access, transactions, etc). But the performance is bad compared to the filesystem -- it's a tradeoff. Most real applications (and especially big websites) use the filesystem, but it's perfectly reasonable to use blobs in some cases.
简短的回答是:如果图像很小并且图像不多——数据库中的 blob 可能没问题(更容易备份、控制访问、事务等)。但是与文件系统相比,性能很差——这是一种权衡。大多数实际应用程序(尤其是大型网站)使用文件系统,但在某些情况下使用 blob 是完全合理的。
The blob (or file) is just going to be the encoded data (JPG) -- it's the exact same stream of bytes. In a web app, you would use an img tag -- if you are building a desktop GUI, look at what components it has for displaying images.
blob(或文件)只是编码数据(JPG)——它是完全相同的字节流。在 Web 应用程序中,您将使用 img 标签——如果您正在构建桌面 GUI,请查看它具有哪些组件来显示图像。
If you are using a blob, and need to display in a web app, the src of the img tag will be set to a script/view/etc that returns a content-type set to the mime-type of the image (e.g. image/jpeg) and the content of the blob.
如果您使用的是 blob,并且需要在 Web 应用程序中显示,则 img 标签的 src 将设置为脚本/视图/等,该脚本/视图/等返回设置为图像的 mime-type 的内容类型(例如图像/jpeg) 和 blob 的内容。
回答by nkrkv
The standard approach is to convert image to JPG only once, when it is get uploaded. Then save it as a regular file on file system. Use DB just to store relative path to saved image.
标准方法是在上传图像时仅将图像转换为 JPG 一次。然后将其保存为文件系统上的常规文件。使用 DB 只是为了存储保存图像的相对路径。
When you want to render it just use HTML:
当你想渲染它时,只需使用 HTML:
<img src="/images/relative/path/to/my/image.jpg" />
and setup your web-server (nginx, lighttpd, whatever) to serve directory you save images to under virtual directory /images
并设置您的网络服务器(nginx、lighttpd 等)为您将图像保存到虚拟目录下的目录提供服务 /images
回答by Marcos Placona
Why don't you simply store the images on the file system, and only store their references on the database. That's a lot more elegant, and won't consume loads of your database.
为什么不简单地将图像存储在文件系统上,而只将它们的引用存储在数据库中。这更加优雅,并且不会消耗您的数据库负载。
Also, you won't have to use any kind of binary functions to read them from the DB, saving memory and loading time.
此外,您不必使用任何类型的二进制函数从数据库中读取它们,从而节省内存和加载时间。
Is there a very specific reason why you wanna store it on the DB?
你想把它存储在数据库上有什么非常具体的原因吗?
Cheers
干杯