vy_007 0 Newbie Poster

I want to print the histogram of an image in the same window. The given code works fine with viewing the histogram through Windows Image Viewer(default program) but fails to print the required histogram and image in a frame.

# RGB Hitogram
# This script will create a histogram image based on the RGB content of
# an image. It uses PIL to do most of the donkey work but then we just
# draw a pretty graph out of it.
#
# 
#

import ImageDraw
from Tkinter import *
from PIL import ImageTk, Image

root=Tk()

def show(root,filename):
 imagepath = filename  # The image to build the histogram of


 histHeight = 120            # Height of the histogram
 histWidth = 256             # Width of the histogram
 multiplerValue = 1.5        # The multiplier value basically increases
                            # the histogram height so that love values
                            # are easier to see, this in effect chops off
                            # the top of the histogram.
 showFstopLines = True       # True/False to hide outline
 fStopLines = 5


 # Colours to be used
 backgroundColor = (51,51,51)    # Background color
 lineColor = (102,102,102)       # Line color of fStop Markers 
 red = (255,60,60)               # Color for the red lines
 green = (51,204,51)             # Color for the green lines
 blue = (0,102,255)              # Color for the blue lines

 ##################################################################################


 img = Image.open(imagepath)
 hist = img.histogram()
 histMax = max(hist)                                     #comon color
 xScale = float(histWidth)/len(hist)                     # xScaling
 yScale = float((histHeight)*multiplerValue)/histMax     # yScaling 


 im = Image.new("RGBA", (histWidth, histHeight), backgroundColor)   
 draw = ImageDraw.Draw(im)


 # Draw Outline is required
 if showFstopLines:    
    xmarker = histWidth/fStopLines
    x =0
    for i in range(1,fStopLines+1):
        draw.line((x, 0, x, histHeight), fill=lineColor)
        x+=xmarker
    draw.line((histWidth-1, 0, histWidth-1, 200), fill=lineColor)
    draw.line((0, 0, 0, histHeight), fill=lineColor)


 # Draw the RGB histogram lines
 x=0; c=0;
 for i in hist:
    if int(i)==0: pass
    else:
        color = red
        if c>255: color = green
        if c>511: color = blue
        draw.line((x, histHeight, x, histHeight-(i*yScale)), fill=color)        
    if x>255: x=0
    else: x+=1
    c+=1

 # Now save and show the histogram    
 im.save('tmp.png', 'PNG')
 im.show()              #woking
 im=Image.open('tmp.png')

 h_im = im.resize((400,400),Image.ANTIALIAS)
 photo = ImageTk.PhotoImage(h_im.mode,h_im.size)

 label=Label(root, image=photo)
 label.image=photo
 label.grid(row=6, column=0, columnspan=5, rowspan=5, sticky=W+E+N+S)#not working

 im=Image.open('logo.jpg')
 l_im = im.resize((400,400),Image.ANTIALIAS)
 ph = ImageTk.PhotoImage(l_im.mode,l_im.size)

 l2=Label(root,image=ph)
 l2.grid(row=7, column=0, columnspan=5, rowspan=5, sticky=W+E+N+S) #not working

show(root,"logo.jpg")

root.mainloop()

Please help me...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.