Re: Python 3.0 automatic decoding of UTF16

2008-12-05 Thread J Kenneth King
Johannes Bauer [EMAIL PROTECTED] writes:

 Traceback (most recent call last):
   File ./modify.py, line 12, in module
 a = AddressBook(2008_11_05_Handy_Backup.txt)
   File ./modify.py, line 7, in __init__
 line = f.readline()
   File /usr/local/lib/python3.0/io.py, line 1807, in readline
 while self._read_chunk():
   File /usr/local/lib/python3.0/io.py, line 1556, in _read_chunk
 self._set_decoded_chars(self._decoder.decode(input_chunk, eof))
   File /usr/local/lib/python3.0/io.py, line 1293, in decode
 output = self.decoder.decode(input, final=final)
   File /usr/local/lib/python3.0/codecs.py, line 300, in decode
 (result, consumed) = self._buffer_decode(data, self.errors, final)
   File /usr/local/lib/python3.0/encodings/utf_16.py, line 69, in
 _buffer_decode
 return self.decoder(input, self.errors, final)
 UnicodeDecodeError: 'utf16' codec can't decode bytes in position 74-75:
 illegal encoding

It probably means what it says: that the input file contains characters
it cannot read using the specified encoding.

Are you generating the file from python using a file object with the
same encoding? If not, then you might want to look at your input data
and find a way to deal with the exception.
--
http://mail.python.org/mailman/listinfo/python-list


Re: RELEASED Python 3.0 final

2008-12-04 Thread J Kenneth King
Barry Warsaw [EMAIL PROTECTED] writes:

 On behalf of the Python development team and the Python community, I  
 am happy to announce the release of Python 3.0 final.

Yay!

Thanks for all the great work.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python equivalent of Common Lisp Macros?

2008-11-26 Thread J Kenneth King
dpapathanasiou [EMAIL PROTECTED] writes:

 I'm using the feedparser library to extract data from rss feed items.

 After I wrote this function, which returns a list of item titles, I
 noticed that most item attributes would be retrieved the same way,
 i.e., the function would look exactly the same, except for the single
 data.append line inside the for loop.

 In CL, I could simply write a macro, then replace the data.append line
 depending on which attribute I wanted.

 Is there anything similar in Python?

 Here's the function:

 def item_titles (feed_url):
 Return a list of the item titles found in this feed url
 data = []
 feed = feedparser.parse(feed_url)
 if feed:
 if len(feed.version)  0:
 for e in feed.entries:
 data.append(e.title.encode('utf-8'))
 return data

No macros -- need the whole code as data paradigm which doesn't exist
in python.

The pythonic way to create the sort of abstraction you're looking for
would be to use the *attr functions and either a map of attribute
identities or inspect the attributes from the objects.

Some UNTESTED code...

def extract_entry(e, attrs=['title', 'datetime', 'article']):
out = []
for attr in attrs:
assert hasattr(e, attr)
out.append(getattr(e, attr))
return out

Using the inspect module would give an even more generic function, but
with more complex code of course.

Then your code would look more like:

def items(feed_url):
feed = feedparser.feed(feed_url)
if feed and len(feed)  0:
return [extract_entry(entry) for entry in feed.entries]

Of course, I'm making liberal assumptions about what you're looking for
in your ouput here... but it's just an example, not a full
solution. YMMV. Hope it helps. :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: function parameter scope python 2.5.2

2008-11-21 Thread J Kenneth King
alex23 [EMAIL PROTECTED] writes:

 On Nov 21, 9:40 am, J Kenneth King [EMAIL PROTECTED] wrote:
 Of course, providing a shallow (or deep as necessary) copy makes it
 work, I'm curious as to why the value passed as a parameter to a
 function outside the class is passed a reference rather than a copy.

 You're passing neither a reference nor a copy, you're passing the
 object (in this case a list) directly:

 http://effbot.org/zone/call-by-object.htm

Ah, thanks -- that's precisely what I was looking for.

I knew it couldn't be a mistake; I just couldn't find the documentation
on the behaviour since I didn't know what it was called in the python
world.

Cheers.
--
http://mail.python.org/mailman/listinfo/python-list


Re: function parameter scope python 2.5.2

2008-11-21 Thread J Kenneth King
Steven D'Aprano [EMAIL PROTECTED] writes:

 On Thu, 20 Nov 2008 18:31:12 -0500, J Kenneth King wrote:

 Of course I expected that recursive_func() would receive a copy of
 weird_obj.words but it appears to happily modify the object.

 I am curious why you thought that. What made you think Python should/did 
 make a copy of weird_obj.words when you pass it to a function?

 This is a serious question, I'm not trying to trap you into something :)

