0

我需要制作三个条纹,第一个需要是形状高度的 40% 和 256 像素宽,红色分量从 0-255 逐渐增加并水平遍历图像

第二个是形状高度的 20%,宽度相同(高度 300)它是纯绿色

第三是形状高度的 40%,蓝色将从 255-0 减少

我在第二个 for 循环中不断收到错误 (rheight,rheight) 请帮忙!

def drawLines():
  height = int(input("Enter Height: "))
  width = 256
  picture = makeEmptyPicture(width,height)
  rheight = height*0.4

  redValue = 0
  for y in range(0,height):
    for x in range(0,width):
      pixel = getPixel(picture, x, y)
      color = makeColor(redValue,0,0)
      setColor(pixel, color)
    redValue = redValue + 50
  explore(picture)


  for y in range(rheight,rheight):    
    for x in range(0, width):         
       pixel = getPixel(picture, x, y)
       color = makeColor(0, 0, 0)      # Change the current pixel to black
       setColor(pixel, color)
  explore(picture)                   
4

2 回答 2

0

A simple way to increment your color values by one, and to avoid the rheight level:

def d():
  file = pickAFile()
  pic = makePicture(file)
  w= getWidth(pic)
  h= getHeight(pic)
  show (pic)
  newPic = makeEmptyPicture(w,h)
  for y in range (0 ,h-1):  
    for x in range(0,w-1):
      pixel = getPixel(pic, x, y)
      newPixel = getPixel(newPic,x, y)
      if(y == h*0.4):
        #the red value will increase incrementally by one as the x value increases
        color = makeColor(x,0,0)
      else:
        color = getColor(pixel)
      setColor(newPixel, color)
  writePictureTo(newPic, r"D:\temp.jpg")
  explore(newPic)

Just vary color and horizontal or vertical values and parameters as needed. Following this type of logic will get you the results

于 2014-01-26T10:52:07.403 回答
0

关于你的错误:

The error was: 1st arg can't be coerced to int
Inappropriate argument type.
An attempt was made to call a function with a parameter of an invalid type. 
This means that you did something such as trying to pass a string to a method 
that is expecting an integer.

这是因为range()函数需要integers作为参数。

当您这样做时rheight = height*0.4,作为0.4浮点数,python/jython 解释器也将 "height*0.4" 计算为浮点数。导致“rheight”是一个浮点数。

修复:您必须cast将值明确表示为整数:

rheight = int(height*0.4)
于 2013-11-11T13:53:01.410 回答