Fix modified flag behavior

This commit is contained in:
Tony S Yu
2013-07-03 00:19:07 -05:00
parent c881cf32e6
commit 896e471a12
2 changed files with 33 additions and 9 deletions
+9 -9
View File
@@ -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
+24
View File
@@ -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)