How to find the index of value in python list?

How to draw a histogram in python?

  • For my programming class I have to write a program where I have to draw a picture histogram. Here is the problem I have to solve.... In the histograms on the right, the x axis (going rightward) represents increasing brightness from 0 to 255. The height of the vertical line of black pixels represents the relative percentage of the pixels at that level of brightness. How do you do this? The idea is to first create a list named pixelCount, with 256 slots. Initially it will be filled with zeros (hint: you should not type out 256 zeros by hand). For each pixel in the image, your program will compute its brightness i (the average of its R,G,B values). It will add 1 to the running total pixelCount[i], which is the number of pixels in the image with brightness i. So, to state a different way, each slot in pixelCount will hold the number of pixels with the same intensity level as the index number for that slot. Once your program has finished calculating the pixelCount list, it will compute the maximum value in that list. To do this, use a second loop that keeps a running maximum. Finally, you will display the histogram. You will create an empty 256x100 picture, and use a third loop for x from 0 through 256. You will use the addLine function to draw the vertical black lines. The length (re: height) of the line at horizontal position x is the relative percentage of pixels with brightness level i. The relative percentage of pixels with brightness level i will be 100* pixelCount[i] divided by this maximum found above.   Here is my coding so far..... def main( ): #make a picture from what the users selected file = pickAFile ( ) pic = makePicture ( file ) show ( pic ) #list pixelCount with 250 slot filled with zero pixelCount = range ( 0, 251 ) total = 0 for number in pixelCount: total = total + number printNow ( total ) #get the brightness for px in getPixels ( pic ): r = getRed ( px ) g = getGreen ( px ) b = getBlue ( px ) #compute its brightness and add 1 to the running total pixelCount brightness = ( r + g + b )/ 3 total = brightness + 1 repaint ( pic ) #program finish calcutating the pixelount list, it will compute the max vlaue in the list #use a loop that keeps a running max max = pixelCount [ 0 ] for k in range (1, len( pixelCount) ): if pixelCount [ k ] > max: max = pixelCount [ k ] printNow ( max ) #create empty 265x100 picture picture = makeEmptyPicture ( 265, 100, white ) show ( pic) I really need help please because I don't understand how to do it....Please and Thank you

  • Answer:

    I don't know the library you're using for display and you didn't include any import statements or def's for functions you are using. I can comment on the histogram, though. Broken. Multiple compound fracture. You need a list of counters, initially all zero, for each possible brightness. That what it looks like you intend pixelCount to be, but range(0, 251) isn't big enough, isn't a list (it's a range object...a special kind of tuple. You can't update either a tuple or a range object.) and doesn't start with all zeros. Try: histogram = [0]*256 That creates a list of 256 zeros. I peeked at the web and it looks like you are using JES. The brightness is computed correctly but what you want to do with that is: histogram[brightness] = histogram[brightness] + 1 That counts how many pixels have each possible average brightness from 0 to 255. That's what a histogram is...a count of the number of times each value occurs in a collection of values. You also need a total pixel count. Try using the name "pixelCount" for that, instead of for your histogram. You could initialize pixelCount to 0 and add 1 to it inside the "for px in getPixels" loop. Or, you can save some time and simply total up the histogram entries after the loop is done. On a 10 megapixel image, what do you want to do...10 million adds or 256? Total up the histogram manually with: pixelCount = 0 for (x in histogram): pixelCount = pixelCount + x Or, even better...use the builtin sum function: pixelCount = sum(histogram) Get rid of total and what you previously did with pixelCount. Your maximum computation is off. First, if you are going to find the maximum manually, don't bother with "ki in range". You can get the same effect with: maxCount = histogram[0] # don't use max...see below for why not for x in histogram: ... if x > maxCount: ... ... maxCount = x The ... tokens are for indentation. That will find the maximum, looking at histogram[0] twice, but that's not too expensive for clarity. But, there's a better way. This comes up often enough that there's a builtin function: maxCount = max(histogram) However, if you use max as a variable, that will hide the builtin and you won't be able to use it. Can't help you with the drawing of the histogram, though. You'll have to look at your notes for that.

babybug0... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.