ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: Leonardo on November 02, 2017, 06:20:47 PM

Title: Decoding FLIR image's raw data with python script.
Post by: Leonardo on November 02, 2017, 06:20:47 PM
Hello, friends

I'm trying to make some code involving the flir thermal images and python language.

What I trying to do is to get the raw data of an image and decode those binary values to "readable" ones. So I can use the equation discussed in this link https://exiftool.org/forum/index.php/topic,4898.60.html (https://exiftool.org/forum/index.php/topic,4898.60.html) to transform it in real temperature values.

To do this, I'm doing the following command:
exiftool -j -b FLIR0208.jpg
So I can get all the necessary data in Json format to implement in the equation automatically with my python script.

I get the string using:
encodedData = str.encode(imageData['RawThermalImage'][7:])

Since the first seven characters are "base64:", I just ignore them.

I'm decoding it with this line
preDecodedData = bytearray(base64.b64decode(encodedData))

Since each value is a 16bit information, I'm getting each two bytes with the following example:
temp = preDecodedData[0] << 8 | preDecodedData[1]

So I use this temp variable in the equation. The thing is it's not giving me the correct values. I checked the equation and it is right. So my doubt is how can I manually decode this binary data correctly.

Any help will be apreciated :)
Title: Re: Decoding FLIR image's raw data with python script.
Post by: Phil Harvey on November 03, 2017, 07:17:59 AM
To check to see if you are decoding correctly, write the raw image to a data file and use a hexdump utility to look at the file:

exiftool -b -rawthermalimage FILE > out.dat

Then compare with your data to check if your Base64 decoding is correct.

Then, are you sure the 16-bit values are big-endian?  Perhaps try

temp = preDecodedData[1] << 8 | preDecodedData[0]

- Phil
Title: Re: Decoding FLIR image's raw data with python script.
Post by: Leonardo on November 05, 2017, 02:27:09 PM
Hey, Phil

Actually, the preDecodedData, which is assigned with

preDecodedData = bytearray(base64.b64decode(encodedData))

