parameters: (filename, maskfilename, imageDom, destimage, stretch=True)
return_statement: return True
docstring: Paste the image specified in the filename into the specified image
docstring_summary: Paste the image specified in the filename into the specified image
docstring_tokens: Paste the image specified in the filename into the specified image
function: def paintImage(filename, maskfilename, imageDom, destimage, stretch=True):
      """Paste the image specified in the filename into the specified image"""
  
      if not doesFileExist(filename):
          write("Image file (%s) does not exist" % filename)
          return False
  
      picture = Image.open(filename, "r")
      xpos = getScaledAttribute(imageDom, "x")
      ypos = getScaledAttribute(imageDom, "y")
      w = getScaledAttribute(imageDom, "w")
      h = getScaledAttribute(imageDom, "h")
      (imgw, imgh) = picture.size
      write("Image (%s, %s) into space of (%s, %s) at (%s, %s)" % (imgw, imgh, w, h, xpos, ypos), False)
  
      # the theme can override the default stretch behaviour
      if imageDom.hasAttribute("stretch"):
          if imageDom.attributes["stretch"].value == "True":
              stretch = True
          else:
              stretch = False
  
      if stretch == True:
          imgw = w;
          imgh = h;
      else:
          if float(w)/imgw < float(h)/imgh:
              # Width is the constraining dimension
              imgh = imgh*w//imgw
              imgw = w
              if imageDom.hasAttribute("valign"):
                  valign = imageDom.attributes["valign"].value
              else:
                  valign = "center"
  
              if valign == "bottom":
                  ypos += h - imgh
              if valign == "center":
                  ypos += (h - imgh)//2
          else:
              # Height is the constraining dimension
              imgw = imgw*h//imgh
              imgh = h
              if imageDom.hasAttribute("halign"):
                  halign = imageDom.attributes["halign"].value
              else:
                  halign = "center"
  
              if halign == "right":
                  xpos += w - imgw
              if halign == "center":
                  xpos += (w - imgw)//2
  
      write("Image resized to (%s, %s) at (%s, %s)" % (imgw, imgh, xpos, ypos), False)
      picture = picture.resize((imgw, imgh))
      picture = picture.convert("RGBA")
  
      if maskfilename != None and doesFileExist(maskfilename):
          maskpicture = Image.open(maskfilename, "r").resize((imgw, imgh))
          maskpicture = maskpicture.convert("RGBA")
      else:
          maskpicture = picture
  
      destimage.paste(picture, (xpos, ypos), maskpicture)
      del picture
      if maskfilename != None and doesFileExist(maskfilename):
          del maskpicture
  
      write ("Added image %s" % filename)
  
      return True
function_tokens: def paintImage ( filename , maskfilename , imageDom , destimage , stretch = True ) : if not doesFileExist ( filename ) : write ( "Image file (%s) does not exist" % filename ) return False picture = Image . open ( filename , "r" ) xpos = getScaledAttribute ( imageDom , "x" ) ypos = getScaledAttribute ( imageDom , "y" ) w = getScaledAttribute ( imageDom , "w" ) h = getScaledAttribute ( imageDom , "h" ) ( imgw , imgh ) = picture . size write ( "Image (%s, %s) into space of (%s, %s) at (%s, %s)" % ( imgw , imgh , w , h , xpos , ypos ) , False ) # the theme can override the default stretch behaviour if imageDom . hasAttribute ( "stretch" ) : if imageDom . attributes [ "stretch" ] . value == "True" : stretch = True else : stretch = False if stretch == True : imgw = w imgh = h else : if float ( w ) / imgw < float ( h ) / imgh : # Width is the constraining dimension imgh = imgh * w // imgw imgw = w if imageDom . hasAttribute ( "valign" ) : valign = imageDom . attributes [ "valign" ] . value else : valign = "center" if valign == "bottom" : ypos += h - imgh if valign == "center" : ypos += ( h - imgh ) // 2 else : # Height is the constraining dimension imgw = imgw * h // imgh imgh = h if imageDom . hasAttribute ( "halign" ) : halign = imageDom . attributes [ "halign" ] . value else : halign = "center" if halign == "right" : xpos += w - imgw if halign == "center" : xpos += ( w - imgw ) // 2 write ( "Image resized to (%s, %s) at (%s, %s)" % ( imgw , imgh , xpos , ypos ) , False ) picture = picture . resize ( ( imgw , imgh ) ) picture = picture . convert ( "RGBA" ) if maskfilename != None and doesFileExist ( maskfilename ) : maskpicture = Image . open ( maskfilename , "r" ) . resize ( ( imgw , imgh ) ) maskpicture = maskpicture . convert ( "RGBA" ) else : maskpicture = picture destimage . paste ( picture , ( xpos , ypos ) , maskpicture ) del picture if maskfilename != None and doesFileExist ( maskfilename ) : del maskpicture write ( "Added image %s" % filename ) return True