I'm trying to use the ROracle package to pull images from a database. The images are stored in the database as BLOBs.
I've managed to get the images into R in "raw" format. Is there a way to convert it from raw into jpeg/png?
This is what I have so far:
con <- dbConnect(drv, username = user,
password = pw, dbname=connect.string)
query.string <- paste("select db.img",
"from database db",
"where db.id = '01234567')")
## run a SQL statement by first creating a resultSet object
rs <- dbSendQuery(con, query.string)
## fetch records from the resultSet into a data.frame
data <- fetch(rs)
## extract all rows
str(data)
> str(data)
'data.frame': 1 obs. of 1 variable:
$ DATA:List of 1
..$ : raw ff d8 ff e1 ...
Ideally, I could then do something like this:
pic <- rawToJpeg(unlist(data)[1])
which would then allow me to plot/save the image
Consider using writeBin
to transfer the binary data to file:
rawToJpeg <- function(pic_data) {
f = file(paste0('output.jpeg'), "wb") # OPEN FILE CONNECTION
writeBin(pic_data, con = f, useBytes=TRUE) # TRANSFER RAW DATA
close(f) # CLOSE FILE CONNECTION
}
rawToJpeg(unlist(data)[1])