Skip to content

Commit

Permalink
Add Image.orientation property
Browse files Browse the repository at this point in the history
  • Loading branch information
dittos committed Apr 25, 2013
1 parent 9935d40 commit 8857446
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions wand/api.py
Expand Up @@ -272,6 +272,11 @@ class MagickPixelPacket(ctypes.Structure):
library.MagickGetImageHeight.argtypes = [ctypes.c_void_p]
library.MagickGetImageHeight.restype = ctypes.c_size_t

library.MagickGetImageOrientation.argtypes = [ctypes.c_void_p]
library.MagickGetImageOrientation.restype = ctypes.c_int

library.MagickSetImageOrientation.argtypes = [ctypes.c_void_p, ctypes.c_int]

library.MagickGetImageUnits.argtypes = [ctypes.c_void_p]

library.MagickSetImageUnits.argtypes = [ctypes.c_void_p, ctypes.c_int]
Expand Down
26 changes: 25 additions & 1 deletion wand/image.py
Expand Up @@ -25,7 +25,7 @@

__all__ = ('ALPHA_CHANNEL_TYPES', 'CHANNELS', 'COMPOSITE_OPERATORS',
'EVALUATE_OPS', 'FILTER_TYPES', 'GRAVITY_TYPES', 'IMAGE_TYPES',
'UNIT_TYPES',
'ORIENTATION_TYPES', 'UNIT_TYPES',
'ChannelDepthDict', 'ChannelImageDict', 'ClosedImageError',
'Image', 'ImageProperty', 'Iterator', 'Metadata', 'OptionDict')

Expand Down Expand Up @@ -326,6 +326,13 @@
'center', 'east', 'south_west', 'south', 'south_east',
'static')

#: (:class:`tuple`) The list of :attr:`~Image.orientation` types.
#:
#: .. versionadded:: 0.3.0
ORIENTATION_TYPES = ('undefined', 'top_left', 'top_right', 'bottom_right',
'bottom_left', 'left_top', 'right_top', 'right_bottom',
'left_bottom')

#: (:class:`collections.Set`) The set of available :attr:`~Image.options`.
#:
#: .. versionadded:: 0.3.0
Expand Down Expand Up @@ -778,6 +785,23 @@ def height(self, height):
raise TypeError('height must be a integral, not ' + repr(height))
library.MagickSetSize(self.wand, self.width, height)

@property
def orientation(self):
"""(:class:`basestring`) The image orientation. It's a string from
:const:`ORIENTATION_TYPES` list. It also can be set."""
orientation_index = library.MagickGetImageOrientation(self.wand)
return ORIENTATION_TYPES[orientation_index]

@orientation.setter
def orientation(self, value):
if not isinstance(value, basestring):
raise TypeError('expected a string, not ' + repr(value))
elif value not in ORIENTATION_TYPES:
raise ValueError('expected a string from ORIENTATION_TYPES, not '
+ repr(value))
index = ORIENTATION_TYPES.index(value)
library.MagickSetImageOrientation(self.wand, index)

@property
def font_color(self):
return Color(self.options['fill'])
Expand Down
16 changes: 16 additions & 0 deletions wandtests/image.py
Expand Up @@ -1147,3 +1147,19 @@ def flop():
assert flopped[-1, 0] == img[0, 0]
assert flopped[0, -1] == img[-1, -1]
assert flopped[-1, -1] == img[0, -1]


@tests.test
def get_orientation():
with Image(filename=asset('sasha.jpg')) as img:
assert img.orientation == 'undefined'

with Image(filename=asset('beach.jpg')) as img:
assert img.orientation == 'top_left'


@tests.test
def set_orientation():
with Image(filename=asset('beach.jpg')) as img:
img.orientation = 'bottom_right'
assert img.orientation == 'bottom_right'

0 comments on commit 8857446

Please sign in to comment.