Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44727 > unrolled thread
| Started by | peter berrett <pwberrett@gmail.com> |
|---|---|
| First post | 2013-05-04 23:06 -0700 |
| Last post | 2013-05-06 17:28 +0100 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
Help with loading file into an array peter berrett <pwberrett@gmail.com> - 2013-05-04 23:06 -0700
Re: Help with loading file into an array Fábio Santos <fabiosantosart@gmail.com> - 2013-05-05 08:43 +0100
Re: Help with loading file into an array "Colin J. Williams" <cjw@ncf.ca> - 2013-05-05 07:42 -0400
Re: Help with loading file into an array jt@toerring.de (Jens Thoms Toerring) - 2013-05-05 11:44 +0000
Re: Help with loading file into an array Joshua Landau <joshua.landau.ws@gmail.com> - 2013-05-06 17:28 +0100
| From | peter berrett <pwberrett@gmail.com> |
|---|---|
| Date | 2013-05-04 23:06 -0700 |
| Subject | Help with loading file into an array |
| Message-ID | <c4a4e4ee-11c9-4b5e-a24a-10be49a4ce5d@googlegroups.com> |
Hi all 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. For the record this is not for homework but is a pet project of mine. I have already written a version of the program in Justbasic but python is faster. I am also interested in readers views as to which is the simplest and best way to achieve what I am trying to do. Thanks Peter
[toc] | [next] | [standalone]
| From | Fábio Santos <fabiosantosart@gmail.com> |
|---|---|
| Date | 2013-05-05 08:43 +0100 |
| Message-ID | <mailman.1290.1367740305.3114.python-list@python.org> |
| In reply to | #44727 |
[Multipart message — attachments visible in raw view] — view raw
Using a nested array should waste a lot of memory. I think you should use PIL to load and read the image. > > 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) >
[toc] | [prev] | [next] | [standalone]
| From | "Colin J. Williams" <cjw@ncf.ca> |
|---|---|
| Date | 2013-05-05 07:42 -0400 |
| Message-ID | <km5gj7$mk2$1@theodyn.ncf.ca> |
| In reply to | #44728 |
On 05/05/2013 3:43 AM, Fábio Santos wrote: > Using a nested array should waste a lot of memory. I think you should > use PIL to load and read the image. > > > > > 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) > > > Fabio, Have you considered numpy? Colin W.
[toc] | [prev] | [next] | [standalone]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Date | 2013-05-05 11:44 +0000 |
| Message-ID | <aumv0jFfjh5U1@mid.uni-berlin.de> |
| In reply to | #44727 |
peter berrett <pwberrett@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@toerring.de
\__________________________ http://toerring.de
[toc] | [prev] | [next] | [standalone]
| From | Joshua Landau <joshua.landau.ws@gmail.com> |
|---|---|
| Date | 2013-05-06 17:28 +0100 |
| Message-ID | <mailman.1366.1367857761.3114.python-list@python.org> |
| In reply to | #44727 |
[Multipart message — attachments visible in raw view] — view raw
On 5 May 2013 07:06, peter berrett <pwberrett@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.
>
This is not what you should really be doing. There are specialised things
for this.
Take a look at PIL (http://www.pythonware.com/products/pil/).
It will install as "Image" and from there you can do:
import Image
image = Image.open("FILE")
pixels = image.load()
and access the pixels like:
pixels[x, y] # You get (r, g, b) tuple of integers from 0 to 255
> 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)
>
*Ahem*:
1) Python standard is to only uppercase class (and sometimes module) names,
so it should just be "image".
2) Technically, you mean list.
3) Chose the above method instead, so instead of indexing it with
"image[x][y]" you use "image[x, y]".
Now, you only want the red channel. What this means is that you want to
"split" the image into channels before using image.load():
import Image
image = Image.open("FILE")
image.load() # .open is lazy, so you have to load now
red, green, blue = image.split()
red_pixels = red.load()
And you access as before:
red_pixels[x, y] # You get integer from 0 to 255
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.
>
Changing directories is done with "os.chdir" (
http://docs.python.org/3/library/os.html#os.chdir).
> For the record this is not for homework but is a pet project of mine. I
> have already written a version of the program in Justbasic but python is
> faster. I am also interested in readers views as to which is the simplest
> and best way to achieve what I am trying to do.
>
Thanks for the clarification, btw. Good luck, as well.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web