12:07:11 am on 2/11/12

Menu
» Home
» About Scott
» VD Labs
» QRSS VD
» Old Stuff
» Archive
» Publications
» Contact

Categories
» C/C++
» Circuitry
» DIY ECG
» General
» high altitude balloon
» Linux
» Microcontrollers
» Molecular Biology
» My Website
» PHP
» Prime Numbers
» Python
» Radio
» UCF Lab
» Everything
» RF Links

Writings
» MD Labels
» Streamrip
» AIM Thoughts
» WindowsXP?
» Partitioning
» CD/DVD Repair
» Monitor Info
» CRT Deflection
» Venomcrack
» Flash Thing
» Heart/Brain
» Diabetes
» Triops
» Biomed

Friends
» Mike
» Fred
» Kyle W
» Nick
» Louis
» Tom
» Kyle H




Archives
» August 2011
» July 2011
» June 2011
» March 2011
» February 2011
» January 2011
» December 2010
» November 2010
» September 2010
» August 2010
» July 2010
» June 2010
» May 2010
» April 2010
» March 2010
» February 2010
» January 2010
» December 2009
» September 2009
» August 2009
» July 2009
» June 2009
» May 2009
» April 2009
» March 2009
» February 2009
» January 2009
» December 2008
» November 2008
» October 2008
» September 2008
» September 2007
» December 2006
» August 2006
» January 2006
» August 2005
» July 2005
» June 2005
» May 2005
» April 2005
» March 2005
» February 2005
» January 2005
» December 2004
» November 2004
» October 2004
» September 2004
» August 2004
» July 2004
» June 2004
» May 2004
» April 2004
» March 2004
» February 2004
» January 2004
» December 2003
» November 2003
» October 2003
» September 2003
» August 2003
» July 2003
» June 2003
» May 2003
» April 2003
» March 2003
» February 2003
» January 2003
» December 2002
» November 2002
» October 2002
» September 2002
» June 2001
« Viewing large Images with Scrollbars Using Python, Tk, and PIL
Realtime FFT Graph of Audio WAV File or Microphone Input with Python, Scipy, and WCKgraph »


Smoothly Scroll an Image Across a Window with Tkinter vs. PyGame
470 words | Posted on March 5th, 2010
Scott was 24.44 years old when he wrote this!
Filed under: General, Python

The goal is simple: have a super-large image (larger than the window) automatically scroll across a Python-generated GUI window. I already have the code created to generate spectrograph images in realtime, now I just need a way to have them displayed in realtime. tk scrollingAt first I tried moving the coordinates of my images and even generating new images with create_image(), but everything I did resulted in a tacky “flickering” effect (not to mention it was slow). Thankfully I found that self.canv.move(self.imgtag,-1,0) can move a specific item (self.imgtag) by a specified amount and it does it smoothly (without flickering). Here’s some sample code. Make sure “snip.bmp” is a big image in the same folder as this script

from Tkinter import *
import Image, ImageTk

class scrollingImage(Frame):

     def go(self):
		self.canv.move(self.imgtag,-1,0)
		self.canv.update()
		self.after(100,self.go)

     def __init__(self, parent=None):
		Frame.__init__(self, parent)
		self.master.title("Spectrogram Viewer")
		self.pack(expand=YES, fill=BOTH)
		self.canv = Canvas(self, relief=SUNKEN)
		self.canv.config(width=200, height=200)
		self.canv.config(highlightthickness=0)

		sbarV = Scrollbar(self, orient=VERTICAL)
		sbarH = Scrollbar(self, orient=HORIZONTAL)

		sbarV.config(command=self.canv.yview)
		sbarH.config(command=self.canv.xview)

		self.canv.config(yscrollcommand=sbarV.set)
		self.canv.config(xscrollcommand=sbarH.set)

		sbarV.pack(side=RIGHT, fill=Y)
		sbarH.pack(side=BOTTOM, fill=X)

		self.canv.pack(side=LEFT, expand=YES, fill=BOTH)
		self.im=Image.open("./snip.bmp")
		width,height=self.im.size
		#self.canv.config(scrollregion=(0,0,width,height))
		self.canv.config(scrollregion=(0,0,300,300))
		self.im2=ImageTk.PhotoImage(self.im)
		x,y=0,0
		self.imgtag=self.canv.create_image(x,y,\
		     anchor="nw",image=self.im2)
		self.go()

scrollingImage().mainloop()

Alternatively, I found a way to accomplish a similar thing with PyGame. I’ve decided not to use PyGame for my software package however, because it’s too specific and can’t be run well alongside Tk windows, and it would be insanely hard to add scrollbars to the window. However it’s extremely effective at scrolling images smoothly. Anyhow, here’s the code:

import pygame
from PIL import Image

im=Image.open("1hr_original.jpg")
graphic = pygame.image.fromstring(im.tostring(),im.size,im.mode)
screen = pygame.display.set_mode((400, 300))
clock = pygame.time.Clock()
running = 1
x,y=0,0
while running:
   clock.tick(30)
   for event in pygame.event.get():    #get user input
      if event.type == pygame.QUIT:    #if user clicks the close X
           running = 0                 #make running 0 to break out of loop
   screen.blit(graphic, (x, y))
   pygame.display.flip()   #Update screen
   x-=1




This entry was posted on Friday, March 5th, 2010 at 8:49 amand is filed under General, Python. 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.

Leave a Reply




copyright © 2006 swharden@gmail.com