@@ -4692,26 +4692,59 @@ def copy_replace(self, sourceImage, *, from_coords=None, to=None, shrink=False,
46924692 options .extend (('-compositingrule' , compositingrule ))
46934693 self .tk .call (self .name , 'copy' , sourceImage , * options )
46944694
4695- def get (self , x , y ):
4696- """Return the color (red, green, blue) of the pixel at X,Y."""
4697- return self .tk .call (self .name , 'get' , x , y )
4695+ @staticmethod
4696+ def _metadata (metadata ):
4697+ # A Tcl dict is a flat list of alternating keys and values. A Python
4698+ # dict passed directly would expand to its keys only, so flatten it.
4699+ flat = ()
4700+ for key , value in metadata .items ():
4701+ flat += (key , value )
4702+ return flat
4703+
4704+ def get (self , x , y , * , withalpha = False ):
4705+ """Return the color of the pixel at X,Y as a tuple of its red, green
4706+ and blue components.
4707+
4708+ If WITHALPHA is true, the returned tuple has a fourth element giving
4709+ the alpha (opacity) value of the pixel. This requires Tcl/Tk 9.0 or
4710+ newer.
4711+ """
4712+ args = (self .name , 'get' , x , y )
4713+ if withalpha :
4714+ args += ('-withalpha' ,)
4715+ return self .tk .call (args )
46984716
4699- def put (self , data , to = None ):
4717+ def put (self , data , to = None , * , format = None , metadata = None ):
47004718 """Put row formatted colors to image starting from
4701- position TO, e.g. image.put("{red green} {blue yellow}", to=(4,6))"""
4719+ position TO, e.g. image.put("{red green} {blue yellow}", to=(4,6))
4720+
4721+ The FORMAT option specifies the format of the image DATA, so that only
4722+ image file format handlers whose names begin with it are tried.
4723+
4724+ The METADATA option, a dictionary passed to the image format driver,
4725+ requires Tcl/Tk 9.0 or newer.
4726+ """
47024727 args = (self .name , 'put' , data )
4728+ if format is not None :
4729+ args += ('-format' , format )
4730+ if metadata is not None :
4731+ args += ('-metadata' , self ._metadata (metadata ))
47034732 if to :
47044733 if to [0 ] == '-to' :
47054734 to = to [1 :]
4706- args = args + ('-to' ,) + tuple (to )
4735+ args += ('-to' ,) + tuple (to )
47074736 self .tk .call (args )
47084737
4709- def read (self , filename , format = None , * , from_coords = None , to = None , shrink = False ):
4738+ def read (self , filename , format = None , * , from_coords = None , to = None ,
4739+ shrink = False , metadata = None ):
47104740 """Reads image data from the file named FILENAME into the image.
47114741
47124742 The FORMAT option specifies the format of the image data in the
47134743 file.
47144744
4745+ The METADATA option, a dictionary passed to the image format driver,
4746+ requires Tcl/Tk 9.0 or newer.
4747+
47154748 The FROM_COORDS option specifies a rectangular sub-region of the image
47164749 file data to be copied to the destination image. It must be a tuple
47174750 or a list of 1 to 4 integers (x1, y1, x2, y2). (x1, y1) and
@@ -4731,6 +4764,8 @@ def read(self, filename, format=None, *, from_coords=None, to=None, shrink=False
47314764 options = ()
47324765 if format is not None :
47334766 options += ('-format' , format )
4767+ if metadata is not None :
4768+ options += ('-metadata' , self ._metadata (metadata ))
47344769 if from_coords is not None :
47354770 options += ('-from' , * from_coords )
47364771 if shrink :
@@ -4740,13 +4775,16 @@ def read(self, filename, format=None, *, from_coords=None, to=None, shrink=False
47404775 self .tk .call (self .name , 'read' , filename , * options )
47414776
47424777 def write (self , filename , format = None , from_coords = None , * ,
4743- background = None , grayscale = False ):
4778+ background = None , grayscale = False , metadata = None ):
47444779 """Writes image data from the image to a file named FILENAME.
47454780
47464781 The FORMAT option specifies the name of the image file format
47474782 handler to be used to write the data to the file. If this option
47484783 is not given, the format is guessed from the file extension.
47494784
4785+ The METADATA option, a dictionary passed to the image format driver,
4786+ requires Tcl/Tk 9.0 or newer.
4787+
47504788 The FROM_COORDS option specifies a rectangular region of the image
47514789 to be written to the image file. It must be a tuple or a list of 1
47524790 to 4 integers (x1, y1, x2, y2). If only x1 and y1 are specified,
@@ -4765,6 +4803,8 @@ def write(self, filename, format=None, from_coords=None, *,
47654803 options = ()
47664804 if format is not None :
47674805 options += ('-format' , format )
4806+ if metadata is not None :
4807+ options += ('-metadata' , self ._metadata (metadata ))
47684808 if from_coords is not None :
47694809 options += ('-from' , * from_coords )
47704810 if grayscale :
@@ -4774,9 +4814,12 @@ def write(self, filename, format=None, from_coords=None, *,
47744814 self .tk .call (self .name , 'write' , filename , * options )
47754815
47764816 def data (self , format = None , * , from_coords = None ,
4777- background = None , grayscale = False ):
4817+ background = None , grayscale = False , metadata = None ):
47784818 """Returns image data.
47794819
4820+ The METADATA option, a dictionary passed to the image format driver,
4821+ requires Tcl/Tk 9.0 or newer.
4822+
47804823 The FORMAT option specifies the name of the image file format
47814824 handler to be used. If this option is not given, this method uses
47824825 a format that consists of a tuple (one element per row) of strings
@@ -4803,6 +4846,8 @@ def data(self, format=None, *, from_coords=None,
48034846 options = ()
48044847 if format is not None :
48054848 options += ('-format' , format )
4849+ if metadata is not None :
4850+ options += ('-metadata' , self ._metadata (metadata ))
48064851 if from_coords is not None :
48074852 options += ('-from' , * from_coords )
48084853 if grayscale :
0 commit comments