@@ -4583,26 +4583,59 @@ def copy_replace(self, sourceImage, *, from_coords=None, to=None, shrink=False,
45834583 options .extend (('-compositingrule' , compositingrule ))
45844584 self .tk .call (self .name , 'copy' , sourceImage , * options )
45854585
4586- def get (self , x , y ):
4587- """Return the color (red, green, blue) of the pixel at X,Y."""
4588- return self .tk .call (self .name , 'get' , x , y )
4586+ @staticmethod
4587+ def _metadata (metadata ):
4588+ # A Tcl dict is a flat list of alternating keys and values. A Python
4589+ # dict passed directly would expand to its keys only, so flatten it.
4590+ flat = ()
4591+ for key , value in metadata .items ():
4592+ flat += (key , value )
4593+ return flat
4594+
4595+ def get (self , x , y , * , withalpha = False ):
4596+ """Return the color of the pixel at X,Y as a tuple of its red, green
4597+ and blue components.
4598+
4599+ If WITHALPHA is true, the returned tuple has a fourth element giving
4600+ the alpha (opacity) value of the pixel. This requires Tcl/Tk 9.0 or
4601+ newer.
4602+ """
4603+ args = (self .name , 'get' , x , y )
4604+ if withalpha :
4605+ args += ('-withalpha' ,)
4606+ return self .tk .call (args )
45894607
4590- def put (self , data , to = None ):
4608+ def put (self , data , to = None , * , format = None , metadata = None ):
45914609 """Put row formatted colors to image starting from
4592- position TO, e.g. image.put("{red green} {blue yellow}", to=(4,6))"""
4610+ position TO, e.g. image.put("{red green} {blue yellow}", to=(4,6))
4611+
4612+ The FORMAT option specifies the format of the image DATA, so that only
4613+ image file format handlers whose names begin with it are tried.
4614+
4615+ The METADATA option, a dictionary passed to the image format driver,
4616+ requires Tcl/Tk 9.0 or newer.
4617+ """
45934618 args = (self .name , 'put' , data )
4619+ if format is not None :
4620+ args += ('-format' , format )
4621+ if metadata is not None :
4622+ args += ('-metadata' , self ._metadata (metadata ))
45944623 if to :
45954624 if to [0 ] == '-to' :
45964625 to = to [1 :]
4597- args = args + ('-to' ,) + tuple (to )
4626+ args += ('-to' ,) + tuple (to )
45984627 self .tk .call (args )
45994628
4600- def read (self , filename , format = None , * , from_coords = None , to = None , shrink = False ):
4629+ def read (self , filename , format = None , * , from_coords = None , to = None ,
4630+ shrink = False , metadata = None ):
46014631 """Reads image data from the file named FILENAME into the image.
46024632
46034633 The FORMAT option specifies the format of the image data in the
46044634 file.
46054635
4636+ The METADATA option, a dictionary passed to the image format driver,
4637+ requires Tcl/Tk 9.0 or newer.
4638+
46064639 The FROM_COORDS option specifies a rectangular sub-region of the image
46074640 file data to be copied to the destination image. It must be a tuple
46084641 or a list of 1 to 4 integers (x1, y1, x2, y2). (x1, y1) and
@@ -4622,6 +4655,8 @@ def read(self, filename, format=None, *, from_coords=None, to=None, shrink=False
46224655 options = ()
46234656 if format is not None :
46244657 options += ('-format' , format )
4658+ if metadata is not None :
4659+ options += ('-metadata' , self ._metadata (metadata ))
46254660 if from_coords is not None :
46264661 options += ('-from' , * from_coords )
46274662 if shrink :
@@ -4631,13 +4666,16 @@ def read(self, filename, format=None, *, from_coords=None, to=None, shrink=False
46314666 self .tk .call (self .name , 'read' , filename , * options )
46324667
46334668 def write (self , filename , format = None , from_coords = None , * ,
4634- background = None , grayscale = False ):
4669+ background = None , grayscale = False , metadata = None ):
46354670 """Writes image data from the image to a file named FILENAME.
46364671
46374672 The FORMAT option specifies the name of the image file format
46384673 handler to be used to write the data to the file. If this option
46394674 is not given, the format is guessed from the file extension.
46404675
4676+ The METADATA option, a dictionary passed to the image format driver,
4677+ requires Tcl/Tk 9.0 or newer.
4678+
46414679 The FROM_COORDS option specifies a rectangular region of the image
46424680 to be written to the image file. It must be a tuple or a list of 1
46434681 to 4 integers (x1, y1, x2, y2). If only x1 and y1 are specified,
@@ -4656,6 +4694,8 @@ def write(self, filename, format=None, from_coords=None, *,
46564694 options = ()
46574695 if format is not None :
46584696 options += ('-format' , format )
4697+ if metadata is not None :
4698+ options += ('-metadata' , self ._metadata (metadata ))
46594699 if from_coords is not None :
46604700 options += ('-from' , * from_coords )
46614701 if grayscale :
@@ -4665,9 +4705,12 @@ def write(self, filename, format=None, from_coords=None, *,
46654705 self .tk .call (self .name , 'write' , filename , * options )
46664706
46674707 def data (self , format = None , * , from_coords = None ,
4668- background = None , grayscale = False ):
4708+ background = None , grayscale = False , metadata = None ):
46694709 """Returns image data.
46704710
4711+ The METADATA option, a dictionary passed to the image format driver,
4712+ requires Tcl/Tk 9.0 or newer.
4713+
46714714 The FORMAT option specifies the name of the image file format
46724715 handler to be used. If this option is not given, this method uses
46734716 a format that consists of a tuple (one element per row) of strings
@@ -4694,6 +4737,8 @@ def data(self, format=None, *, from_coords=None,
46944737 options = ()
46954738 if format is not None :
46964739 options += ('-format' , format )
4740+ if metadata is not None :
4741+ options += ('-metadata' , self ._metadata (metadata ))
46974742 if from_coords is not None :
46984743 options += ('-from' , * from_coords )
46994744 if grayscale :
0 commit comments