Don't worry, I don't feel trapped in usenet. ;)

It was more of an intuitive expectation than a suggestion that Python
got something wrong.

I was working on a program of some complexity recently and quickly
caught the issue in my tests. I knew what was going on and fixed it
expediently, but the behaviour confused me and I couldn't find any
technical documentation on it so I figured I just didn't know what it
was referred to in Python. Hence the post. :)

I suppose I have some functional sensibilities and assumed that an
object wouldn't let a non-member modify its properties even if they were
mutable.

Of course if there is any further reading on the subject, I'd appreciate
some links.

Cheers.
--
http://mail.python.org/mailman/listinfo/python-list


Re: function parameter scope python 2.5.2

2008-11-21 Thread J Kenneth King
Peter Pearson [EMAIL PROTECTED] writes:

 On Fri, 21 Nov 2008 10:12:08 -0500, J Kenneth King wrote:
 Steven D'Aprano [EMAIL PROTECTED] writes:

 I am curious why you thought that. What made you think Python should/did 
 make a copy of weird_obj.words when you pass it to a function?
 [snip]
 Of course if there is any further reading on the subject, I'd appreciate
 some links.

 As one relatively new Python fan to another, I recommend following
 this newsgroup.  Many important aspects of Python that several books
 failed to drive through my skull are very clearly (and repeatedly)
 explained here.  Hang around for a week, paying attention to posts
 with subjects like Error in Python subscripts (made-up example),
 and curse me if you don't find it greatly rewarding.

I do lurk more often than I post and sometimes I help out people new to
Python or new to programming in general. I know how helpful usenet can
be and usually this group in particular is quite special. It's good
advice to read before you post; quite often the question has been
proposed and answered long before it came to your little head (not you
in particular; just general you).

In this case, I was simply lacking the terminology to find what I was
looking for on the subject. In such cases turning to the community seems
like a fairly reasonable way to find clarification. I've only been
programming in Python specifically for two years or so now, so I hope I
can be forgiven.

Cheers.
--
http://mail.python.org/mailman/listinfo/python-list


function parameter scope python 2.5.2

2008-11-20 Thread J Kenneth King

I recently encountered some interesting behaviour that looks like a bug
to me, but I can't find the appropriate reference to any specifications
to clarify whether it is a bug.

Here's the example code to demonstrate the issue:

class SomeObject(object):

def __init__(self):
self.words = ['one', 'two', 'three', 'four', 'five']

def main(self):
recursive_func(self.words)
print self.words

def recursive_func(words):
if len(words)  0:
word = words.pop()
print Popped: %s % word
recursive_func(words)
else:
print Done

if __name__ == '__main__':
weird_obj = SomeObject()
weird_obj.main()


The output is:

Popped: five
Popped: four
Popped: three
Popped: two
Popped: one
Done
[]

Of course I expected that recursive_func() would receive a copy of
weird_obj.words but it appears to happily modify the object.

Of course a work around is to explicitly create a copy of the object
property befor passing it to recursive_func, but if it's used more than
once inside various parts of the class that could get messy.

Any thoughts? Am I crazy and this is supposed to be the way python works?
--
http://mail.python.org/mailman/listinfo/python-list


Re: function parameter scope python 2.5.2

2008-11-20 Thread J Kenneth King
J Kenneth King [EMAIL PROTECTED] writes:

 I recently encountered some interesting behaviour that looks like a bug
 to me, but I can't find the appropriate reference to any specifications
 to clarify whether it is a bug.

 Here's the example code to demonstrate the issue:

 class SomeObject(object):

 def __init__(self):
 self.words = ['one', 'two', 'three', 'four', 'five']

 def main(self):
 recursive_func(self.words)
 print self.words

 def recursive_func(words):
 if len(words)  0:
 word = words.pop()
 print Popped: %s % word
 recursive_func(words)
 else:
 print Done

 if __name__ == '__main__':
 weird_obj = SomeObject()
 weird_obj.main()


 The output is:

 Popped: five
 Popped: four
 Popped: three
 Popped: two
 Popped: one
 Done
 []

 Of course I expected that recursive_func() would receive a copy of
 weird_obj.words but it appears to happily modify the object.

 Of course a work around is to explicitly create a copy of the object
 property befor passing it to recursive_func, but if it's used more than
 once inside various parts of the class that could get messy.

 Any thoughts? Am I crazy and this is supposed to be the way python works?

