Reputation: 1
This code down below should draw two vertical lines, one red line from (50,0) to (50,300) and one made up of random pixels from (150,50) to (150,250). Where it says randomRed.setPixel(150,x,randomRed) on line 13, it points out that 'Pixel' object has no attribute 'setPixel' error. I cannot figure out why this is an error. I have to setPixel right?
from cImage import*
import random
myImWin = ImageWin("Line Image", 300, 300)
lineImage = EmptyImage(300,300)
redPixel = Pixel(255,0,0)
randomRed = Pixel(random.randint(0,255),0,0)
for i in range(300):
for x in range(250):
lineImage.setPixel(50,i,redPixel)
randomRed.setPixel(150,x,randomRed) # here is the error it points out
lineImage.draw(myImWin)
randomRed.save("lineImage.gif")
myImWin.exitonClick()
Upvotes: 0
Views: 2290
Reputation: 61617
randomRed
is a Pixel
. Why would you set a pixel of a pixel? What are you expecting that to mean?
You're trying to draw the randomRed
Pixel
onto the same lineImage
that you draw the redPixel
onto, so both times that you use .setPixel
, it should be lineImage.setPixel
.
The error means exactly what it says. When you write something like x.y(...)
, you are attempting to call the y
method of the x
object. More precisely, you attempt to look up the y
attribute of the x
object, and then treat it as a function by calling it. (Methods are a special kind of attribute, and a special kind of function; x
gets passed implicitly as a parameter to the function when it is called.)
Also, your looping logic is wrong. By nesting the loops, you run the innermost code for every combination of (i, x) values. Each line is a separate entity. Run one loop over i
to draw the first line, and another loop over x
to draw the second line.
Finally, you will not get a line "of random pixels" the way you are doing things, because you create the randomRed
pixel once, with a given random number, and then reuse the same pixel for the entire line. To fix that, create the random pixel inside the loop before drawing it. Then, each time through the loop, it gets re-created, forcing another random number to be chosen.
Upvotes: 0
Reputation: 8721
My guess would be that you are calling randomRed.setPixel(...)
instead of lineImage.setPixel(...)
.
Upvotes: 1