Right now, the PhotoImage copy method looks like this:
# XXX copy -from, -to, ...?
def copy(self):
"""Return a new PhotoImage with the same image as this widget."""
destImage = PhotoImage(master=self.tk)
self.tk.call(destImage, 'copy', self.name)
return destImage
But with the all tk’s options, it will be possible to crop and scale images (and more).
Like this:
class MyPhotoImage(PhotoImage):
def __init__(self, name=None, cnf={}, master=None, **kw):
super().__init__(name, cnf, master, **kw)
def copy(self, dest=None, from_coords=None, to=None, shrink=None,
zoom=None, subsample=None, compositingrule=None):
"""Copies a region from the PhotoImage to the DEST
(which must be a PhotoImage), possibly with pixel
zooming and/or subsampling. If no options are specified, this
command returns a new PhotoImage with the same image
as this widget."""
destImage = PhotoImage(master=self.tk) if not dest else dest
args = (destImage, 'copy', self.name,)
if from_coords:
args = args + ('-from',) + tuple(from_coords)
if to:
args = args + ('-to',) + tuple(to)
if shrink:
args = args + ('-shrink',)
if zoom:
args = args + ('-zoom',) + tuple(zoom)
if subsample:
args = args + ('-subsample',) + tuple(subsample)
if compositingrule:
args = args + ('-compositingrule',) + tuple(compositingrule)
self.tk.call(args)
return destImage
tiles = MyPhotoImage(file="platformer_tiles.png")
tile = tiles.copy(zoom=(2, 2), from_coords=(0, 32, 32, 64))
cv.create_image(0, 0, image = tile, anchor=NW)