我需要制作三条条纹第一个需要形状高度的 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)
关于你的错误:
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)
一种将颜色值递增 1 并避免高度级别的简单方法:
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)
只需根据需要更改颜色和水平或垂直值和参数。遵循这种类型的逻辑将得到结果