summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2023-12-18 14:22:45 +0100
committerjaseg <git@jaseg.de>2023-12-18 14:22:45 +0100
commit7840074004536b44396083fcab75a9e6355e4a42 (patch)
tree3bdfd3e907e5548de679722e73a1288b8f070acd
downloadinfiray_irg_py-7840074004536b44396083fcab75a9e6355e4a42.tar.gz
infiray_irg_py-7840074004536b44396083fcab75a9e6355e4a42.tar.bz2
infiray_irg_py-7840074004536b44396083fcab75a9e6355e4a42.zip
Initial commitv1.0
-rw-r--r--.gitignore2
-rw-r--r--LICENSE7
-rw-r--r--README.md58
-rw-r--r--example.irgbin0 -> 181235 bytes
-rw-r--r--example.py27
-rw-r--r--pyproject.toml22
-rw-r--r--src/infiray_irg.py36
7 files changed, 152 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..762d1c5
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+**/__pycache__
+plot.png
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..8c45181
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,7 @@
+Copyright 2023 Jan Götte <code@jaseg.de>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..8b73e56
--- /dev/null
+++ b/README.md
@@ -0,0 +1,58 @@
+# Infiray IRG file format parser
+
+This python module contains a simple parser for the "IRG" file format that the Infiray C200 series thermal cameras use
+to dump their raw data. The files contain three things: A thermal image with 8 bit resolution with contrast scaled to
+fit the 0...255 range, a thermal image with 16 bit resolution containing absolute temperature values with 1/16 K
+resolution, and a JPEG with the image from the low-res visual camera.
+
+## Requirements
+
+pillow and numpy
+
+## API
+
+Call `infiray_irg.load(data)` with a `bytes` object containing the IRG file's contents. It will return a tuple `(coarse,
+fine, vis)` of the coarse and fine images as numpy arrays, followed by the visual image as a pillow image. The coarse
+image has dtype uint8 and contains values from 0 to 255, where 0 is the coldest pixel in the image, and 255 is the
+hottest. The fine image has dtype float and contains absolute degree Celsius values.
+
+## Example
+
+```python
+from matplotlib import pyplot as plt
+from pathlib import Path
+
+import infiray_irg
+
+coarse, fine, vis = infiray_irg.load(Path('example.irg').read_bytes())
+
+fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(15, 18))
+
+ax1.imshow(coarse)
+ax1.set_title('Coarse contrast-maximized')
+
+fine_plt = ax2.imshow(fine)
+ax2.set_title('Fine absolute temperatures')
+fig.colorbar(fine_plt, ax=ax2, location='right', label='degrees Celsius')
+
+ax3.imshow(vis)
+ax3.set_title('Visual')
+
+ax4.hist(fine.flatten(), bins=100)
+ax4.set_title('Temperature histogram')
+ax4.set_xlabel('degrees Celsius')
+
+fig.tight_layout()
+fig.savefig('plot.png')
+
+print('Coldest pixel:', fine.min(), 'C', 'Hottest pixel:', fine.max(), 'C')
+```
+
+## Bugs
+
+If you find a bug, or find a file that this library can't load, please send me an email at <code@jaseg.de>.
+
+## License
+
+This module is licensed under the MIT license.
+
diff --git a/example.irg b/example.irg
new file mode 100644
index 0000000..6c2a55a
--- /dev/null
+++ b/example.irg
Binary files differ
diff --git a/example.py b/example.py
new file mode 100644
index 0000000..0cb97cc
--- /dev/null
+++ b/example.py
@@ -0,0 +1,27 @@
+from matplotlib import pyplot as plt
+from pathlib import Path
+
+import infiray_irg
+
+coarse, fine, vis = infiray_irg.load(Path('example.irg').read_bytes())
+
+fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(15, 18))
+
+ax1.imshow(coarse)
+ax1.set_title('Coarse contrast-maximized')
+
+fine_plt = ax2.imshow(fine)
+ax2.set_title('Fine absolute temperatures')
+fig.colorbar(fine_plt, ax=ax2, location='right', label='degrees Celsius')
+
+ax3.imshow(vis)
+ax3.set_title('Visual')
+
+ax4.hist(fine.flatten(), bins=100)
+ax4.set_title('Temperature histogram')
+ax4.set_xlabel('degrees Celsius')
+
+fig.tight_layout()
+fig.savefig('plot.png')
+
+print('Coldest pixel:', fine.min(), 'C', 'Hottest pixel:', fine.max(), 'C')
diff --git a/pyproject.toml b/pyproject.toml
new file mode 100644
index 0000000..8ac7d29
--- /dev/null
+++ b/pyproject.toml
@@ -0,0 +1,22 @@
+[build-system]
+requires = ["hatchling"]
+build-backend = "hatchling.build"
+
+[project]
+name = "infiray_irg"
+version = "1.0.0"
+authors = [
+ { name="jaseg", email="code@jaseg.de" },
+]
+description = "A simple parser for Infiray IRG radiometric thermal imaging files"
+readme = "README.md"
+requires-python = ">=3.8"
+classifiers = [
+ "Programming Language :: Python :: 3",
+ "License :: OSI Approved :: MIT License",
+ "Operating System :: OS Independent",
+]
+
+[project.urls]
+Homepage = "https://jaseg.de/project/infiray_irg"
+Issues = "https://github.com/jaseg/infiray_irg/issues"
diff --git a/src/infiray_irg.py b/src/infiray_irg.py
new file mode 100644
index 0000000..9d573f6
--- /dev/null
+++ b/src/infiray_irg.py
@@ -0,0 +1,36 @@
+from PIL import Image
+import numpy as np
+import struct
+import io
+
+def load(data):
+ def consume(n):
+ nonlocal data
+ out, data = data[:n], data[n:]
+ if len(out) < n:
+ raise ValueError('file is truncated')
+ return out
+
+ header = consume(128)
+ if header[:2] != bytes([0xca, 0xac]) or header[-2:] != bytes([0xac, 0xca]):
+ raise ValueError('Header magic not found.')
+
+ _unk0, coarse_section_length, y_res, x_res,\
+ _zero0, _unk1, _zero1, fine_offset, _unk2, jpeg_length,\
+ y_res_2, x_res_2, _unk3, = struct.unpack('<HIHHHHHHHIHHI', header[2:34])
+
+ _zero_celsius0, _zero_celsius1, *rest, high_gain_mode_flag = struct.unpack('<11I', header[34:78])
+
+ if (x_res, y_res) != (x_res_2, y_res_2):
+ raise ValueError(f'Resolution mismatch in header: {x_res}*{y_res} != {x_res_2}*{y_res_2}')
+
+ if x_res*y_res != coarse_section_length:
+ raise ValueError('Resolution mismatch in header')
+
+ coarse_img = np.frombuffer(consume(coarse_section_length), dtype=np.uint8).reshape((y_res, x_res))
+ fine_img = np.frombuffer(consume(x_res*y_res*2), dtype=np.int16).reshape((y_res, x_res))
+ fine_img = (fine_img / 16) - 273
+ vis_jpg = Image.open(io.BytesIO(consume(jpeg_length)))
+
+ return coarse_img, fine_img, vis_jpg
+