Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix numpy2 compatibility #183

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
fail-fast: true
matrix:
os: ["windows-latest", "ubuntu-latest", "macos-latest"]
python-version: ["3.8", "3.9", "3.10"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
experimental: [false]
system-libtiff: [false]
include:
Expand Down
23 changes: 15 additions & 8 deletions libtiff/libtiff_ctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@

__all__ = ['libtiff', 'TIFF']

SCTYPES = {
"float": [np.float16, np.float32, np.float64],
"uint": [np.uint8, np.uint16, np.uint32, np.uint64, np.bool_],
"int": [np.int8, np.int16, np.int32, np.int64],
"complex": [np.complex64, np.complex128],
}

cwd = os.getcwd()
try:
os.chdir(os.path.dirname(__file__))
Expand Down Expand Up @@ -729,13 +736,13 @@ def write_image(self, arr, compression=None, write_rgb=False):
compression = self._fix_compression(compression)

arr = np.ascontiguousarray(arr)
if arr.dtype in np.sctypes['float']:
if arr.dtype in SCTYPES["float"]:
sample_format = SAMPLEFORMAT_IEEEFP
elif arr.dtype in np.sctypes['uint'] + [np.bool_]:
elif arr.dtype in SCTYPES["uint"]:
sample_format = SAMPLEFORMAT_UINT
elif arr.dtype in np.sctypes['int']:
elif arr.dtype in SCTYPES["int"]:
sample_format = SAMPLEFORMAT_INT
elif arr.dtype in np.sctypes['complex']:
elif arr.dtype in SCTYPES["complex"]:
sample_format = SAMPLEFORMAT_COMPLEXIEEEFP
else:
raise NotImplementedError(repr(arr.dtype))
Expand Down Expand Up @@ -816,13 +823,13 @@ def write_tiles(self, arr, tile_width=None, tile_height=None,
compression=None, write_rgb=False):
compression = self._fix_compression(compression)

if arr.dtype in np.sctypes['float']:
if arr.dtype in SCTYPES["float"]:
sample_format = SAMPLEFORMAT_IEEEFP
elif arr.dtype in np.sctypes['uint'] + [np.bool_]:
elif arr.dtype in SCTYPES["uint"]:
sample_format = SAMPLEFORMAT_UINT
elif arr.dtype in np.sctypes['int']:
elif arr.dtype in SCTYPES["int"]:
sample_format = SAMPLEFORMAT_INT
elif arr.dtype in np.sctypes['complex']:
elif arr.dtype in SCTYPES["complex"]:
sample_format = SAMPLEFORMAT_COMPLEXIEEEFP
else:
raise NotImplementedError(repr(arr.dtype))
Expand Down
2 changes: 1 addition & 1 deletion libtiff/test_bittools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_setgetbit():


def test_setgetword():
for dtype in [numpy.ubyte, numpy.int32, numpy.float64]:
for dtype in [numpy.int32, numpy.float64]:
arr = numpy.array(list(range(-256, 256)), dtype=dtype)
arr2 = numpy.zeros(arr.shape, dtype=arr.dtype)
for i in range(arr.nbytes):
Expand Down
1 change: 1 addition & 0 deletions libtiff/tiff_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ def tif_write(_tif, _offset, _data):

compression = 1 / (float(compressed_data_size) / image_data_size)

total_size = int(total_size)
if compressed_data_size != image_data_size:
sdiff = image_data_size - compressed_data_size
total_size -= sdiff
Expand Down
1 change: 1 addition & 0 deletions libtiff/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def isindisk(path):

def bytes2str(bytes):
lst = []
bytes = int(bytes)
SimonSegerblomRex marked this conversation as resolved.
Show resolved Hide resolved
Pbytes = bytes // 1024**5
if Pbytes:
lst.append('%sPi' % (Pbytes))
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools>=60", "wheel", "setuptools_scm[toml]>=8.0", 'oldest-supported-numpy']
requires = ["setuptools>=60", "wheel", "setuptools_scm[toml]>=8.0", "numpy>=2.0"]
pearu marked this conversation as resolved.
Show resolved Hide resolved
build-backend = "setuptools.build_meta"

[tool.setuptools_scm]
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def setup_package():
"Intended Audience :: Science/Research",
"License :: OSI Approved",
"Programming Language :: Python",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering",
"Topic :: Software Development",
"Operating System :: Microsoft :: Windows",
Expand All @@ -42,8 +42,8 @@ def setup_package():
description='PyLibTiff: a Python tiff library.',
long_description=long_description,
long_description_content_type='text/markdown',
install_requires=['numpy>=1.13.3'],
python_requires='>=3.8',
install_requires=['numpy>=1.23.5'],
python_requires='>=3.9',
extras_require={
'bitarray': ['bitarray'],
},
Expand Down
Loading