撤消Gui中的图像 [英] Undo in Gui for images
问题描述
我有显示图像的Gui和黑白,镜子等的几个按钮.
我需要实现一个调用undo
函数的撤消按钮,显然,该按钮撤消对图像执行的最后一个操作.
我需要使用global
变量-history=[]
(空列表)来保存在图像上进行的所有操作.
我不知道如何执行此操作,很高兴获得指导.
代码的一部分:
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()
推荐答案
这取决于您要应用的函数集,如果它们是可逆的,那么它将变得非常简单.
It depends on the set of functions you are applying, if they are invertible then it gets very simple.
从mirror
函数开始,该函数实际上沿y轴翻转.如果要撤消该操作,只需再次沿y轴翻转.否定图像的工作方式相同.要进行取反,您需要知道图像类型(例如255)所接受的最大值,因此您只需执行255-image即可.要还原该图像,请再次应用255-图像.因此,在这种情况下,您的history
可以很简单地是所应用的功能(因此,您只需调用它们即可撤消操作).
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).
现在考虑旋转90度.您不能通过旋转90度来撤消它,而是需要进行-90.通过此新操作,很明显需要修改history
.实际上,您实际上想存储执行的操作列表以及转换的逆过程(并且您只会向用户显示第一个信息).
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).
让我们再增加一点问题.现在,您可以旋转任意角度,这需要使用插值方法.通常,您不能通过简单地否定度数来通过旋转来撤消它,因此在应用初始旋转之前,所得图像将是不同的.在应用N个任意旋转然后尝试撤消它们之后,情况会变得更糟,离散网格不允许进行如此完美的撤消.还有一些操作不能简单地撤消,与处于离散域还是连续域无关(侵蚀是一个简单的例子).此时,您需要区分可逆操作和不可逆操作,这实际上很容易做到.然后的问题是,当用户执行不可逆操作时,最简单的操作是在修改当前映像之前将其保存(在内存中或在磁盘上).如果您有足够的内存/磁盘可用,则完全不保存图像是没有问题的.
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.
这篇关于撤消Gui中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!