[Numpy-discussion] convert array to tuples?

2011-04-05 Thread Christopher Barker
HI folks,

What's the fastest way to convert a numpy array to tuples?

Unfortunately, not all packages take arbitrary sequences when they are 
expecting a list of tuples. In this case, I'm using PIL's ImageDraw, and 
want to do:

draw.line(points)

(points is an Nx2 numpy array of ints)

but if I pass in an array, I get, oddly enough, nothing. NO error, just 
nothing drawn.

if I do:

draw.line(points.tolist())

SystemError: new style getargs format but argument is not a tuple

So I seem to need tuples. This works:

draw.line([tuple(p) for p in points.tolist()])

but that seems like it would be slower than it should be (to be honest, 
not profiled).

Is there a faster way?

Maybe numpy should have a ndarray.totuple() method.

-Chris






-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/ORR(206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115   (206) 526-6317   main reception

chris.bar...@noaa.gov
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] convert array to tuples?

2011-04-05 Thread Sebastian Haase
On Tue, Apr 5, 2011 at 7:02 PM, Christopher Barker
chris.bar...@noaa.gov wrote:
 HI folks,

 What's the fastest way to convert a numpy array to tuples?

 Unfortunately, not all packages take arbitrary sequences when they are
 expecting a list of tuples. In this case, I'm using PIL's ImageDraw, and
 want to do:

 draw.line(points)

 (points is an Nx2 numpy array of ints)

 but if I pass in an array, I get, oddly enough, nothing. NO error, just
 nothing drawn.

 if I do:

 draw.line(points.tolist())

 SystemError: new style getargs format but argument is not a tuple

 So I seem to need tuples. This works:

 draw.line([tuple(p) for p in points.tolist()])

 but that seems like it would be slower than it should be (to be honest,
 not profiled).

 Is there a faster way?

 Maybe numpy should have a ndarray.totuple() method.

 -Chris

How about fixing PIL... I know that there is not a good track record
of getting patches into PIL ,
but did you get to the bottom of it and find how draw.line is implemented?
BTW, is it drawing anti-aliased lines ?

- Sebastian Haase
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] convert array to tuples?

2011-04-05 Thread josef . pktd
On Tue, Apr 5, 2011 at 1:02 PM, Christopher Barker
chris.bar...@noaa.gov wrote:
 HI folks,

 What's the fastest way to convert a numpy array to tuples?

 Unfortunately, not all packages take arbitrary sequences when they are
 expecting a list of tuples. In this case, I'm using PIL's ImageDraw, and
 want to do:

 draw.line(points)

 (points is an Nx2 numpy array of ints)

 but if I pass in an array, I get, oddly enough, nothing. NO error, just
 nothing drawn.

 if I do:

 draw.line(points.tolist())

 SystemError: new style getargs format but argument is not a tuple

 So I seem to need tuples. This works:

 draw.line([tuple(p) for p in points.tolist()])

 but that seems like it would be slower than it should be (to be honest,
 not profiled).

 Is there a faster way?

 Maybe numpy should have a ndarray.totuple() method.

does a structured dtype work?

Josef


 -Chris






 --
 Christopher Barker, Ph.D.
 Oceanographer

 Emergency Response Division
 NOAA/NOS/ORR            (206) 526-6959   voice
 7600 Sand Point Way NE   (206) 526-6329   fax
 Seattle, WA  98115       (206) 526-6317   main reception

 chris.bar...@noaa.gov
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] convert array to tuples?

2011-04-05 Thread Mark S Niemczyk
Just a suggestion (I am very new to Numpy), but wouldn't

draw.line(tuple(points.tolist()))

accomplish what you want.

Based on my reading, the ndarray.tolist method would return a Python list of 
elements; then Python's built-in tuple function would complete the conversion.


Regards,

Mark


On Apr 5, 2011, at 12:02 PM, Christopher Barker wrote:

 HI folks,
 
 What's the fastest way to convert a numpy array to tuples?
 
 Unfortunately, not all packages take arbitrary sequences when they are 
 expecting a list of tuples. In this case, I'm using PIL's ImageDraw, and 
 want to do:
 
 draw.line(points)
 
 (points is an Nx2 numpy array of ints)
 
 but if I pass in an array, I get, oddly enough, nothing. NO error, just 
 nothing drawn.
 
 if I do:
 
 draw.line(points.tolist())
 
 SystemError: new style getargs format but argument is not a tuple
 
 So I seem to need tuples. This works:
 
 draw.line([tuple(p) for p in points.tolist()])
 
 but that seems like it would be slower than it should be (to be honest, 
 not profiled).
 
 Is there a faster way?
 
 Maybe numpy should have a ndarray.totuple() method.
 
 -Chris
 
 
 
 
 
 
 -- 
 Christopher Barker, Ph.D.
 Oceanographer
 
 Emergency Response Division
 NOAA/NOS/ORR(206) 526-6959   voice
 7600 Sand Point Way NE   (206) 526-6329   fax
 Seattle, WA  98115   (206) 526-6317   main reception
 
 chris.bar...@noaa.gov
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] convert array to tuples?

2011-04-05 Thread Christopher Barker
On 4/5/11 10:52 AM, Sebastian Haase wrote:
 How about fixing PIL... I know that there is not a good track record
 of getting patches into PIL ,
 but did you get to the bottom of it and find how draw.line is implemented?

no. I haven't. And yes adapting PIL to use the buffer interface for this 
kind of thing would be great. I also have wanted to do that for 
wxPython, where it would be welcome, but still haven't gotten around to it.

 BTW, is it drawing anti-aliased lines ?

Not with the ImageDraw module, but there is a new AggDraw module that 
should be more pretty.

On 4/5/11 11:49 AM, Mark S Niemczyk wrote:
 Just a suggestion (I am very new to Numpy), but wouldn't

   draw.line(tuple(points.tolist()))

 accomplish what you want.

nope, tolist() creates a list of lists -- tuple() only converts the 
outer list to a tuple, which I why I did the list comprehension.


On 4/5/11 10:53 AM, josef.p...@gmail.com wrote:
 does a structured dtype work?

Good idea -- I'll give it a try -- indeed it does:

dt = np.dtype([('x', np.int32),('y',np.int32)])

draw.line(points.view(dtype=dt).reshape((-1,)).tolist(), 
fill=rgb(0,255,0), width=4)

but it's not any faster, alas.

The fastest I've come up with is putting the elements in a single 1-d 
list. PIL can take either a sequence of tuples:

[(x,y),(x,y),...]

or the flattened version:

[x,y,x,y,x,y,x,y,...]

So this is the fastest:

draw.line(points.flatten().tolist(), fill=rgb(0,255,0), width=4)


Thanks all,

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/ORR(206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115   (206) 526-6317   main reception

chris.bar...@noaa.gov
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion