Add reset and compare to novice module

- Reset restores the image to the original.
- Compare displays the original and the modified image side-by-side.
This commit is contained in:
Stefan van der Walt
2016-03-02 16:22:24 -08:00
parent 3ffca2fdbd
commit a8057ed941
5 changed files with 36 additions and 4 deletions
+1 -1
View File
@@ -157,7 +157,7 @@ def imshow(arr, plugin=None, **plugin_args):
def imshow_collection(ic, plugin=None, **plugin_args):
"""Display an image.
"""Display a collection of images.
Parameters
----------
+1 -1
View File
@@ -171,7 +171,7 @@ def imshow_collection(ic, *args, **kwargs):
"""
fig, axes = plt.subplots(1, len(ic))
for n, image in enumerate(ic):
kwargs['axis'] = axes[n]
kwargs['ax'] = axes[n]
imshow(image, *args, **kwargs)
+9 -2
View File
@@ -56,9 +56,9 @@ Changing `size` resizes the picture.
>>> picture.size = (45, 30)
We can preview the changes we made to the picture with our earlier command:
We can preview the changes we made to the picture with the ``compare`` command:
>>> picture.show() # doctest: +SKIP
>>> picture.compare() # doctest: +SKIP
You can iterate over pixels, which have RGB values between 0 and 255,
and know their location in the picture.
@@ -88,6 +88,13 @@ True
>>> picture.modified
False
An image can also be restored to its original state after modification:
>>> picture[0:20, 0:20] = (0, 0, 0)
>>> picture.compare() # doctest: +SKIP
>>> picture.reset()
>>> picture.compare() # doctest: +SKIP
"""
from ._novice import Picture, open, colors, color_dict
+15
View File
@@ -7,6 +7,7 @@ from .. import io, img_as_ubyte
from ..transform import resize
from ..color import color_dict
from ..io.util import file_or_url_context, is_url
from ..io.collection import ImageCollection
import six
from six.moves.urllib import request
@@ -305,6 +306,7 @@ class Picture(object):
def array(self, array):
self._array = array
self._xy_array = array_to_xy_origin(array)
self._array_backup = self._array.copy()
@property
def xy_array(self):
@@ -329,6 +331,12 @@ class Picture(object):
self._path = os.path.abspath(path)
self._format = imghdr.what(path)
def reset(self):
"""Reset image to its original state, removing modifications.
"""
self.array = self._array_backup
@property
def path(self):
"""The path to the picture."""
@@ -388,6 +396,13 @@ class Picture(object):
io.imshow(self.array)
io.show()
def compare(self):
"""Compare the image to its unmodified version."""
images = [self._array_backup, self.array]
ic = ImageCollection([0, 1], load_func=lambda x: images[x])
io.imshow_collection(images)
io.show()
def _makepixel(self, x, y):
"""Create a Pixel object for a given x, y location."""
rgb = self.xy_array[x, y]
+10
View File
@@ -135,8 +135,18 @@ def test_modified_on_set_pixel():
assert pic.modified
def test_reset():
pic = novice.Picture(SMALL_IMAGE_PATH)
v = pic[0, 0]
pic[0, 0] = (1, 1, 1)
pic.reset()
assert_equal(pic[0, 0], v)
def test_update_on_save():
pic = novice.Picture(array=np.zeros((3, 3, 3)))
pic[0, 0] = (255, 255, 255) # prevent attempting to save low-contrast image
with all_warnings(): # precision loss
pic.size = (6, 6)
assert pic.modified