Help with loading file into an array
Jens Thoms Toerring
jt at toerring.de
Sun May 5 07:44:19 EDT 2013
peter berrett <pwberrett at gmail.com> wrote:
> I am trying to build a program that can find comets in a series of
> astronomical images. I have already written functions to find the comet in a
> series of images, the data of which is stored in embedded lists.
> The area I am having difficulty with is take a standard gif file (1024 x
> 1024) and reading it into an array or embedded lists.
> In a nutshell here is an example of what I want to do
> Let's say I have a gif file called 20130428_0000_c2_1024.gif in a folder
> called c:\comets
> I want to read the data from that gif file taking the red data (excluding
> the green and blue data) and store that in an array called Image[][] which
> is a nested array length 1024 with a list in each item of 1024 length (ie
> 1024 x 1024)
> Could someone please provide a piece of code to do the above so I can then
> go on to modify it to pick up different files from different folders? In
> particular I am keen to seen how you read in the data and also how you
> change the directory from which you are reading the image. >
the following should do the trick using, as Fábio already
suggested, the Python Image Library (PIL):
----------------8<-------------------------
#!/ur/bin/env python
import Image
im = Image.open( 'c:/comets/20130428_0000_c2_1024.gif' )
w, h = im.size
red_arr = [ ]
for y in range( h ) :
red_line = [ ]
for x in range( w ) :
red_line.append( im.getpixel( ( x, y ) )[ 0 ] )
red_arr.append( red_line )
----------------8<-------------------------
For obvious reasons I couldn't use 'Image' as the name of the list
of lists, so it's 'red_arr' instead. This is probably not the fas-
test solution, but it's simple and hopefully will get you started.
Concerning reading other files: here I may not understand your
problem since it looks rather trivial to me by simply passing
the open() method of 'Image' the name of a file in a different
directory.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt at toerring.de
\__________________________ http://toerring.de
More information about the Python-list
mailing list