1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
import atexit
from types import GeneratorType
import pytest
from PIL import Image
from term_image.ctlseqs import SGR_NORMAL
from term_image.exceptions import TermImageError
from term_image.image import BlockImage, ImageIterator, Size
_size = (30, 15)
png_img = Image.open("tests/images/python.png")
png_image = BlockImage(png_img)
gif_img = Image.open("tests/images/lion.gif")
gif_image = BlockImage(gif_img)
webp_img = Image.open("tests/images/anim.webp")
webp_image = BlockImage(webp_img)
gif_image._size = _size
webp_image._size = _size
@atexit.register
def close_imgs():
for img in (png_img, gif_img, webp_img):
img.close()
def test_args():
for value in ("tests/images/anim.webp", gif_img, webp_img):
with pytest.raises(TypeError, match="'image'"):
ImageIterator(value)
with pytest.raises(ValueError, match="not animated"):
ImageIterator(png_image)
for value in (None, 2.0, 0.2, "2"):
with pytest.raises(TypeError, match="'repeat'"):
ImageIterator(gif_image, value)
with pytest.raises(ValueError, match="'repeat'"):
ImageIterator(gif_image, 0)
for value in (None, 2.0, 2):
with pytest.raises(TypeError, match="'format_spec'"):
ImageIterator(gif_image, format_spec=value)
with pytest.raises(ValueError, match="format specifier"):
ImageIterator(gif_image, format_spec=".")
for value in (None, 2.0, "2"):
with pytest.raises(TypeError, match="'cached'"):
ImageIterator(gif_image, cached=value)
for value in (0, -1, -10):
with pytest.raises(ValueError, match="'cached'"):
ImageIterator(gif_image, cached=value)
class TestInit:
def test_defaults(self):
for image in (gif_image, webp_image):
image_it = ImageIterator(image)
assert image_it._image is image
assert image_it._repeat == -1
assert image_it._format == ""
assert image_it._cached is (image.n_frames <= 100)
assert isinstance(image_it._animator, GeneratorType)
def test_with_args(self):
for repeat, format_spec, cached in (
(-1, "", 100),
(2, "#", True),
(10, "1.1", False),
(100, "#.9", 1),
):
image_it = ImageIterator(gif_image, repeat, format_spec, cached)
assert image_it._image is gif_image
assert image_it._repeat == repeat
assert image_it._format == format_spec
assert image_it._cached is (
cached if isinstance(cached, bool) else gif_image.n_frames <= cached
)
assert isinstance(image_it._animator, GeneratorType)
def test_no_caching_if_repeat_equals_1(self):
for value in (True, 1, 100):
image_it = ImageIterator(gif_image, 1, cached=value)
assert image_it._cached is False
def test_image_seek_position_unchanged(self):
gif_image.seek(2)
image_it = ImageIterator(gif_image)
assert gif_image.tell() == 2
next(image_it)
assert gif_image.tell() == 0
def test_next():
image_it = ImageIterator(gif_image, 1, "1.1")
assert isinstance(next(image_it), str)
for _ in range(gif_image.n_frames - 1):
next(image_it)
with pytest.raises(StopIteration):
next(image_it)
# Frame number is set to zero
assert gif_image.tell() == 0
# Iterator is closed
assert not hasattr(image_it, "_animator")
assert not hasattr(image_it, "_img")
# All calls after StopIteration is first raised also raise StopIteration
for _ in range(2):
with pytest.raises(StopIteration):
next(image_it)
def test_image_seek_has_no_effect():
image_it = ImageIterator(gif_image, 1)
next(image_it)
assert gif_image.tell() == 0
gif_image.seek(4)
next(image_it)
assert gif_image.tell() == 1
def test_iter():
image_it = ImageIterator(gif_image, 1, "1.1")
assert iter(image_it) is image_it
# Image seek position is updated
for n, _ in enumerate(ImageIterator(gif_image, 1, "1.1")):
assert gif_image.tell() == n
for image in (gif_image, webp_image):
frames = tuple(ImageIterator(image, 1, "1.1"))
assert len(frames) == image.n_frames
assert all(isinstance(frame, str) for frame in frames)
# Consecutive frames are different
prev_frame = None
for frame in frames:
assert frame != prev_frame
prev_frame = frame
# Frames are the same as for manual iteration
gif_image2 = BlockImage(gif_img) # Need to change image size
gif_image2._size = _size
for n, frame in enumerate(ImageIterator(gif_image, 1, "1.1")):
gif_image2.seek(n)
assert frame == str(gif_image2)
def test_repeat():
for image in (gif_image, webp_image):
for value in (False, True):
frames = tuple(ImageIterator(image, 2, "1.1", cached=value))
# # Number of frames is multiplied
assert len(frames) == image.n_frames * 2
# # Corresponding frames in different repeat loops are the same
assert frames[: image.n_frames] == frames[image.n_frames :]
def test_caching():
def render(*args, **kwargs):
nonlocal n_calls
n_calls += 1
if gif_image2.tell() == gif_image2.n_frames:
raise EOFError
return ""
gif_image2 = BlockImage(gif_img) # Need to change image size
gif_image2._size = _size
gif_image2._render_image = render
n_calls = 0
[*ImageIterator(gif_image2, 2, "1.1", cached=True)]
assert n_calls == gif_image2.n_frames + 1 # +1 for EOF call
n_calls = 0
[*ImageIterator(gif_image2, 2, "1.1", cached=False)]
assert n_calls == gif_image2.n_frames * 2 + 2 # +2 for EOF calls
del gif_image2._render_image
image_it = ImageIterator(gif_image2, 4, "1.1", cached=True)
gif_image2._size = (20, 40)
frame_0_1 = next(image_it)
assert frame_0_1.count("\n") + 1 == 40
image_it.seek(gif_image2.n_frames - 1)
assert next(image_it).count("\n") + 1 == 40
# Unchanged
assert next(image_it) is frame_0_1
image_it.seek(gif_image2.n_frames - 1)
assert next(image_it).count("\n") + 1 == 40
# Change in size
gif_image2._size = (40, 20)
frame_0_2 = next(image_it)
assert frame_0_2 is not frame_0_1
assert frame_0_2.count("\n") + 1 == 20
image_it.seek(gif_image2.n_frames - 1)
assert next(image_it).count("\n") + 1 == 20
image_it.close()
def test_sizing():
def test(image_it):
for value in Size:
gif_image2.size = value
assert next(image_it).count("\n") + 1 == gif_image2.rendered_height
assert gif_image2.size is value
gif_image2._size = (40, 20)
assert next(image_it).count("\n") + 1 == 20
assert gif_image2._size == (40, 20)
gif_image2._size = (20, 10)
assert next(image_it).count("\n") + 1 == 10
assert gif_image2._size == (20, 10)
gif_image2.size = Size.FIT
next(image_it)
assert gif_image2.size is Size.FIT
gif_image2 = BlockImage(gif_img) # Need to change image size
# Uncached loop
image_it = ImageIterator(gif_image2, 1, "1.1")
test(image_it)
# Cached loop
image_it = ImageIterator(gif_image2, 2, "1.1", True)
for _ in range(gif_image2.n_frames):
next(image_it)
test(image_it)
def test_formatting():
# Transparency enabled, not padded
image_it = ImageIterator(gif_image, 1, "1.1")
assert next(image_it).count("\n") + 1 == _size[1]
# First line without escape codes
assert next(image_it).partition("\n")[0].strip(SGR_NORMAL) == " " * _size[0]
# Transparency disabled, not padded
image_it = ImageIterator(gif_image, 1, "1.1#")
assert next(image_it).count("\n") + 1 == _size[1]
# First line without escape codes
assert next(image_it).partition("\n")[0].strip(SGR_NORMAL) != " " * _size[0]
# Transparency disabled, padded
image_it = ImageIterator(gif_image, 1, f"{_size[0] + 2}.{_size[1] + 2}#")
assert next(image_it).count("\n") + 1 == _size[1] + 2
# First line should be padding, so no escape codes
assert next(image_it).partition("\n")[0] == " " * (_size[0] + 2)
def test_loop_no():
for cached in (False, True):
image_it = ImageIterator(gif_image, 2, cached=cached)
assert image_it.loop_no is None
next(image_it)
assert image_it.loop_no == 2
for _ in range(gif_image.n_frames - 1):
next(image_it)
assert image_it.loop_no == 2
next(image_it)
assert image_it.loop_no == 1
for _ in range(gif_image.n_frames - 1):
next(image_it)
assert image_it.loop_no == 1
with pytest.raises(StopIteration):
next(image_it)
assert image_it.loop_no == 0
def test_close():
# From PIL image-sourced image
image_it = ImageIterator(gif_image, 1)
next(image_it)
img = image_it._img
assert img is gif_img
image_it.close()
assert gif_image.tell() == 0
assert not hasattr(image_it, "_animator")
assert not hasattr(image_it, "_img")
assert img.load()
# File-sourced image
image_it = ImageIterator(BlockImage.from_file(gif_image._source.filename), 1)
next(image_it)
img = image_it._img
image_it.close()
assert gif_image.tell() == 0
assert not hasattr(image_it, "_animator")
assert not hasattr(image_it, "_img")
with pytest.raises(ValueError, match="Operation on closed image"):
img.load()
class TestSeek:
def test_seek_args(self):
image_it = ImageIterator(gif_image)
for value in (1.0, "1", [], ()):
with pytest.raises(TypeError):
image_it.seek(value)
for value in (-2, -1, gif_image.n_frames, gif_image.n_frames + 1):
with pytest.raises(ValueError):
image_it.seek(value)
with pytest.raises(TermImageError):
image_it.seek(1)
def test_seek(self):
for cached in (False, True):
image_it = ImageIterator(gif_image, 2, cached=cached)
frame_0 = next(image_it)
for _ in range(5):
next(image_it)
frame_6 = next(image_it)
# Image seek position is always the number of the last yielded frame. Hence:
# - must remain unchanged after `seek()`.
# - must equal the seek-ed position after the `next()` immediately following
# `seek()`.
# 1st loop
image_it.seek(0)
assert image_it._loop_no == 2
assert gif_image.tell() == 6
assert frame_0 == next(image_it)
assert image_it._loop_no == 2
assert gif_image.tell() == 0
image_it.seek(6)
assert image_it._loop_no == 2
assert gif_image.tell() == 0
assert frame_6 == next(image_it)
assert image_it._loop_no == 2
assert gif_image.tell() == 6
image_it.seek(gif_image.n_frames - 1)
assert image_it._loop_no == 2
assert gif_image.tell() == 6
next(image_it)
assert image_it._loop_no == 2
assert gif_image.tell() == gif_image.n_frames - 1
# 2nd loop (cached when `cached=True`)
assert frame_0 == next(image_it)
assert image_it._loop_no == 1
assert gif_image.tell() == 0
image_it.seek(6)
assert image_it._loop_no == 1
assert gif_image.tell() == 0
assert frame_6 == next(image_it)
assert image_it._loop_no == 1
assert gif_image.tell() == 6
image_it.seek(0)
assert image_it._loop_no == 1
assert gif_image.tell() == 6
assert frame_0 == next(image_it)
assert image_it._loop_no == 1
assert gif_image.tell() == 0
image_it.seek(gif_image.n_frames - 1)
assert image_it._loop_no == 1
assert gif_image.tell() == 0
next(image_it)
assert image_it._loop_no == 1
assert gif_image.tell() == gif_image.n_frames - 1
# Iteration ends normally
with pytest.raises(StopIteration):
next(image_it)
# After closing
with pytest.raises(TermImageError):
image_it.seek(0)
def test_seek_skipped_frames_caching(self):
image_it = ImageIterator(gif_image, 1)
next(image_it)
frame_1 = next(image_it)
for _ in range(4):
next(image_it)
frame_6 = next(image_it)
image_it = ImageIterator(gif_image, 2, cached=True)
next(image_it)
image_it.seek(gif_image.n_frames - 1)
next(image_it) and next(image_it)
cache = image_it._animator.gi_frame.f_locals["cache"]
assert frame_1 == next(image_it) is cache[1][0]
image_it.seek(6)
assert frame_6 == next(image_it) is cache[6][0]
|