Archive
screenshot.py
I made a simple wrapper script called screenshot.py that calls phantomjs and convert to do the real job. The advantage of screenshot.py is its very simple usage.
Usage
screenshot.py -full http://reddit.com full.jpg
Screenshot of the entire page (can be very high).
screenshot.py -window http://reddit.com window.jpg
Screenshot of the area that you see in the browser.
screenshot.py -thumb http://reddit.com thumb.jpg
Thumbnail of the area that you see in the browser.
Links
- sample screenshots
- project’s page: https://github.com/jabbalaci/screenshot.py
- this project of mine appeared in ImportPython Weekly Newsletter – Issue No 39
Face detection with Python
Today I found an interesting post: Face Detection in Static Images with Python. You give an image as input and the program draws a rectangle around the face(s) on the image.
Installation
sudo apt-get install python-opencv libcv-dev opencv-doc
Under Ubuntu 11.04, the required XML is in the opencv-doc package at a different location, thus you need to modify the source code:
...
cascade = cvLoadHaarClassifierCascade(
'/usr/share/doc/opencv-doc/examples/haarcascades/haarcascades/haarcascade_frontalface_default.xml.gz',
cvSize(1,1))
...
See the full (patched) source code here.
Lesson learned
You can draw a rectangle on an image the following way:
convert original.jpg -stroke red -fill none -draw "rectangle 50,36 115,101" output.jpg
Where 50,36 is the top left, 115,101 is the bottom right coordinates.
/ @reddit /
unicode to ascii
Problem
I had the following unicode string: “Kellemes Ünnepeket!” that I wanted to simplify to this: “Kellemes Unnepeket!”, that is strip “Ü” to “U”. Furthermore, most of the strings were normal ascii, only some of them were in unicode.
Solution
import unicodedata
title = ... # get the string somehow
try:
# if the title is a unicode string, normalize it
title = unicodedata.normalize('NFKD', title).encode('ascii','ignore')
except TypeError:
# if it was not a unicode string => OK, do nothing
pass
Credits
I used the following resources:
- http://www.peterbe.com/plog/unicode-to-ascii
- http://stackoverflow.com/questions/196345/how-to-check-if-a-string-in-python-is-in-ascii
