使用 Python 从数据库存储和检索图像
我想将与每个人的个人资料相关的图像存储在数据库中并检索它们 根据要求并将其保存为 .jpg 文件 - 并将其显示给用户。
如何将存储在数据库中的图像数据渲染为图像并将其存储在本地?
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.
How could I render the image data stored in the DB as an image and store it locally??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在确定是否将图像存储在数据库中还是文件系统中时,这个 StackOverflow 问题是一个很好的开始
在数据库中存储图像 - 是或否?
我还在我的博客上写过有关这些问题的文章
http://www.atalasoft.com/cs/blogs/loufranco/archive /2007/12/03/images-in-databases-part-i-what-to-store.aspx
http://www.atalasoft.com/cs/blogs/ Loufranco/archive/2007/12/04/images-in-databases-part-ii-web-images-are-random-access.aspx
http://www.atalasoft.com/cs/blogs /loufranco/archive/2009/04/28/document-storage-database-blobs-or-the-filesystem.aspx
简短的答案是:如果图像很小并且数量不多 - - 数据库中的 blob 可能没问题(更容易备份、控制访问、事务等)。但与文件系统相比,性能很差——这是一个权衡。大多数实际应用程序(尤其是大型网站)都使用文件系统,但在某些情况下使用 blob 是完全合理的。
Blob(或文件)只是编码数据(JPG)——它是完全相同的字节流。在 Web 应用程序中,您将使用 img 标签 - 如果您正在构建桌面 GUI,请查看它具有哪些用于显示图像的组件。
如果您使用的是 blob,并且需要在 Web 应用程序中显示,则 img 标记的 src 将设置为脚本/视图/等,该脚本/视图/等返回设置为图像的 mime 类型的内容类型(例如 image /jpeg)和 blob 的内容。
This StackOverflow Question is a good one to start with when figuring out whether to store images in a DB or on a filesystem
Storing Images in DB - Yea or Nay?
I have also written about the issues on my blog
http://www.atalasoft.com/cs/blogs/loufranco/archive/2007/12/03/images-in-databases-part-i-what-to-store.aspx
http://www.atalasoft.com/cs/blogs/loufranco/archive/2007/12/04/images-in-databases-part-ii-web-images-are-random-access.aspx
http://www.atalasoft.com/cs/blogs/loufranco/archive/2009/04/28/document-storage-database-blobs-or-the-filesystem.aspx
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.
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.
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.
标准方法是在上传图像时将其转换为 JPG 一次。然后将其保存为文件系统上的常规文件。使用 DB 只是存储保存图像的相对路径。
当你想渲染它时,只需使用 HTML:
并设置你的网络服务器(nginx,lighttpd,等等)来服务你将图像保存到虚拟目录
/images
下的目录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.
When you want to render it just use HTML:
and setup your web-server (nginx, lighttpd, whatever) to serve directory you save images to under virtual directory
/images
为什么不简单地将图像存储在文件系统上,而只将它们的引用存储在数据库上。这更加优雅,并且不会消耗数据库的负载。
此外,您不必使用任何类型的二进制函数从数据库中读取它们,从而节省内存和加载时间。
您想将其存储在数据库中是否有非常具体的原因?
干杯
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