3008/2010

2608/2010

0908/2010

2707/2010

Priya checked in at Phoenix Worldgate 9 at 4:55 p.m.

2007/2010

1107/2010

Priya checked in at near Amberwinds Ct & Summerwinds Dr at 9:37 a.m.

1007/2010

0107/2010

Just back from wapo labs HTML 5 talk. Few more sessions to come. Today enjoyed the history of HTML. from web at 10:09 a.m.

2706/2010

On the way to pittsburgh. It's fun to travel with my cute little cousin. from Twitter for iPhone at 4:02 a.m.

1506/2010

Priya checked in at District of Columbia at 2:10 p.m.

0806/2010

Priya checked in at Uncle Julio's Rio Grande Cafe at 5:33 p.m.

thanks to @amazon. I'm alive now...was really frustrated as none of the stores has 85w mac power adapter...including apple store... from web at 11:36 a.m.

2805/2010

Priya checked in at San Francisco International Airport at 6:46 a.m.

2305/2010

@8en Just found a flaw in your setup. Hire me as a security consultant. Although its fixed now. from web at 11:01 a.m.

1905/2010

PIL Image Handling


at 8:39 p.m. Be the first to comment.

Just playing around with Tornado Web Server and trying to make a twitter clone with realtime updates. I wanna share the ImageHandling snippet which requires Python Imaging Library module to resize images. BTW for PIL installation there are many valuable resources available on the web.

I will get the image from the form and the imghdr module is used to sniff the content type of the file. In this case, jpeg and png are the accepted file types. Here comes the interesting part, Instead of writing the image to the disk I am using StringIO module which reads the image from the memory. By this we are simulating a file object as expected by Image.open.

I want the image to be in three different sizes.
1. avatar_square the largest square of the uploaded image.
2. thumbnail size (48*48).
3. mininail size (24*24) to show the followers on the side as twitter.

class ImageHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        return self.render("image.html")

    @tornado.web.authenticated
    def post(self):
        user_id=self.current_user
        username = self.db.get("SELECT username from user WHERE id=%s", user_id)
        uploaded_image=self.request.files
        if imghdr.what('ignore', uploaded_image['avatar'][0]['body']) in ['jpeg','png']:
            avatar = Image.open(StringIO.StringIO(uploaded_image['avatar'][0]['body']))
            width, height = avatar.size
                        if width > height:
                            delta = width - height
                            left = int(delta/2)
                            upper = 0
                            right = height + left
                            lower = height
                        else:
                            delta = height - width
                            left = int(delta)/2
                            upper = 0
                            right = width
                            lower = width + upper
                        avatar_square = avatar.crop((left, upper, right, lower))
                        avatar_mini = copy.copy(avatar_square)
                        avatar_square.thumbnail((48,48))
                        avatar_mini.thumbnail((24,24))
                        return self.write("Successfully uploaded the image")
        else: return self.write("file format is not accepted")

gist is here

read more

0705/2010

Learning cost of mysql replication on Amazon spot instances is 6cents. from Tweetie at 8:06 p.m.

0505/2010

0305/2010

2204/2010

Priya checked in at Digg Inc at 6:25 p.m.

1404/2010

Priya checked in at San Jose at noon

Archives

Hi, this is Priya. This is my tumblelog, a reverse chronological order of all my social activity on the web.

September 2010
Su Mo Tu We Th Fr Sa
25 26 27 28 29 30 31
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 1 2 3 4