Of course, providing a shallow (or deep as necessary) copy makes it
work, I'm curious as to why the value passed as a parameter to a
function outside the class is passed a reference rather than a copy.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Multiprocessing vs. [Pyro, RPyC]

2008-11-15 Thread J Kenneth King
Jeffrey Barish [EMAIL PROTECTED] writes:

 [EMAIL PROTECTED] wrote:

 
 Jeffrey With the release of multiprocessing in Python 2.6, is there
 any Jeffrey reason to use Pyro or RPyC?
 
 As far as I know the multiprocessing module only works on one machine
 (multi-cpu or multi-core), not across machines.

 So I thought at first, but then I saw this statement in the documentation:

 It is possible to run a manager server on one machine and have clients use
 it from other machines (assuming that the firewalls involved allow it).

Depends. I don't know much about the multiprocessing module in 2.6,
but I have built a distributed application on Pyro.


Pyro has many advantages -- a query-able name server, GUI tools for
monitoring your setup, and remote agents. It is also rather simple in
comparison to other similar tools (*cough*twisted.pb*cough*). However,
it is essentially an RPC style system, so some people might not be too
comfortable with it. YMMV.
--
http://mail.python.org/mailman/listinfo/python-list


Re: duck-type-checking?

2008-11-12 Thread J Kenneth King
Joe Strout [EMAIL PROTECTED] writes:

 Let me preface this by saying that I think I get the concept of
 duck- 
 typing.

 However, I still want to sprinkle my code with assertions that, for
 example, my parameters are what they're supposed to be -- too often I
 mistakenly pass in something I didn't intend, and when that happens, I
 want the code to fail as early as possible, so I have the shortest
 possible path to track down the real bug.  Also, a sufficiently clever
 IDE could use my assertions to know the type of my identifiers, and so
 support me better with autocompletion and method tips.

 So I need functions to assert that a given identifier quacks like a
 string, or a number, or a sequence, or a mutable sequence, or a
 certain class, or so on.  (On the class check: I know about
 isinstance, but that's contrary to duck-typing -- what I would want
 that check to do instead is verify that whatever object I have, it has
 the same public (non-underscore) methods as the class I'm claiming.)

 Are there any standard methods or idioms for doing that?

 Thanks,
 - Joe

I generally use the 'assert' keyword when I'm in a rush otherwise unit
tests generally catch this kind of thing.
--
http://mail.python.org/mailman/listinfo/python-list


Re: best python unit testing framwork

2008-11-11 Thread J Kenneth King
Brendan Miller [EMAIL PROTECTED] writes:

 What would heavy python unit testers say is the best framework?

 I've seen a few mentions that maybe the built in unittest framework
 isn't that great. I've heard a couple of good things about py.test and
 nose. Are there other options? Is there any kind of concensus about
 the best, or at least how they stack up to each other?

 Brendan

I find nose to be the best. It's simple, easy, and doesn't sacrifice
power. All good things if you value your time. :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Daemon and logging - the best approach?

2008-11-07 Thread J Kenneth King
Lech Karol Pawłaszek [EMAIL PROTECTED] writes:

 Hello.

 I'm trying to make a daemon and I want to log to a file its activity.
 I'm using logging module with a configuration file for it (loaded via
 fileConfig()).

 And now I want to read logging config file before daemonize the program
 because the file might not be accessible after daemonization. OTOH while
 daemonizing I am closing all opened file descriptors - including those
 opened for logging.

 What is the best approach to handle such situation? Should I close all
 FD before daemonizing or just forget it? Any other hints?

 Kind regards,

Depends.

For *NIX systems, it's a good idea to use the syslog daemon for logging
where it's available.

I believe the logging module can be configured to log to the local
syslog daemon.

Cheers.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python suitable for Midi ?

2008-10-29 Thread J Kenneth King
Derek Martin [EMAIL PROTECTED] writes:

 On Tue, Oct 28, 2008 at 06:54:57PM +0200, Chuckk Hubbard wrote:
 The problem I've run into is that I can't set the audio to a higher
 priority than the GUI (Tkinter).  If I move the mouse over the app, no
 matter what, I get audio dropouts.  AFAICT this is the same for all
 Python, regardless of what modules one uses: you can't assign system
 priorities to different threads.  If you're planning to pipe MIDI to
 another app for playback, maybe it won't be an issue for you.

 FWIW...  You could take your own advice, and devide your application
 in two: one process manages the GUI, and the second is a back-end
 process that plays the MIDI.  Your GUI can even launch the back end,
 which will inherit the priority of the GUI, after which the GUI can
 reduce its own priority (the priority of the back end will not be
 affected by the change)...


 -- 
 Derek D. Martin
 http://www.pizzashack.org/
 GPG Key ID: 0x81CFE75D

