numpyimage 2.5.0__tar.gz → 2.6.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: numpyimage
3
- Version: 2.5.0
3
+ Version: 2.6.0
4
4
  Summary: Load, save, & manipulate image files as numpy arrays
5
5
  Author-email: Jasper Phelps <jasper.s.phelps@gmail.com>
6
6
  License: GNU GENERAL PUBLIC LICENSE
@@ -409,8 +409,10 @@ def load_video(filename,
409
409
  data = np.empty((num_frames, *first_img.shape), dtype=first_img.dtype)
410
410
  # Then fill it up frame by frame
411
411
  data[0] = first_img
412
- for i, frame in tqdm(enumerate(frame_iter, start=1), total=num_frames,
412
+ for i, frame in tqdm(enumerate(frame_iter), total=num_frames,
413
413
  desc='Loading video', disable=not progress_bar):
414
+ if i == 0:
415
+ continue
414
416
  img = frame.to_ndarray(format='rgb24')
415
417
  data[i] = img
416
418
  if return_framerate:
@@ -459,8 +461,7 @@ codec_aliases = {
459
461
  }
460
462
 
461
463
 
462
- def video_writer(filename, framerate=30, crf=23, compression_speed='medium',
463
- codec: Literal['libx264', 'libx265'] = 'libx264') -> 'VideoWriter':
464
+ class VideoWriter:
464
465
  """
465
466
  Create a video writer object for saving frames to a video file.
466
467
 
@@ -478,59 +479,57 @@ def video_writer(filename, framerate=30, crf=23, compression_speed='medium',
478
479
  codec : Literal['libx264', 'libx265'], default 'libx264'
479
480
  The video codec to use for encoding. Can be any of a number of aliases for
480
481
  these two codecs, including avc1/h264 vs hevc/hvc1/hev1/h265.
481
-
482
- Returns
483
- -------
484
- VideoWriter
485
- The video writer object supporting context manager protocol.
486
482
  """
487
- try:
488
- import av
489
- except ImportError:
490
- raise ImportError('Missing optional dependency for video processing,'
491
- ' run `pip install av tqdm`')
492
-
493
- class VideoWriter:
494
- def __init__(self, filename, framerate, crf, compression_speed, codec):
495
- self.filename = filename
496
- self.framerate = framerate
497
- self.crf = crf
498
- self.compression_speed = compression_speed
499
- self.codec = codec_aliases[codec]
500
- self.container = av.open(filename, mode='w')
501
- self.stream = self.container.add_stream(codec, rate=framerate)
502
- self.stream.pix_fmt = 'yuv420p'
503
- self.stream.options = {'crf': str(crf), 'preset': compression_speed}
504
- self._closed = False
505
- self.stream.width = 0
506
- self.stream.height = 0
507
-
508
- def write(self, frame):
509
- if not isinstance(frame, av.VideoFrame):
510
- if frame.ndim == 3 and frame.shape[-1] == 3:
511
- frame = av.VideoFrame.from_ndarray(frame, format='rgb24')
512
- elif frame.ndim == 2:
513
- frame = av.VideoFrame.from_ndarray(frame, format='gray')
514
- else:
515
- raise ValueError(f'Frame must be (H, W) or (H, W, 3) but was {frame.shape}')
516
- if self.stream.width == 0:
517
- self.stream.width = frame.width
518
- if self.stream.height == 0:
519
- self.stream.height = frame.height
520
- for packet in self.stream.encode(frame):
521
- self.container.mux(packet)
522
-
523
- def __enter__(self):
524
- return self
483
+ def __init__(self, filename, framerate=30, crf=23, compression_speed='medium',
484
+ codec: Literal['libx264', 'libx265'] = 'libx264',
485
+ overwrite=False):
486
+ try:
487
+ import av
488
+ except ImportError:
489
+ raise ImportError('Missing optional dependency for video processing,'
490
+ ' run `pip install av tqdm`')
491
+ self.av = av
492
+ filename = os.path.expanduser(str(filename))
493
+ if os.path.exists(filename) and not overwrite:
494
+ raise FileExistsError(f'File {filename} already exists. '
495
+ 'Set overwrite=True to overwrite.')
496
+ self.filename = filename
497
+ self.framerate = framerate
498
+ self.crf = crf
499
+ self.compression_speed = compression_speed
500
+ self.codec = codec_aliases[codec.lower()]
501
+ self.container = av.open(filename, mode='w')
502
+ self.stream = self.container.add_stream(self.codec, rate=framerate)
503
+ self.stream.pix_fmt = 'yuv420p'
504
+ self.stream.options = {'crf': str(crf), 'preset': compression_speed}
505
+ self._closed = False
506
+ self.stream.width = 0
507
+ self.stream.height = 0
508
+
509
+ def write(self, frame):
510
+ if not isinstance(frame, self.av.VideoFrame):
511
+ if frame.ndim == 3 and frame.shape[-1] == 3:
512
+ frame = self.av.VideoFrame.from_ndarray(frame, format='rgb24')
513
+ elif frame.ndim == 2:
514
+ frame = self.av.VideoFrame.from_ndarray(frame, format='gray')
515
+ else:
516
+ raise ValueError(f'Frame must be (H, W) or (H, W, 3) but was {frame.shape}')
517
+ if self.stream.width == 0:
518
+ self.stream.width = frame.width
519
+ if self.stream.height == 0:
520
+ self.stream.height = frame.height
521
+ for packet in self.stream.encode(frame):
522
+ self.container.mux(packet)
525
523
 
526
- def __exit__(self, exc_type, exc_val, exc_tb):
527
- # Flush stream
528
- for packet in self.stream.encode():
529
- self.container.mux(packet)
530
- self.container.close()
531
- self._closed = True
524
+ def __enter__(self):
525
+ return self
532
526
 
533
- return VideoWriter(filename, framerate, crf, compression_speed, codec)
527
+ def __exit__(self, exc_type, exc_val, exc_tb):
528
+ # Flush stream
529
+ for packet in self.stream.encode():
530
+ self.container.mux(packet)
531
+ self.container.close()
532
+ self._closed = True
534
533
 
535
534
 
536
535
  def save_video(data, filename, time_axis=0, color_axis=None, overwrite=False,
@@ -634,8 +633,9 @@ def save_video(data, filename, time_axis=0, color_axis=None, overwrite=False,
634
633
  if pad != [[0, 0], [0, 0], [0, 0]]:
635
634
  data = np.pad(data, pad, mode='edge')
636
635
 
637
- with video_writer(filename, framerate=framerate, crf=crf,
638
- compression_speed=compression_speed, codec=codec) as writer:
636
+ with VideoWriter(filename, framerate=framerate, crf=crf,
637
+ compression_speed=compression_speed, codec=codec,
638
+ overwrite=overwrite) as writer:
639
639
  for frame_i in tqdm(range(n_frames), total=n_frames,
640
640
  desc='Saving video', disable=not progress_bar):
641
641
  writer.write(data[frame_i])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: numpyimage
3
- Version: 2.5.0
3
+ Version: 2.6.0
4
4
  Summary: Load, save, & manipulate image files as numpy arrays
5
5
  Author-email: Jasper Phelps <jasper.s.phelps@gmail.com>
6
6
  License: GNU GENERAL PUBLIC LICENSE
@@ -4,7 +4,7 @@ build-backend = 'setuptools.build_meta'
4
4
 
5
5
  [project]
6
6
  name = 'numpyimage'
7
- version = '2.5.0'
7
+ version = '2.6.0'
8
8
  description = 'Load, save, & manipulate image files as numpy arrays'
9
9
  readme.file = 'README.md'
10
10
  readme.content-type = 'text/markdown'
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes