Viewing large Images with Scrollbars Using Python, Tk, and PIL
447 words | Posted by Scott on March 3rd, 2010
Scott was 24.44 years old when he wrote this!
Filed under: General, Python, Radio
I don’t have much time to write (I have a million things to do over this spring break!) but I’ll try to post what I discover as I go with the hope that it might help someone else out there. It’s amazing how simple this program is, and it’s frustrating how long it took me to figure out how to code the darn image viewer thing. So, for what it’s worth, here it is.
This little Python program will load an image (pretty much any format) using the Python Imaging Library (which must be installed) and allows you to see it on a scrollable canvas (in two directions) with Tkinter and ImageTk. The above screenshot is of the program viewing the image below:

What is that image? I won’t get ahead of myself, but it’s about 5kHz above 10.140mHz (30m radio band) including a popular QRSS calling frequency representing an entire hour of data. Also, I did all of the math myself with my own python scripts (yes, it eats WAV files and spits out spectrographs). Note that the JPG I uplaoded “full size” is no where near the ACTUAL full size of the image. It’s gagillions of pixels in all directions. Sweet. My goal is to have it scroll in the TK window, with slide-adjustable brightness/contrast/etc. I’ll get there eventually.
from Tkinter import *
import Image, ImageTk
class ScrolledCanvas(Frame):
def __init__(self, parent=None):
Frame.__init__(self, parent)
self.master.title("Spectrogram Viewer")
self.pack(expand=YES, fill=BOTH)
canv = Canvas(self, relief=SUNKEN)
canv.config(width=400, height=200)
#canv.config(scrollregion=(0,0,1000, 1000))
#canv.configure(scrollregion=canv.bbox('all'))
canv.config(highlightthickness=0)
sbarV = Scrollbar(self, orient=VERTICAL)
sbarH = Scrollbar(self, orient=HORIZONTAL)
sbarV.config(command=canv.yview)
sbarH.config(command=canv.xview)
canv.config(yscrollcommand=sbarV.set)
canv.config(xscrollcommand=sbarH.set)
sbarV.pack(side=RIGHT, fill=Y)
sbarH.pack(side=BOTTOM, fill=X)
canv.pack(side=LEFT, expand=YES, fill=BOTH)
self.im=Image.open("./1hr_original.jpg")
width,height=self.im.size
canv.config(scrollregion=(0,0,width,height))
self.im2=ImageTk.PhotoImage(self.im)
self.imgtag=canv.create_image(0,0,anchor="nw",image=self.im2)
ScrolledCanvas().mainloop()
This entry was posted on Wednesday, March 3rd, 2010 at 7:56 pmand is filed under General, Python, Radio. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.