1
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ 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"]. $\endgroup$ – WReach Apr 13 '18 at 5:19
  • $\begingroup$ Thank you, ImportString[FromCharacterCode[picData], "JPEG"] works beautifully. Very much appreciated. $\endgroup$ – Guaro Apr 13 '18 at 8:15
4
$\begingroup$

The leading byte sequence 255, 216, 255, 224 looks like the file header that one finds when an image is in JPEG File Interchange Format. To import such an image, we can use:

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"]
$\endgroup$
  • $\begingroup$ 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... $\endgroup$ – Albert Retey Apr 13 '18 at 16:08
  • $\begingroup$ @AlbertRetey In case one doesn't know, it could maybe be determined with FileFormat, as the documentation says that it "bases the final result it returns only on the actual contents of files." $\endgroup$ – C. E. Oct 2 '18 at 12:26
  • $\begingroup$ 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... $\endgroup$ – Albert Retey Oct 2 '18 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.