From 896e471a123567efa2337bfb10d0f2c0e20a4ade Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Wed, 3 Jul 2013 00:19:07 -0500 Subject: [PATCH] Fix `modified` flag behavior --- skimage/novice/novice.py | 18 +++++++++--------- skimage/novice/tests/test_novice.py | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/skimage/novice/novice.py b/skimage/novice/novice.py index 5757002a..37eda63c 100644 --- a/skimage/novice/novice.py +++ b/skimage/novice/novice.py @@ -111,13 +111,9 @@ class Pixel(object): NOTE: Using Cartesian coordinate system! """ - # skimage dimensions are flipped: y, x - self._image[self._picture.height - self._y - 1, self._x] = \ - (self.red, self.green, self.blue) - - # Modified pictures lose their paths - self._picture._picture_path = None - self._picture._picture_modified = True + row = self._picture.height - self._y - 1 + self._picture._image[row, self._x] = self.rgb + self._picture._image_modified() def __repr__(self): args = self.red, self.green, self.blue @@ -407,6 +403,10 @@ class Picture(object): """True if the picture has changed.""" return self._modified + def _image_modified(self): + self._modified = True + self._path = None + @property def format(self): """The image format of the picture.""" @@ -427,8 +427,7 @@ class Picture(object): self._image = img_as_ubyte(resize(self._image, (int(value[1]), int(value[0])), order=0)) - self._modified = True - self._path = None + self._image_modified() except TypeError: msg = "Expected (width, height), but got {0} instead!" raise TypeError(msg.format(value)) @@ -519,6 +518,7 @@ class Picture(object): raise TypeError("Invalid value type") else: raise TypeError("Invalid key type") + self._image_modified() def __repr__(self): args = self.format, self.path, self.modified diff --git a/skimage/novice/tests/test_novice.py b/skimage/novice/tests/test_novice.py index f039d9b2..19a2d34c 100644 --- a/skimage/novice/tests/test_novice.py +++ b/skimage/novice/tests/test_novice.py @@ -1,6 +1,7 @@ import os import tempfile +import numpy as np from numpy.testing import assert_equal, assert_raises from skimage import novice from skimage import data_dir @@ -50,6 +51,29 @@ def test_modify(): assert_equal(pic.modified, True) assert_equal(pic.path, None) + +def test_modified_on_set(): + pic = novice.Picture(SMALL_IMAGE_PATH) + pic[0, 0] = (1, 1, 1) + assert_equal(pic.modified, True) + assert_equal(pic.path, None) + + +def test_modified_on_set_pixel(): + data = np.zeros(shape=(10, 5, 3), dtype=np.uint8) + pic = novice.Picture(image=data) + + pixel = pic[0, 0] + pixel.green = 1 + assert_equal(pic.modified, True) + + +def test_update_on_save(): + pic = novice.Picture(image=np.zeros((3, 3))) + pic.size = (6, 6) + assert_equal(pic.modified, True) + assert_equal(pic.path, None) + with tempfile.NamedTemporaryFile(suffix=".jpg") as tmp: pic.save(tmp.name)