在JES中覆盖图像上的图像和图像上的文本的代码

时间:2018-04-15 10:53:31

标签: python

我需要帮助制作一个代码,用于覆盖图像和JES中图像上的文本。

由于对图像上覆盖图像,图像上的文本,缩放文本和图像上下的透明度以及透明度水平的了解有限,我无法启动。

如果有人会帮助我,那就太好了。感谢

def scale(picture,factor):
  newHeight = int(factor*getHeight(picture))+1
  newWidth = int(factor*getWidth(picture))+1
  returnPic = makeEmptyPicture(int(newWidth),int(newHeight))
  sx = 0
  for tx in range(0,newWidth):
    sy = 0
    for ty in range(0,newHeight):
      if (int(sx) < getWidth(picture)) and (int(sy) < getHeight(picture)):
        sp = getPixel(picture,int(sx),int(sy))
        tp = getPixel(returnPic,tx,ty)
        setColor(tp,getColor(sp))
      sy = sy + (1/factor)
    sx = sx + (1/factor)

  show(returnPic)

def grayScaleNew():
   picture = makePicture("forbidden-city.jpg")
   for px in getPixels(picture):
     newRed = getRed(px) * 0.2121 
     newGreen = getGreen(px) * 0.7152
     newBlue = getBlue(px) *  0.0722
     luminance = newRed + newGreen + newBlue
     setColor(px,makeColor(luminance,luminance,luminance))

def testTransparentOverlay():

   # This function reads in two pictures and produces a new picture that
   # is made by copying picture 1 and then overlaying picture 2 on top
   # of picture 1. (It is placed in the centre of the image)
   # This requires the width and height of picture 2 to be less than 
   # the width and height of picture 2 (so it fits). So it checks 
   # that the size of each image is correct before creating the new picture.

   # 1. Get the two pictures and show them  
   file = pickAFile()
   picture1 = makePicture(file)
   file = pickAFile()
   picture2 = makePicture(file)
   show(picture1)
   show(picture2)

   #2. Test the size of the pictures - picture 2 needs to fit inside
   #   picture 1 (so check width and height)

   if getWidth(picture2) > getWidth(picture1):
      errorMessageWidth = "ERROR: The width of picture2 is greater than picture1"
      print(errorMessageWidth)
   elif getHeight(picture2) > getHeight(picture1):
      errorMessageWidth = "ERROR: The width of picture2 is greater than picture1"
      print(errorMessageWidth)
   else: #do the overlay and show the result
      #work out the position for overlaying first
      xMargin = (getWidth(picture1) - getWidth(picture2)) / 2
      yMargin = (getHeight(picture1) - getHeight(picture2)) / 2
      proportion = 60 #try 60% transparency
      picture3 = transparency(picture1, picture2, xMargin, yMargin, proportion)
      show(picture3)


def transparency(picture1, picture2, xStart, yStart, proportion):
     # overlays picture 2 in the centre of picture 1 with 
     # a transparency of proportion (%)
     # returns a new picture - copy of picture 1 with the overlay 
     # (there are no side effects)

     picture3 = duplicatePicture(picture1)

     # calculate the proportions of overlay and the base image
     # keep any decimal places until after calculations
     overlay = (proportion) / 100.0   # convert overlay from percentage to a fraction 
                                      # eg. 60% is 60/100 which is 0.6 
     base = (100-proportion) / 100.0  # convert base amount from percentage to a fraction 
                                      # eg. if overlap is 60% is (100-60) / 100 which is 0.5  

     #Copy all the pixels from picture 2 onto the new picture 3
     for x in range(0, getWidth(picture2)):
        for y in range(0, getHeight(picture2)):  
           pixel2 = getPixel(picture2, x, y)                # pixel to copy (and colours)
           pixel2Red   = getRed(pixel2)
           pixel2Green = getGreen(pixel2)
           pixel2Blue  = getBlue(pixel2) 

           pixel3 = getPixel(picture3, x+xStart, y+yStart)  # overlap pixel (and colours)
           pixel3Red   = getRed(pixel3)
           pixel3Green = getGreen(pixel3)
           pixel3Blue  = getBlue(pixel3)

           #combine the colours in correct proportions - convert back to integers
           newRed =   int(pixel2Red * overlay)   + int(pixel3Red * base)
           newGreen = int(pixel2Green * overlay) + int(pixel3Green* base)
           newBlue =  int(pixel2Blue * overlay)  + int(pixel3Blue * base)

           #set the new colours for picture3
           setRed(pixel3, newRed)
           setGreen(pixel3, newGreen)
           setBlue(pixel3, newBlue)


     return picture3

这些是我编写的所有代码,但我不确定如何将它们组合在一起以使其整体工作。如果那是有道理的。

谢谢。

1 个答案:

答案 0 :(得分:0)

我首先给出你要求帮助的INFT1004作业的引用。

&#34; 特别是,您应该尝试不使用外部来源的代码或算法,而不是从教师以外的人那里获得帮助,因为这可能会阻止您掌握这些概念&#34;

在此作业中明确指出,您不应该在线询问人或使用您在网上找到或要求的代码,并且违反纽卡斯尔大学学术诚信代码 - 您知道在开始之前您做过模块的事情课程。 该帖子的副本将发送给课程讲师。