JE中图像对图像和文本对图像的覆盖代码

2024-06-24 13:47:51 发布

您现在位置: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

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

谢谢。在


Tags: andofthein图像forisint
1条回答
网友
1楼 · 发布于 2024-06-24 13:47:51

我先给你引述一下你正在寻求帮助的INFT1004作业。在

尤其是,您应该尽量不要使用来自外部来源的代码或算法,也不要从导师以外的人那里获得帮助,因为这会妨碍您掌握这些概念

这项作业特别指出,你不应该在网上问别人,也不应该使用你在网上找到或要求的代码,这违反了纽卡斯尔大学学术诚信准则——你知道你在开始课程之前所做的模块。 这篇文章的一份副本将随附给课程指导老师。在

相关问题 更多 >