I need to check the dimensions of images in a directory. Currently it has ~700 images. I just need to check the sizes, and if the size does not match a given dimension, it will be moved to a different folder. How do I get started? Answer You can use the Python Imaging Library (aka PIL) to read the image
Tag: python
match an alternative url – regular expression django urls
I want a Django URL with just 2 alternatives /module/in/ or /module/out/ I’m using But it matches other patterns like /module/i/, /module/n/ and /module/ou/. Any hint is appreciated :) Answer Try r’^(?P<status>in|out)/$’ You need to remove w+, which matches one or more alphanumeric cha…
Log output of multiprocessing.Process
Is there a way to log the stdout output from a given Process when using the multiprocessing.Process class in python? Answer The easiest way might be to just override sys.stdout. Slightly modifying an example from the multiprocessing manual: And running it: $ ls m.py $ python m.py $ ls 27493.out 27494.out m.py…
How can I implement decrease-key functionality in Python’s heapq?
I know it is possible to realize decrease-key functionality in O(log n) but I don’t know how? Answer To implement “decrease-key” effectively, you’d need to access the functionality “decrement this element AND swap this element with a child until heap condition is restore”. …
Format floats with standard json module
I am using the standard json module in python 2.6 to serialize a list of floats. However, I’m getting results like this: I want the floats to be formated with only two decimal digits. The output should look like this: I have tried defining my own JSON Encoder class: This works for a sole float object: B…
fcntl substitute on Windows
I received a Python project (which happens to be a Django project, if that matters,) that uses the fcntl module from the standard library, which seems to be available only on Linux. When I try to run it on my Windows machine, it stops with an ImportError, because this module does not exist here. Is there any …
Is there a function to determine which quarter of the year a date is in?
Sure I could write this myself, but before I go reinventing the wheel is there a function that already does this? Answer Given an instance x of datetime.date, (x.month-1)//3 will give you the quarter (0 for first quarter, 1 for second quarter, etc — add 1 if you need to count from 1 instead;-). Original…
How do I determine if my python shell is executing in 32bit or 64bit?
How can I tell what mode the shell is in, from within the shell? I’ve tried looking at the platform module, but it seems only to tell you about “the bit architecture and the linkage format used for the executable”. My binary is compiled as 64bit (I’m running on OS X 10.6), so it seems …
The Assignment Problem, a NumPy function?
Since an assignment problem can be posed in the form of a single matrix, I am wondering if NumPy has a function to solve such a matrix. So far I have found none. Maybe one of you guys know if NumPy/SciPy has an assignment-problem-solve function? Edit: In the meanwhile I have found a Python (not NumPy/SciPy) i…
Calculating a directory’s size using Python?
Before I re-invent this particular wheel, has anybody got a nice routine for calculating the size of a directory using Python? It would be very nice if the routine would format the size nicely in Mb/Gb etc. Answer This walks all sub-directories; summing file sizes: And a oneliner for fun using os.listdir (Doe…