One also has access to nice-levels on unix systems.
--
http://mail.python.org/mailman/listinfo/python-list


Re: python extensions: including project local headers

2008-10-24 Thread J Kenneth King
Philip Semanchuk [EMAIL PROTECTED] writes:

 On Oct 23, 2008, at 3:18 PM, J Kenneth King wrote:

 Philip Semanchuk [EMAIL PROTECTED] writes:

 On Oct 23, 2008, at 11:36 AM, J Kenneth King wrote:


 Hey everyone,

 I'm working on a python extension wrapper around Rob Hess'
 implementation of a SIFT feature detector. I'm working on a
 computer-vision based project that requires interfacing with
 Python at
 the higher layers, so I figured the best way to handle this would be
 in
 C (since my initial implementation in python was ungodly and slow).

 I can get distutils to compile the extension and install it in the
 python path, but when I go to import it I get the wonderful
 exception:

 ImportError: /usr/lib/python2.5/site-packages/pysift.so: undefined
 symbol: _sift_features


 Kenneth,
 You're close but not interpreting the error quite correctly. This
 isn't an error from the compiler or preprocessor, it's a library
 error. Assuming this is dynamically linked, your OS is reporting
 that,
 at runtime, it can't find the library that contains _sift_features.
 Make sure that it's somewhere where your OS can find it.

 This is basically what I was looking for help with. So far the project
 directory is:

 /pysift
  /sift
..
/include
  ..
  sift.h
/src
  ..
  sift.c
  /src
pysift.c
  setup.py

 I thought I could just #include sift.h in pysift.c as long as
 distutils passed the right -I path to gcc.

 That's true, and it sounds like you've got that part working.


 Maybe I should compile the sift code as a shared object and link it to
 my extension? How would I get distutils to build the makefile and tell
 gcc how to link it?

 Thanks for the reply. Python has spoiled me and my C is rather
 rusty. :)

 I don't know how to get setup.py to build a shared object separately.
 I am in the same Python/C situation as you. I'm scrubbing the rust off
 of my C skills and I'm also a n00b at developing extensions. I've
 learned a lot from looking at other people's setup code, so maybe I
 can help you there.

 My posix_ipc module links to the realtime lib rt and here's the
 relevant snippets of setup.py:

 --
 import distutils.core as duc

 libraries = [ ]

 libraries.append(rt)

 source_files = [posix_ipc_module.c]

 ext_modules = [ duc.Extension(posix_ipc,
   source_files,
   define_macros=define_macros,
   libraries=libraries
  )
   ]

 duc.setup(name=posix_ipc, version=VERSION, ext_modules=ext_modules)

 --

 You can download the whole thing here if you want to examine all the
 code:
 http://semanchuk.com/philip/posix_ipc/

 HTH
 Philip

I'll take a look, thanks! :)
--
http://mail.python.org/mailman/listinfo/python-list


python extensions: including project local headers

2008-10-23 Thread J Kenneth King

Hey everyone,

I'm working on a python extension wrapper around Rob Hess'
implementation of a SIFT feature detector. I'm working on a
computer-vision based project that requires interfacing with Python at
the higher layers, so I figured the best way to handle this would be in
C (since my initial implementation in python was ungodly and slow). 

I can get distutils to compile the extension and install it in the
python path, but when I go to import it I get the wonderful exception:

ImportError: /usr/lib/python2.5/site-packages/pysift.so: undefined
symbol: _sift_features

Of course, _sift_features is a function defined in his header that I'm
#including in my extension.

His sources are sitting in my project root under sift/ while my source
is under src/ -- My setup.py is as follows:

[code]

from distutils.core import setup, Extension

pysift = Extension('pysift',
   include_dirs = ['sift/include'],
   sources = ['src/pysift.c'],
   extra_link_args = ['-lm', '-lcv', '-lcxcore',
  '-lhighgui', '-lcvaux'])

