[英]How to download images from postgres bytea column using R or python?
基本上,我有一個帶有名為“person”的表的本地 postgres 數據庫,它有一個名為“image”的列,它是一個包含 .png 圖像的 bytea 列。 我不使用 postgresql,我想將圖像下載為后綴為“.png”的另一列中的名稱
我找到的所有答案都是 postgres 方面的答案,我真的不能同意。 R或python中是否有可以下載圖像的正版軟件包?
這是使用R
的解決方案:
# Packages we need
require("RPostgreSQL")
require('jsonlite')
# Create connection to database
pgConn <- dbConnect(PostgreSQL(), dbname="your_db", host='127.0.0.1')
# Query string - edit this to suit your needs
# Important thing here is that we're getting Postgres to base64 encode
# the image data (converting it from binary to text)
thisQ = "SELECT encode(imageColumn, 'base64') AS image from test_table WHERE name='image';"
# Execute the SELECT query and fetch the results
resultSet = dbSendQuery(pgConn, thisQ)
resultData <- fetch(resultSet, n=-1)
dbClearResult(resultSet)
# Get the image data as text
imageData <- resultData$image
# Decode from base64 back to binary
imageDataDecoded <- jsonlite::base64_dec(imageData)
# Create a file connection and write the binary data to disk using mode "wb".
write.filename = file("/temp/file.png", "wb")
writeBin(imageDataDecoded, write.filename)
close(write.filename)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.