# How do I visualize images stored in SQL?

here is the background:

• I have a SQL database that (among other data) contains images

• I connect to the database, extract and store the relevant data in variable picData like so



(* Create connection to DB *)
dbDir="C:\\DataBases";
sqlDB=FileNameJoin[{dbDir, "test.db"}];
(* Conenct to DB *)
conn=OpenSQLConnection[JDBC["SQLite", sqlDB]];
(* Extract image data *)
dumData=Last[SQLSelect[conn,"IMGTBL","IMAGE"]];
(*
dumData looks like this
dumData={SQLBinary[{255, 216, 255, 224, 0, 1, 74,...}];
*)

(* Now select ONLY the numerical data *)
picData=dumData[[1,1]];
(* so that picData is numeric vector of variable length (e.g. 6,153 elements)
picData={255, 216, 255, 224, 0, 1, 74,...};
*)


Questions:

What does picData precisely represent? (... as these data cannot be visualized using something as simple as

Image[Partition[picData, 293], "Byte"]

where 293 is a divisor of "Length[picData]"

and, most importantly, how do I visualize picData?

I am obviously missing some important piece of knowledge here. Any assistance would be much appreciated. Thank you.

• The leading byte sequence 255, 216, 255, 224 suggests that the data is in JPEG File Interchange Format. You might try ImportByteArray[ByteArray[picData], "JPEG"] or, in versions older than 11.2, ImportString[FromCharacterCode[picData], "JPEG"]. – WReach Apr 13 '18 at 5:19
• Thank you, ImportString[FromCharacterCode[picData], "JPEG"] works beautifully. Very much appreciated. – Guaro Apr 13 '18 at 8:15

ImportByteArray[ByteArray[picData], "JPEG"]

ImportByteArray was introduced in version 11.2 of Mathematica. In earlier versions, we can use this instead:
ImportString[FromCharacterCode[picData], "JPEG"]

• good answer, but I think the general answer would be that one would have to know what has been written to the database to correctly interpret the binary data read. If one cannot as in this case guess the format one would have to ask whoever generated the data. Your answer is valid for other cases as well, one would just need to change the format of the ImportString... – Albert Retey Apr 13 '18 at 16:08
• yes, but you would have to export to a file (according to the doc it wants a filename) and hope that the heuristics implemented will work correctly (which I would expect for some formats but not all). FileFormat certainly is something to have in mind and well worth a try as a last resort... – Albert Retey Oct 2 '18 at 20:13