setup(name = 'pysift',
  version = '0.0',
  description = 'A SIFT feature detection package',
  author = 'James Kenneth King',
  author_email = [EMAIL PROTECTED],
  url = http://agentultra.com/;,
  long_description = 
  A python extension package for detecting SIFT
  features using Rob Hess' C implementation.

  http://web.engr.oregonstate.edu/~hess/

  Original SIFT feature descriptors by David Lowe
  and patented by the University of British Columbia.
  ,
  ext_modules = [pysift])

[/code]

And the include to Rob's header file is on the second line of pysift.c:

#include sift.h

The weird thing (to me in my somewhat hackish knowledge of C) is that I
can use all the #defines from sift.h with no complaints from the
preprocessor (in fact, there are no complaints at all from the compiler
when compiling the extension module).

Once I get this bugger working, I'll be setting up a project page to
share sources and will also be releasing extension wrappers to the
OpenCV libraries.

I've never released any code before so any help getting this right and
proper for the community would be greatly appreciated.

Cheers.
--
http://mail.python.org/mailman/listinfo/python-list


Re: python extensions: including project local headers

2008-10-23 Thread J Kenneth King
Robert Kern [EMAIL PROTECTED] writes:

 Philip Semanchuk wrote:

 On Oct 23, 2008, at 11:36 AM, J Kenneth King wrote:


 Hey everyone,

 I'm working on a python extension wrapper around Rob Hess'
 implementation of a SIFT feature detector. I'm working on a
 computer-vision based project that requires interfacing with Python at
 the higher layers, so I figured the best way to handle this would be in
 C (since my initial implementation in python was ungodly and slow).

 I can get distutils to compile the extension and install it in the
 python path, but when I go to import it I get the wonderful exception:

 ImportError: /usr/lib/python2.5/site-packages/pysift.so: undefined
 symbol: _sift_features


 Kenneth,
 You're close but not interpreting the error quite correctly. This
 isn't an error from the compiler or preprocessor, it's a library
 error. Assuming this is dynamically linked, your OS is reporting
 that, at runtime, it can't find the library that contains
 _sift_features. Make sure that it's somewhere where your OS can find
 it.

 It looks like the library implementing it was not linked into the
 extension. sift_features() is not part of OpenCV.

 James, are you including the source of Rob Hess's implementation with
 your extension, or are you trying to link against an already installed
 version of the library? If the former, you need to add the C sources
 to the pysift Extension(). If the latter, you need to add the name of
 the library to the list of libraries.

I'm including Rob Hess' sources with the extension.

Would that mean I should add library_dirs to Extension() to point to the
sources in the project's path?

 Also, you don't want to pass the list of libraries with
 extra_link_args. Instead, use libraries=.

 pysift = Extension('pysift',
include_dirs = ['sift/include'],
sources = ['src/pysift.c'],
libraries = ['feat', 'cv', 'cxcore', 'highgui',
 'cvaux', 'm'])

Thanks for taking a moment to help me out. :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: python extensions: including project local headers

2008-10-23 Thread J Kenneth King
Philip Semanchuk [EMAIL PROTECTED] writes:

 On Oct 23, 2008, at 11:36 AM, J Kenneth King wrote:


 Hey everyone,

 I'm working on a python extension wrapper around Rob Hess'
 implementation of a SIFT feature detector. I'm working on a
 computer-vision based project that requires interfacing with Python at
 the higher layers, so I figured the best way to handle this would be
 in
 C (since my initial implementation in python was ungodly and slow).

 I can get distutils to compile the extension and install it in the
 python path, but when I go to import it I get the wonderful exception:

 ImportError: /usr/lib/python2.5/site-packages/pysift.so: undefined
 symbol: _sift_features


 Kenneth,
 You're close but not interpreting the error quite correctly. This
 isn't an error from the compiler or preprocessor, it's a library
 error. Assuming this is dynamically linked, your OS is reporting that,
 at runtime, it can't find the library that contains _sift_features.
 Make sure that it's somewhere where your OS can find it.

This is basically what I was looking for help with. So far the project
directory is:

/pysift
  /sift
..
/include
  ..
  sift.h
/src
  ..
  sift.c
  /src
pysift.c
  setup.py

I thought I could just #include sift.h in pysift.c as long as
distutils passed the right -I path to gcc.

Maybe I should compile the sift code as a shared object and link it to
my extension? How would I get distutils to build the makefile and tell
gcc how to link it?

Thanks for the reply. Python has spoiled me and my C is rather
rusty. :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: PYTHON WORKING WITH PERL ??

