1

画像を表示するGuiと、白黒、ミラーなどのいくつかのボタンがあります。関数
を呼び出す元に戻すボタンを実装する必要があります。これは、明らかに、画像に対して行われた最後のアクションを元に戻します。画像に対して行ったすべてのアクションを保存するには、変数 - (空のリスト) を使用する必要があります。これをどのように行うのかわかりません。方向性を教えていただければ幸いです。 コードの一部:undo
globalhistory=[]

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()
4

1 に答える 1

1

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.

于 2013-01-09T02:30:02.467 に答える