Skip to content

Commit

Permalink
Add padding to PiCameraResolution
Browse files Browse the repository at this point in the history
  • Loading branch information
waveform80 committed Oct 13, 2016
1 parent 83a45a9 commit f875d92
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/api_camera.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ PiVideoFrame

.. autoclass:: PiVideoFrame(index, frame_type, frame_size, video_size, split_size, timestamp)


PiCameraResolution
==================

.. autoclass:: PiCameraResolution(width, height)
1 change: 1 addition & 0 deletions picamera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
PiCameraMMALError,
mmal_check,
)
from picamera.mmalobj import PiCameraResolution
from picamera.camera import PiCamera
from picamera.frames import PiVideoFrame, PiVideoFrameType
from picamera.encoders import (
Expand Down
2 changes: 2 additions & 0 deletions picamera/frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ class PiVideoFrame(namedtuple('PiVideoFrame', (
Added the :attr:`complete` attribute.
"""

__slots__ = () # workaround python issue #24931

@property
def position(self):
"""
Expand Down
33 changes: 33 additions & 0 deletions picamera/mmalobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,40 @@ class PiCameraResolution(namedtuple('PiCameraResolution', ('width', 'height'))):
"""
A :func:`~collections.namedtuple` derivative which represents a resolution
with a :attr:`width` and :attr:`height`.
.. attribute:: width
The width of the resolution in pixels
.. attribute:: height
The height of the resolution in pixels
.. versionadded:: 1.11
"""

__slots__ = () # workaround python issue #24931

def pad(self, width=32, height=16):
"""
Returns the resolution padded up to the nearest multiple of *width*
and *height* which default to 32 and 16 respectively (the camera's
native block size for most operations). For example:
.. code-block:: pycon
>>> PiCameraResolution(1920, 1080).pad()
PiCameraResolution(width=1920, height=1088)
>>> PiCameraResolution(100, 100).pad(16, 16)
PiCameraResolution(width=128, height=112)
>>> PiCameraResolution(100, 100).pad(16, 16)
PiCameraResolution(width=112, height=112)
"""
return PiCameraResolution(
width=((self.width + (width - 1)) // width) * width,
height=((self.height + (height - 1)) // height) * height,
)

def __str__(self):
return '%dx%d' % (self.width, self.height)

Expand Down

0 comments on commit f875d92

Please sign in to comment.