104104from __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
110110import base64
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(
11641167def _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
12101212def _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 )
0 commit comments