Skip to content

Commit eba4e53

Browse files
authored
Handle unknown compression schemes (#62)
1 parent 4600be4 commit eba4e53

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

python/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
*.whl
2+
13
# Byte-compiled / optimized / DLL files
24
__pycache__/
35
*.py[cod]

python/python/async_tiff/_ifd.pyi

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ class ImageFileDirectory:
2424
@property
2525
def bits_per_sample(self) -> list[int]: ...
2626
@property
27-
def compression(self) -> CompressionMethod: ...
27+
def compression(self) -> CompressionMethod | int:
28+
"""Access the compression tag.
29+
30+
An `int` will be returned if the compression is not one of the values in
31+
`CompressionMethod`.
32+
"""
2833
@property
2934
def photometric_interpretation(self) -> PhotometricInterpretation: ...
3035
@property

python/src/enums.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use async_tiff::tiff::tags::{
22
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, Predictor, ResolutionUnit,
33
SampleFormat,
44
};
5-
use pyo3::intern;
65
use pyo3::prelude::*;
76
use pyo3::types::{PyString, PyTuple};
7+
use pyo3::{intern, IntoPyObjectExt};
88

99
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1010
pub(crate) struct PyCompressionMethod(CompressionMethod);
@@ -136,5 +136,10 @@ fn to_py_enum_variant<'py>(
136136
value: u16,
137137
) -> PyResult<Bound<'py, PyAny>> {
138138
let enums_mod = py.import(intern!(py, "async_tiff.enums"))?;
139-
enums_mod.call_method1(enum_name, PyTuple::new(py, vec![value])?)
139+
if let Ok(enum_variant) = enums_mod.call_method1(enum_name, PyTuple::new(py, vec![value])?) {
140+
Ok(enum_variant)
141+
} else {
142+
// If the value is not included in the enum, return the integer itself
143+
value.into_bound_py_any(py)
144+
}
140145
}

0 commit comments

Comments
 (0)