2008-10-16 Thread J Kenneth King
Pat [EMAIL PROTECTED] writes:

 Sean DiZazzo wrote:
 On Sep 29, 12:44 pm, Blubaugh, David A. [EMAIL PROTECTED]
 wrote:
 Sir,

 You are absolutely correct.  I was praying to G_d I did not have to
 slaughter my project's source code in this manner.  However, like life
 itself, I was given legacy source code (i.e. someone else errors to fix)
 in Perl.  However, I have just found out that there is a way to import
 the Perl interpreter within Python!!!  I now believe I can utilize
 python as the main platform to develop the project upon !!  

 Thanks,

 David

 -Original Message-
 From: D'Arcy J.M. Cain [mailto:[EMAIL PROTECTED]
 Sent: Monday, September 29, 2008 1:32 PM
 To: Blubaugh, David A.

 Cc: [EMAIL PROTECTED]
 Subject: Re: PYTHON WORKING WITH PERL ??

 On Mon, 29 Sep 2008 13:16:14 -0400
 Blubaugh, David A. [EMAIL PROTECTED] wrote:
 I was wondering if it was possible to have a situation where a
 programming project would utilized BOTH python and perl?  Such as
 utilizing python for internet programming and then utilize perl for
 text processing and systems programming?  Is this even feasible???
 I don't see why not but I also question if it is a good idea.  Once you
 have all your objects and low level methods written in Python it just
 makes sense to re-use them rather than trying to duplicate the
 functionality in another language.

 Of course, sometimes we don't have control over our entire environment
 so yes, you can mix them if you have to.



 Rewrite everything in python.  Save yourself now...while you still
 can.

 ~Sean

 Trust me. Sean is absolutely correct. I'm currently in the process of
 converting a large Perl project to Python (and learning Python at the
 same time) and the improvement in code is incredible.  After you learn
 Python, you'll come to despise Perl.

Depends on the person -- I still love Perl, but program in Python
every day at work.

Python is great, but don't be mistaken: it's not the one language to
rule them all. No language is (except maybe Lisp).

But yay for converting a project to python. :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: PYTHON WORKING WITH PERL ??

2008-10-16 Thread J Kenneth King
Joshua Kugler [EMAIL PROTECTED] writes:

 Pat wrote:
 Rewrite everything in python.  Save yourself now...while you still
 can.
 
 ~Sean
 
 Trust me. Sean is absolutely correct. I'm currently in the process of
 converting a large Perl project to Python (and learning Python at the
 same time) and the improvement in code is incredible.  After you learn
 Python, you'll come to despise Perl.

 I'm not a Python fan-boy, but I'm going to have to agree with Sean too.  I
 had been using Perl on-and-off for some 10 years, when I finally had to
 write a 500-ish line script in Perl for some data processing.  It got messy
 in a hurry.  Is this a hash? Is it a reference to a hash? Is it a reference
 to a hash of array references?  Gaaah!  I rewrote it in Python, and it was
 so much easier to keep all my data structures straight.

 j

Perl just gives you a lot of rope.

You can do a lot of neat things with rope. ;)
--
http://mail.python.org/mailman/listinfo/python-list


Re: PIL problem

2008-10-08 Thread J Kenneth King
bfrederi [EMAIL PROTECTED] writes:

 I am having a problem using PIL. I am trying to crop and image to a
 square, starting from the center of the image, but when I try to crop
 the image, it won't crop. Here are the relevant code snippets:

 ### Function I am testing ###
 def create_square_image(file_name):
  Creates a thumbnail sized image and turns it into a square 
 image = Image.open(open(file_name))

 size_tuple = image.size
 width = size_tuple[0]
 height = size_tuple[1]

 square_length = 75

 x1 = (width / 2) - (square_length / 2)
 x2 = x1 + square_length
 y1 = (height / 2) - (square_length / 2)
 y2 = y1 + square_length

 image.crop((x1,y1,x2,y2))
 image.save(file_name, JPEG)

def create_square_image(filename, size=75):
file = open(filename, 'rb')
image = Image.open(file)
w, h = image.size

x1 = (w / 2) - (size / 2)
x2 = x1 + size
y1 = (h / 2) - (size / 2)
y2 = y1 + size

thumb = image.crop((x1,y1,x2,y2))
thumb.save(thumb_ + filename, JPEG)

...

of course PIL has a thumbnail method that does this type of stuff.
--
http://mail.python.org/mailman/listinfo/python-list


<    1   2