gives me: bytearray(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x02\x80\x00\x00\x01\xe0\x10\x00\x00\x00\x00@*_{\x00\x00 \x00IDATx\x01d\xbd\t\xd4\xb5YY\x9e\xb9\xdf\x7f\xac\xbf\x8a\x9a)(\xa0\xc0\xa2\x8a\xa1\xb0\x14P\x0bPFA\xed\xa8\x80\xa2&vZ\x12%- and so on.

The decoded data is assigned as:

decodedData = int.from_bytes(list(preDecodedData), byteorder = 'big', signed = False)

Which is big-endian.

In the other hand I used a hexdump app and ran the line you told me to test and it gave me:

014b20  27 37 53 d4 48 28 e9 fb 90 5e cd 15 1e 48 fe ad
014b30  29 94 36 e9 a5 af 8c c2 90 26 5d 2d 91 db f9 40
014b40  03 24 25 bf f5 8d 0c ad 64 7d 04 42 f9 b1 1e 90
014b50  f3 18 20 20 55 52 26 4d 21 9d 03 71 68 8f 08 e7
014b60  19 73 b8 5a 4c 28 45 86 ac c5 89 92 8a 64 1b 61
014b70  1e 4e 8d 43 9d cc 13 b2 62 22 2a 26 65 0b 74 86
014b80  8f c5 99 59 ce d7 1a d5 7b 9f 38 ca a3 82 95 19
014b90  53 91 de d4 9a 4d 5d 94 d6 10 63 8a 26 00 db 20
014ba0  03 26 39 3a 00 90 16 b3 e6 ca ed 7c 70 e7 df 42
014bb0  11 be 70 03 73 39 4c d3 f4 72 04 8f 7b 7f 81 54
014bc0  e6 a6 2b 11 67 bb 7b 50 00 43 87 94 92 da f9 0c
014bd0  ad 9d 80 a9 de 50 e4 bf 38 62 a9 13 95 7c 43 e6
014be0  5e df 92 fc 97 27 34 f1 da d6 81 89 d9 d2 31 61
014bf0  e9 e4 5c ad 9f 4e c7 98 f0 a7 53 58 5a b4 cc b6
014c00  a5 6d d5 76 0c 34 09 e1 5a 18 2b e9 01 54 b8 c3
014c10  c3 bf cf 68 e1 63 09 81 0f 04 e3 40 a0 98 42 60
014c20  91 32 6b ee 76 0f bf 3b 7c 3c c1 e6 d5 e3 99 b4
and much more data.

I checked these values like 0x2737, 0x53d4 and so on to test in the equation and it stills giving me wrong values. I also tested the opposite, like 0x3727and 0xd453
Title: Re: Decoding FLIR image's raw data with python script.
Post by: Phil Harvey on November 05, 2017, 02:48:49 PM
Ah, the raw data is in the form of a PNG image in this file.
Title: Re: Decoding FLIR image's raw data with python script.
Post by: Leonardo on November 06, 2017, 07:08:45 PM
Yes yes. So... what should I do? Sorry.
Title: Re: Decoding FLIR image's raw data with python script.
Post by: Phil Harvey on November 07, 2017, 07:02:13 AM
So you need to process the PNG image to extract the image data. 

Here is an example of how this may be done. (https://exiftool.org/forum/index.php/topic,4898.msg24156.html#msg24156)

- Phil
Title: Re: Decoding FLIR image's raw data with python script.
Post by: Leonardo on November 07, 2017, 06:28:43 PM
I'm getting the raw data using

exiftool -b -rawthermalimage FLIR.jpg > out.dat

And then getting these values in hex format using

hexdump out.dat

So I get the first two values (first 16 bits) and put in the equation and test the same thing with these two values (each one 8 bits) reversed. What's the problem with this process?
Title: Re: Decoding FLIR image's raw data with python script.
Post by: StarGeek on November 07, 2017, 06:43:51 PM
Quote from: Leonardo on November 07, 2017, 06:28:43 PMWhat's the problem with this process?

The problem is that the raw data isn't raw data, it's a PNG Image (https://en.wikipedia.org/wiki/Portable_Network_Graphics).  You will need to decode the PNG before you can treat it as raw.
Title: Re: Decoding FLIR image's raw data with python script.
Post by: Leonardo on November 08, 2017, 07:24:11 PM
ohh ok. Now I got it. I didn't realized the -rawthermalimage parameter is not what I was looking for. So how can I get and decode the raw data in exiftool?
Title: Re: Decoding FLIR image's raw data with python script.
Post by: Phil Harvey on November 08, 2017, 09:03:03 PM
The raw thermal image is stored in PNG format by some FLIR models.  The tag name is a bit misleading for these models I agree, but older models did store it as raw binary data.  ExifTool just extracts whatever is stored.  Read the post I linked earlier for an example of how to manipulate the PNG image, or use a utility like imagemagick to convert it to raw data if you want.

- Phil
Title: Re: Decoding FLIR image's raw data with python script.
Post by: edgar_eacg on June 10, 2019, 10:53:57 AM
Quote from: Phil Harvey on November 08, 2017, 09:03:03 PM
The raw thermal image is stored in PNG format by some FLIR models.  The tag name is a bit misleading for these models I agree, but older models did store it as raw binary data.  ExifTool just extracts whatever is stored.  Read the post I linked earlier for an example of how to manipulate the PNG image, or use a utility like imagemagick to convert it to raw data if you want.

- Phil

Please, could you give more guidelines on how to do this (convert the png encoded rawThermalImage into the radiometric values)? I've followed and tried the process in the suggested link to reproduce results with the same image. But, even though I've installed ImageMagick and Exiftool I receive command errors... "invalid parameter: -set", "invalid parameter: gray:-", "invalid parameter: 16".
What kind of conversion is performed on the radiometric values contained in the "rawThermalImage".
Attached I provide a T630sc Flir model image which, in effect, is a PNG raw thermal image Type.
Title: Re: Decoding FLIR image's raw data with python script.
Post by: Phil Harvey on June 10, 2019, 11:03:42 AM
The problem with the PNG image is that the bytes are in the wrong order.  The 16-bit samples are stored as a 2-byte integer, and these bytes need to be swapped for each pixel in the image.  Once this is done, the range of pixel values should be 0 to 65535, which should correspond to the range of temperatures in the scale (-25.6 to 39.4 C).

Your problem seems to be with ImageMagick when you are doing the byte swapping.  Perhaps an ImageMagick forum would be a better place to ask about problems with this.

- Phil
Title: Re: Decoding FLIR image's raw data with python script.
Post by: edgar_eacg on June 19, 2019, 10:43:47 AM
OK. Sorry for my delay. I was out. And thanks a lot for your prompt response.

Maybe I've not understood very well... I think there must be something else than just swapping the byte order for the PNG decoding. Below I show a fragment of the raw thermal image (I gotten it with exiftool -RawThermalImage -b FLIR0032.jpg >out.dat


0A B6 24 AB 6B 34 A3 99 D1 F4 3B 77 E6 F6 DE DE
93 EF E7 7C EF D6 3B AC 9C 67 3D CF 73 CA 2E BF
BE 7F FB B7 CB 99 3F F4 D5 9B D3 E6 74 6B BA 39
2D F9 DF 9C A6 69 5E CF A6 FC 2E D3 46 AE 7C DD
9B A6 DB F9 DC 9A 6E 24 AD F4 52 CB E3 90 6A CA

It's not enough just to swapp the byte order to get the digital number. I think this is the last step after the PNG decoding process but... how it is this decoding process?

First af all, Thanks
Title: Re: Decoding FLIR image's raw data with python script.
Post by: Phil Harvey on June 19, 2019, 10:52:06 AM
Use an image converter to convert the PNG to a raw image, then swap the bytes in that.