views:

199

answers:

2

This is from a class assignment:

This program is about listening to colors. We will treat pictures as piano scores. Write a function called listenToPicture that takes one picture as an argument. It first shows the picture. Next, it will loop through every 4th pixel in every 4th row and do the following. It will compute the total of the red, green and blue levels of the pixel, divide that by 9, then add the result to 24. That number will be the note number played by playNote. That means that the darker the pixel, the lower the note; the lighter the pixel, the higher the note. It will play that note at full volume (127) for a tenth of a second (100 milliseconds). Every time it moves to a new row, it prints out the row number (y value) on the console. Your main function will ask the user to select a file with a picture. It will print the number of notes to be played (which is the number of pixels in the picture divided by 16; why?). It will then call the listenToPicture function.

Ok I edited in what I have so far and the only thing I haven't figured out (I believe) is how to print the number of notes in the main function. By the way, thanks to everyone who helped. You guys are amazing. Is there a place to donate to this site?

def main():
   pic=makePicture(pickAFile())
   show (pic)
   listenToPicture(pic)

def listenToPicture(pic):
   w=getWidth(pic)
   h=getHeight(pic)

   for y in range(0,h,4):
       printNow(str(y))
       for x in range (0,w,4):
            px=getPixel(pic,x,y)                            
            r=getRed(px)
            g=getGreen(px)
            b=getBlue(px)
            tot=((r+g+b)/9)+24
            playNote(tot,100,127)
+1  A: 

Robbie is right for the width/height for loops.

The loop you are using to get the pixels and play the notes looks as if it is getting ALL the pixels and playing them all every time you get a unique x and y. What you should be doing is be getting the pixel at (x,y) then pulling out the rgb values and calling play note on that. You really shouldn't even need the 3rd for loop. You're not too far off. Try writing the problem out in logical steps in plain English. I find that helps a ton before I start coding.

Good Luck.

Daniel
+1  A: 

You asked about similar things before. Well, since you didn't put any code in about actually retrieving the pixel value, I'll assume that you still aren't able to do that. I know this is going way beyond your question, but last time you were pretty vague about your question and indicated that you needed more help than just what you had asked. If any of this is not necessary then just ignore it. I'm just trying to offer some advice and you can take it or leave it.

In case you haven't figured out how to read a pixel, I recommend using PIL. It has functions for opening images documented here. Then you can access a pixel in the image by its x and y value using getpixel which is documented on the same page.

For playing the note I would recommend looking into the PyAudio module and just making your own sinusoids of various frequencies (depending on the magnitude of the pixel) that you write to an open audio stream. There might be better packages for this part, but this is what I have used in my small adventures in Python audio.

For the audio stuff, I would try just outputting a sound at a fixed frequency before trying to actually emit a varying frequency.

Edit: Your loops look better now so I took out my stuff about your loops.

Justin Peel