Skip to content

Commit 6dbde2c

Browse files
author
The mediapy Authors
committed
Merge pull request #61 from hhoppe:main
PiperOrigin-RevId: 845802990
2 parents cc7a8c5 + c7a44d7 commit 6dbde2c

3 files changed

Lines changed: 22 additions & 18 deletions

File tree

mediapy/__init__.py

Lines changed: 19 additions & 17 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.4'
107+
__version__ = '1.2.5'
108108
__version_info__ = tuple(int(num) for num in __version__.split('.'))
109109

110110
import base64
@@ -187,7 +187,7 @@
187187
_NDArray = typing.TypeVar('_NDArray')
188188
_DType = typing.TypeVar('_DType') # pylint: disable=invalid-name
189189

190-
_IPYTHON_HTML_SIZE_LIMIT = 20_000_000
190+
_IPYTHON_HTML_SIZE_LIMIT = 10**10 # Unlimited seems to be OK now.
191191
_T = typing.TypeVar('_T')
192192
_Path = typing.Union[str, 'os.PathLike[str]']
193193

@@ -681,7 +681,9 @@ def read_contents(path_or_url: _Path) -> bytes:
681681
data: bytes
682682
if _is_url(path_or_url):
683683
assert isinstance(path_or_url, str)
684-
with urllib.request.urlopen(path_or_url) as response:
684+
headers = {'User-Agent': 'Chrome'}
685+
request = urllib.request.Request(path_or_url, headers=headers)
686+
with urllib.request.urlopen(request) as response:
685687
data = response.read()
686688
else:
687689
with _open(path_or_url, 'rb') as f:
@@ -846,7 +848,7 @@ def to_rgb(
846848
rgb_from_scalar = matplotlib.pyplot.cm.get_cmap(cmap) # pylint: disable=no-member
847849
else:
848850
rgb_from_scalar = cmap
849-
a = rgb_from_scalar(a)
851+
a = typing.cast(_NDArray, rgb_from_scalar(a))
850852
# If there is a fully opaque alpha channel, remove it.
851853
if a.shape[-1] == 4 and np.all(to_float01(a[..., 3])) == 1.0:
852854
a = a[..., :3]
@@ -1068,7 +1070,7 @@ def show_images(
10681070
]
10691071

10701072
def maybe_downsample(image: _NDArray) -> _NDArray:
1071-
shape: tuple[int, int] = image.shape[:2]
1073+
shape = image.shape[0], image.shape[1]
10721074
w, h = _get_width_height(width, height, shape)
10731075
if w < shape[1] or h < shape[0]:
10741076
image = resize_image(image, (h, w))
@@ -1111,6 +1113,7 @@ def html_from_compressed_images() -> str:
11111113

11121114
s = html_from_compressed_images()
11131115
while len(s) > _IPYTHON_HTML_SIZE_LIMIT * 0.5:
1116+
warnings.warn('mediapy: subsampling images to reduce HTML size')
11141117
list_images = [image[::2, ::2] for image in list_images]
11151118
png_datas = [compress_image(to_uint8(image)) for image in list_images]
11161119
s = html_from_compressed_images()
@@ -1164,7 +1167,7 @@ def compare_images(
11641167
def _filename_suffix_from_codec(codec: str) -> str:
11651168
if codec == 'gif':
11661169
return '.gif'
1167-
elif codec == 'vp9':
1170+
if codec == 'vp9':
11681171
return '.webm'
11691172

11701173
return '.mp4'
@@ -1206,16 +1209,15 @@ def _run_ffmpeg(
12061209
...
12071210

12081211

1209-
# Only typing.override should have typing annotations
12101212
def _run_ffmpeg(
1211-
ffmpeg_args,
1212-
stdin=None,
1213-
stdout=None,
1214-
stderr=None,
1215-
encoding=None,
1216-
allowed_input_files=None,
1217-
allowed_output_files=None,
1218-
):
1213+
ffmpeg_args: Sequence[str],
1214+
stdin: int | None = None,
1215+
stdout: int | None = None,
1216+
stderr: int | None = None,
1217+
encoding: str | None = None,
1218+
allowed_input_files: Sequence[str] | None = None,
1219+
allowed_output_files: Sequence[str] | None = None,
1220+
) -> subprocess.Popen[bytes] | subprocess.Popen[str]:
12191221
"""Runs ffmpeg with the given args.
12201222
12211223
Args:
@@ -1231,7 +1233,7 @@ def _run_ffmpeg(
12311233
The subprocess.Popen object with running ffmpeg process.
12321234
"""
12331235
argv = []
1234-
env = {}
1236+
env: Any = {}
12351237
ffmpeg_path = _get_ffmpeg_path()
12361238

12371239
# Allowed input and output files are not supported in open source.
@@ -1837,7 +1839,7 @@ def write_video(path: _Path, images: Iterable[_NDArray], **kwargs: Any) -> None:
18371839
**kwargs: Additional parameters for `VideoWriter`.
18381840
"""
18391841
first_image, images = _peek_first(images)
1840-
shape: tuple[int, int] = first_image.shape[:2]
1842+
shape = first_image.shape[0], first_image.shape[1]
18411843
dtype = first_image.dtype
18421844
if dtype == bool:
18431845
dtype = np.dtype(np.uint8)

mediapy_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def darken_image(image):
155155
# %%
156156
# Show multiple videos side-by-side.
157157
s = 90
158-
videos = {
158+
videos: Any = {
159159
'mirror loop': np.concatenate([video3, video3[::-1]], axis=0),
160160
'roll': (np.roll(media.color_ramp((s, s)), i, axis=0) for i in range(s)),
161161
'fade': (np.full((s, s), f) for f in np.linspace(0.0, 1.0, 50)),

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ aggressive = 3
5656
recursive = true
5757

5858
[tool.mypy]
59+
cache_dir = "~/.cache/mypy"
5960
strict = true
6061
ignore_missing_imports = true
6162

@@ -82,6 +83,7 @@ disable = [
8283
"unspecified-encoding", "line-too-long", "too-many-lines",
8384
"too-few-public-methods", "too-many-locals", "too-many-instance-attributes",
8485
"too-many-branches", "too-many-statements", "too-many-arguments",
86+
"too-many-positional-arguments",
8587
"using-constant-test", "wrong-import-order", "use-dict-literal",
8688
"missing-module-docstring",
8789
]

0 commit comments

Comments
 (0)