Skip to content

Commit c51276b

Browse files
Merge pull request #43 from hhoppe:main
PiperOrigin-RevId: 638219237
2 parents 81847d5 + 58b8a07 commit c51276b

File tree

6 files changed

+1242
-488
lines changed

6 files changed

+1242
-488
lines changed

.github/workflows/pytest_and_autopublish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
strategy:
1818
matrix:
19-
python-version: ['3.8', '3.x']
19+
python-version: ['3.10', '3.x']
2020

2121
steps:
2222
- uses: actions/checkout@v3

mediapy/__init__.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
from __future__ import annotations
105105

106106
__docformat__ = 'google'
107-
__version__ = '1.2.0'
107+
__version__ = '1.2.1'
108108
__version_info__ = tuple(int(num) for num in __version__.split('.'))
109109

110110
import base64
@@ -116,6 +116,7 @@
116116
import itertools
117117
import math
118118
import numbers
119+
import os # Package only needed for typing.TYPE_CHECKING.
119120
import pathlib
120121
import re
121122
import shlex
@@ -134,11 +135,9 @@
134135
import PIL.Image
135136
import PIL.ImageOps
136137

137-
if typing.TYPE_CHECKING:
138-
import os # pylint: disable=g-bad-import-order
139138

140139
if not hasattr(PIL.Image, 'Resampling'): # Allow Pillow<9.0.
141-
PIL.Image.Resampling = PIL.Image
140+
PIL.Image.Resampling = PIL.Image # type: ignore
142141

143142
# Selected and reordered here for pdoc documentation.
144143
__all__ = [
@@ -189,7 +188,7 @@
189188

190189
_IPYTHON_HTML_SIZE_LIMIT = 20_000_000
191190
_T = typing.TypeVar('_T')
192-
_Path = typing.Union[str, 'os.PathLike[str]']
191+
_Path = typing.Union[str, os.PathLike[str]]
193192

194193
_IMAGE_COMPARISON_HTML = """\
195194
<script
@@ -420,7 +419,8 @@ def to_type(array: _ArrayLike, dtype: _DTypeLike) -> _NDArray:
420419
a = a.astype(np.float64) * (dst_max / src_max) + 0.5
421420
dst = np.atleast_1d(a)
422421
values_too_large = dst >= np.float64(dst_max)
423-
dst = dst.astype(dtype)
422+
with np.errstate(invalid='ignore'):
423+
dst = dst.astype(dtype)
424424
dst[values_too_large] = dst_max
425425
result = dst if a.ndim > 0 else dst[0]
426426
else:
@@ -606,7 +606,8 @@ def _pil_image(image: _ArrayLike, mode: str | None = None) -> PIL.Image.Image:
606606
image = _as_valid_media_array(image)
607607
if image.ndim not in (2, 3):
608608
raise ValueError(f'Image shape {image.shape} is neither 2D nor 3D.')
609-
return PIL.Image.fromarray(image, mode=mode)
609+
pil_image: PIL.Image.Image = PIL.Image.fromarray(image, mode=mode) # type: ignore[no-untyped-call]
610+
return pil_image
610611

611612

612613
def resize_image(image: _ArrayLike, shape: tuple[int, int]) -> _NDArray:
@@ -884,9 +885,11 @@ def decompress_image(
884885
"""
885886
pil_image = PIL.Image.open(io.BytesIO(data))
886887
if apply_exif_transpose:
887-
pil_image = PIL.ImageOps.exif_transpose(pil_image)
888+
tmp_image = PIL.ImageOps.exif_transpose(pil_image) # Future: in_place=True.
889+
assert tmp_image
890+
pil_image = tmp_image
888891
if dtype is None:
889-
dtype = np.uint16 if pil_image.mode == 'I' else np.uint8
892+
dtype = np.uint16 if pil_image.mode.startswith('I') else np.uint8
890893
return np.array(pil_image, dtype=dtype)
891894

892895

@@ -1222,15 +1225,15 @@ def _get_video_metadata(path: _Path) -> VideoMetadata:
12221225
_, err = proc.communicate()
12231226
bps = fps = num_images = width = height = rotation = None
12241227
for line in err.split('\n'):
1225-
if match := re.search(r', bitrate: *([0-9.]+) kb/s', line):
1228+
if match := re.search(r', bitrate: *([\d.]+) kb/s', line):
12261229
bps = int(match.group(1)) * 1000
1227-
if matches := re.findall(r'frame= *([0-9]+) ', line):
1230+
if matches := re.findall(r'frame= *(\d+) ', line):
12281231
num_images = int(matches[-1])
12291232
if 'Stream #0:' in line and ': Video:' in line:
1230-
if not (match := re.search(r', ([0-9]+)x([0-9]+)', line)):
1233+
if not (match := re.search(r', (\d+)x(\d+)', line)):
12311234
raise RuntimeError(f'Unable to parse video dimensions in line {line}')
12321235
width, height = int(match.group(1)), int(match.group(2))
1233-
if match := re.search(r', ([0-9.]+) fps', line):
1236+
if match := re.search(r', ([\d.]+) fps', line):
12341237
fps = float(match.group(1))
12351238
elif str(path).endswith('.gif'):
12361239
# Some GIF files lack a framerate attribute; use a reasonable default.

0 commit comments

Comments
 (0)