Undo in Gui for images

2019-08-07 14:56发布

I have Gui that show image and few buttons for B&W, mirror etc.
I need to implement a undo button that call to undo function, which, obiviously, undo the last action that was made on the image.
I need to use a global variable - history=[] (empty list) to save all the actions that made on the image. I don't have an idea how do this, I be happy to get a direction.
part of the code:

def mirror():
    '''Flips the image like a mirror does, left to right'''
    global img
    out = Image.new('L',img.size, 'white')
    out=flip(img)
    img = out  
    display() 

def negate():
    '''Negate the image pixels'''
    global img
    out = Image.new('L',img.size, 'white')
    out=negate_pxls(img)
    img = out  
    display()

1条回答
我想做一个坏孩纸
2楼-- · 2019-08-07 15:11

It depends on the set of functions you are applying, if they are invertible then it gets very simple.

Start with your mirror function which actually flips along the y axis. If you want to undo that operation, simply flip along the y axis again. Negating an image works the same way. To do the negation, you know the maximum value accepted by your image type (255 for example), so you simply perform 255 - image. To revert that, you again apply 255 - image. So, in these situations, your history could simple be the functions that were applied (so you just simply call them to undo an operation).

Now consider a rotation by 90 degrees. You cannot undo it by rotating more 90 degrees, instead you need to go for -90. With this new operation, it is clear that your history needs to be modified. You actually want to store the list of operations performed along with the inverse of the transformation (and you would display just the first information to the user).

Let's increment the problem a bit more. You can now rotate by an arbitrary degree, which requires the use of interpolation methods. You cannot undo it in general by rotation by simply negating the degree, the resulting image will not be the same before applying the initial rotation. It gets worse after you apply N arbitrary rotations and then try to undo them, the discrete grid doesn't allow for such perfect undoing. There are also operations that cannot simply be undone, irrelevant of being in the discrete or continuous domain (erosion is a simple example for that). At this point you need to distinguish between invertible and non-invertible operations, which is an easy thing to do actually. The problem is then that when the user perform non-invertible operations, the easiest thing to do is saving the current image before modifying it (either in memory, or to the disk). If you have enough memory/disk available, fully saving an image is a non-issue.

查看更多
登录 后发表回答