Skip to content

Commit

Permalink
Merge pull request #41 from SenteraLLC/lwir_metadata_fix
Browse files Browse the repository at this point in the history
LWIR metadata fix
  • Loading branch information
SamCWill authored Aug 3, 2023
2 parents e49d87d + c4c885d commit d029b26
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
2 changes: 1 addition & 1 deletion imgcorrect/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.7.0"
__version__ = "1.7.1"
76 changes: 40 additions & 36 deletions imgcorrect/thermal_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import shutil
import tempfile

import imageio
import numpy as np
Expand All @@ -22,47 +23,50 @@ def convert_thermal(input_path, output_path, exiftool_path):
if os.path.isfile(os.path.join(input_path, f)) and f.endswith(".tif")
]

# copy image to output location
for image in images:
if "CAL" not in image:
output_image_path = os.path.join(output_path, image)
logger.info("output_path: {}".format(output_image_path))
if input_path != output_path:
with tempfile.TemporaryDirectory() as temp_dir:
# copy image to output location
for image in images:
if "CAL" not in image:
# Copy image to temporary directory
output_image_path = os.path.join(temp_dir, image)
shutil.copy(os.path.join(input_path, image), output_image_path)

# open image in output folder and get image pixel data
image_io_reader = imageio.get_reader(output_image_path, format="tif")
image_io = image_io_reader.get_data(0)
# open image in output folder and get image pixel data
image_io_reader = imageio.get_reader(output_image_path, format="tif")
image_io = image_io_reader.get_data(0)

# multiply pixel data by scale coefficient and write to file
image_io_corrected = (image_io / 100 - 273.15).astype(np.float32)
# multiply pixel data by scale coefficient and write to file
image_io_corrected = (image_io / 100 - 273.15).astype(np.float32)

image_io_writer = imageio.get_writer(output_image_path, format="tif")
image_io_writer.append_data(image_io_corrected)
image_io_writer = imageio.get_writer(output_image_path, format="tif")
image_io_writer.append_data(image_io_corrected)

image_io_reader.close()
image_io_writer.close()
image_io_reader.close()
image_io_writer.close()

# copy exif and xmp data from input image to corrected image
logger.info("copying exif data")
os.system(
f'{exiftool_path} -overwrite_original -TagsFromFile "{os.path.join(input_path, image)}" "-xmp" "-exif" "-all" "{output_image_path}"'
)
# copy exif and xmp data from input image to corrected image
logger.info("copying exif data")
os.system(
f'{exiftool_path} -overwrite_original -TagsFromFile "{os.path.join(input_path, image)}" "-xmp" "-exif" "-all" "{output_image_path}"'
)

logger.info("editing band info")
os.system(
f'{exiftool_path} -config "cfg/exiftool.cfg" -overwrite_original "-xmp-Camera:BandName=LWIR" "-xmp-Camera:CentralWavelength=11000" "-xmp-Camera:WavelengthFWHM=6000" "{output_image_path}" '
)
logger.info("editing band info")
os.system(
f'{exiftool_path} -config "cfg/exiftool.cfg" -overwrite_original "-xmp-Camera:BandName=LWIR" "-xmp-Camera:CentralWavelength=11000" "-xmp-Camera:WavelengthFWHM=6000" "{output_image_path}" '
)

output_files = [
f
for f in os.listdir(output_path)
if os.path.isfile(os.path.join(output_path, f))
]
for file in output_files:
if file.endswith("original"):
try:
os.remove(os.path.join(output_path, file))
logger.info("additional file deleted")
except Exception as e:
logger.error(f"File delete failed with error {e}")
# Copy output from temp directory to output directory
shutil.copy(output_image_path, os.path.join(output_path, image))

output_files = [
f
for f in os.listdir(output_path)
if os.path.isfile(os.path.join(output_path, f))
]
for file in output_files:
if file.endswith("original"):
try:
os.remove(os.path.join(output_path, file))
logger.info("additional file deleted")
except Exception as e:
logger.error(f"File delete failed with error {e}")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "imgcorrect"
version = "1.7.0"
version = "1.7.1"
description = "Library to perform various corrections on imagery from supported sensors"
authors = ["Samuel Williams <sam.williams@sentera.com>"]

Expand Down

0 comments on commit d029b26

Please sign in to comment.