0

I'm trying to use the paste(im, box=(4-tuple)) method to update a shown image in a Label created in Tkinter(python) window.

Following code is a minimal example showing what I'm doing. The background is the existing image and the mask is the image to be pasted onto the existing image. By default, the mask will be update at the top left corner of the existing image.

By adding box argument to the paste method, I hope to shift the mask to a different location. In this instance I tried box=(40,40,90,90) for the box argument.

from Tkinter import *
import Image, ImageTk

root = Tk()

background_Image = Image.new("RGB",(500,500), (255,0,255,255))
background_Tk = ImageTk.PhotoImage(background_Image)
mask = Image.new('RGBX', (50,50), color=(255,255,255,0))

p1 = Label(root, image=background_Tk)
p1.pack()

background_Tk.paste(mask, box=(40,40,90,90))

root.mainloop()

The image is updated but not at the location I desired. It's still appearing on top-left corner of the background. After trying a lot of possible combinations of coordinates for box argument, I still can't change the location of mask. In this case I don't have an exact location for the mask to show up, i just want to move it since I can't find a working example on the internet. I would like to humbly ask what is the correct expression for the box argument? Can you show me a working example?

For your convenience, I copy the documentation for paste method here:

paste(im, box=None)

Paste a PIL image into the photo image. Note that this can be very slow if the photo image is displayed.

Parameters:
im – A PIL image. The size must match the target region. If the mode does not match, the image is converted to the mode of the bitmap image.

box – A 4-tuple defining the left, upper, right, and lower pixel coordinate. If None is given instead of a tuple, all of the image is assumed.

Community
  • 1
  • 1
shi
  • 286
  • 1
  • 9
  • 1
    It might be that "box" defines the portion of the image you are copying _from_, not _to_. I've not used the `paste` function myself, but that seems to explain what you're seeing. Try making the box smaller than the image you are copying from to see if it copies only a subset of the image. – Bryan Oakley Oct 08 '14 at 15:59

1 Answers1

0

The box argument to PhotoImage.paste seems to be ignored altogether. You'll need to supply a complete image:

from tkinter import *
from PIL import Image, ImageTk

root = Tk()

background_Image = Image.new("RGB",(500,500), (255,0,255,255))
background_Tk = ImageTk.PhotoImage(background_Image)
mask = Image.new('RGBX', (50,50), color=(255,255,255,0))

p1 = Label(root, image=background_Tk)
p1.pack()

# ...later
background_Image.paste(mask, box=(40,40,90,90))
background_Tk.paste(background_Image)

root.mainloop()
Oblivion
  • 1,669
  • 14
  • 14