IMOC
July 1st, 2008, 09:18 AM
I have some Python code which loads a gif image from a file and stores the raw image data (just one channel) into a Python array ready to be sent to a display. This code snippet (which does work) shows how I am currently doing this:
import os, time, sys, array # Standard python modules
from Tkinter import *
# Load a .gif file
image_filename = "test.gif" # 1280 x 950 image
start_time = time.time()
root = Tk()
bg_image = PhotoImage(file = image_filename)
image_y_size = bg_image.height()
image_x_size = bg_image.width()
# Copy image data into an array
bg_image_array = array.array('B') # Clear array
# Assume landscape mode background image
for y in range(image_y_size):
for x in range(image_x_size):
bg_image_array.append(int(bg_image.get(x, y).split(' ')[0]) >> 4)
stop_time = time.time()
# Code to send bg_image to read H/W display goes here...
#
# Output time taken
print "Time taken: %f" % (stop_time - start_time)
The problem I have is this code takes 17 seconds to run with a 1280 x 960 image, which is far too long for my application.
I am quite new to Python and this is my first attempt at coding this. Is there a better was to code this which will speed it up?
Is it worth coding this part of the code in C to speed it up?
Thanks
import os, time, sys, array # Standard python modules
from Tkinter import *
# Load a .gif file
image_filename = "test.gif" # 1280 x 950 image
start_time = time.time()
root = Tk()
bg_image = PhotoImage(file = image_filename)
image_y_size = bg_image.height()
image_x_size = bg_image.width()
# Copy image data into an array
bg_image_array = array.array('B') # Clear array
# Assume landscape mode background image
for y in range(image_y_size):
for x in range(image_x_size):
bg_image_array.append(int(bg_image.get(x, y).split(' ')[0]) >> 4)
stop_time = time.time()
# Code to send bg_image to read H/W display goes here...
#
# Output time taken
print "Time taken: %f" % (stop_time - start_time)
The problem I have is this code takes 17 seconds to run with a 1280 x 960 image, which is far too long for my application.
I am quite new to Python and this is my first attempt at coding this. Is there a better was to code this which will speed it up?
Is it worth coding this part of the code in C to speed it up?
Thanks