如何“取消提款"?使用PIL的图像中的一个盒子 [英] How to "undraw" a box in an image using PIL

查看:89
本文介绍了如何“取消提款"?使用PIL的图像中的一个盒子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个函数,可以使用PIL中的ImageDraw.Draw.rectangle方法在用户单击图像的位置周围绘制一个小方框.它很好用,但是问题是,我需要这种能力,以便当用户在新框中单击时,它应该被删除.但是,由于盒子成为图像的一部分,所以我不能这样做.

Essentially, I have a function that draws a small box around where the user clicks on an image by using the ImageDraw.Draw.rectangle method in PIL. It works great, but the problem is, I need the ability so that when the user clicks inside the new box, it should get deleted. However, since the box becomes part of the image, I can't do this.

有没有解决的办法?在PIL之外是否有一个库可以使此操作更容易?

Are there any ways around this? Is there a library outside of PIL that would make this easier?

谢谢!

推荐答案

执行此操作的一种方法是保留基本的原始背景图像的原始,未注释的副本,并保留注释列表(这就是我的意思)调用您提到的方块),就像这样:

One way to do this is to keep a pristine, un-annotated copy of your basic, background image and to keep a list of annotations ( that's what I am calling the squares you mention), like this:

  • 正方形,黑色,10、10、20、20
  • 正方形,红色,100,110、200,210
  • 圆形,绿色,50、50、25
  • 矩形,蓝色,30,30、40,300

然后,每次重新绘制屏幕时,请复制背景图像,并遍历绘制每个图像的注释列表.

Then each time you re-paint the screen, take a copy of your background image and loop through the list of annotations drawing each one on.

要删除时,请遍历列表并删除中心最近的条目,然后重新绘制屏幕.

When it comes time to delete, go through the list and delete the entry with the nearest centre, and re-paint your screen.

您可以在评论中将此方法与@ Mika72的建议结合使用.这样一来,您将获得原始的原始图像以及用于覆盖的单独图像,该图像的大小相同,并且开始时是一块空的纯透明画布.用户单击时,将注释(正方形和矩形)绘制到该层上,并且每次重新绘制屏幕时,都将注释层合成到原始图像上.现在,您只需要遍历注释列表,并在删除注释时重新绘制它们,因此重新绘制将更快.

You could combine this approach with @Mika72's suggestion in the comments. So you would have your original, clean image and a separate image for your overlays which is the same size and starts out as an empty purely transparent canvas. Draw your annotations (squares and rectangles) onto this layer as the user clicks and each time you re-paint your screen, composite the annotation layer over the original image. Now you only have to iterate through the list of annotations and re-draw them when one is deleted, so re-paints will be faster.

另一种选择是通过XOR绘图进行橡皮筋" ,请参见答案此处

Another option is "rubber banding" by drawing with an XOR, see answer here.

这篇关于如何“取消提款"?使用PIL的图像中的一个盒子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