Skip to content

Commit

Permalink
gdalenhance: add minimal tests
Browse files Browse the repository at this point in the history
Co-authored-by: Even Rouault <[email protected]>
  • Loading branch information
dbaston and rouault committed Oct 28, 2024
1 parent da6662d commit f5c07f2
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
8 changes: 8 additions & 0 deletions autotest/pymod/test_cli_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ def get_gdaldem_path():
#


def get_gdalenhance_path():
return get_cli_utility_path("gdalenhance")


###############################################################################
#


def get_gdal_rasterize_path():
return get_cli_utility_path("gdal_rasterize")

Expand Down
90 changes: 90 additions & 0 deletions autotest/utilities/test_gdalenhance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env pytest
# -*- coding: utf-8 -*-
###############################################################################
#
# Project: GDAL/OGR Test Suite
# Purpose: gdalenhance testing
# Author: Daniel Baston <dbaston at gmail.com>
#
###############################################################################
# Copyright (c) 2024, ISciences LLC
#
# SPDX-License-Identifier: MIT
###############################################################################

import gdaltest
import pytest
import test_cli_utilities

from osgeo import gdal

pytestmark = pytest.mark.skipif(
test_cli_utilities.get_gdalenhance_path() is None,
reason="gdalenhance not available",
)


@pytest.fixture()
def gdalenhance_path():
return test_cli_utilities.get_gdalenhance_path()


###############################################################################
# Output a lookup table, then apply it to the image


def test_gdalenhance_output_histogram(gdalenhance_path, tmp_path):

out, err = gdaltest.runexternal_out_and_err(
f"{gdalenhance_path} -equalize ../gcore/data/rgbsmall.tif"
)

assert not err

lines = out.strip().split("\n")
assert len(lines) == 3

assert lines[0].startswith("1:Band ")
assert lines[1].startswith("2:Band ")
assert lines[2].startswith("3:Band ")

lut_fname = tmp_path / "lut.txt"

with open(lut_fname, "w") as outfile:
for line in lines:
outfile.write(line.strip())
outfile.write("\n")

enhanced_fname = tmp_path / "out.tif"

out, err = gdaltest.runexternal_out_and_err(
f"{gdalenhance_path} -config {lut_fname} ../gcore/data/rgbsmall.tif {enhanced_fname}"
)

assert not err

assert enhanced_fname.exists()


###############################################################################
# Write a new image directly


def test_gdalenhance_output_image(gdalenhance_path, tmp_path):

infile = "../gcore/data/rgbsmall.tif"
outfile = tmp_path / "out.tif"

out, err = gdaltest.runexternal_out_and_err(
f"{gdalenhance_path} -quiet -equalize -co COMPRESS=DEFLATE {infile} {outfile}"
)

assert not err

with gdal.Open(infile) as src, gdal.Open(outfile) as dst:
assert src.RasterCount == dst.RasterCount
assert src.RasterXSize == dst.RasterXSize
assert src.RasterYSize == dst.RasterYSize

# check that -co was honored
assert dst.GetMetadata("IMAGE_STRUCTURE")["COMPRESSION"] == "DEFLATE"

0 comments on commit f5c07f2

Please sign in to comment.