English 中文(简体)
GAE images.resize with fixed proportional crop
原标题:

I need to resize and crop to exactly 60x80px from various size and aspect ratio. Just before i put into Datastore. Anyone already got this issue resolved.

Currently i already succed to just transform it to exact height (80px) with various width which nott look so good when i try to display it on a list. e.g jcaroussel.

My db.put code is like bellow:

    if users.get_current_user():
        personal.personal_id = int(self.request.get( personal_id ))
        personal.name = self.request.get( name )
        personal.latitude = self.request.get( latitude )
        personal.info = self.request.get( info )
        photo = images.resize(self.request.get( img ), 0, 80)
        personal.photo = db.Blob(photo)
        personal.lc_id = int(self.request.get( lc_id ))
        personal.put()
        self.redirect( /admin/personal )

    else:
      self.response.out.write( I m sorry, you don t have permission to add this LP Personal Data. )

I just want to do similar result when we upload our avatar on google talk/google chat.

Anyone solved this?

Thx

最佳回答

I used something else:

  • Resize the original image to your max height (80)
  • Store the resized (but complete/not cropped) image
  • Display it inside a <div> that has the following CSS: width: 60px; height: 80px; overflow: hidden;

That way it will show nicely in your list, but you can still display the complete resized picture on your user s profile page (looking at you code I imagine that s what you are trying to do, right?)

问题回答

After your resize your image down to 80 pixels in height, you would have to use the crop function as defined here. For example:

img = images.Image(self.request.get( img ))
img.resize(0, 80)
resized_img = img.execute_transforms(output_encoding=images.JPEG)
left_x = (resized_img.width - 60) / 2
resized_img.crop(left_x, 0, left_x + 60, 80)
cropped_img = resized_image.execute_transforms(output_encoding=images.JPEG)

In my example it crops to the center of the image. It assumes that the resized image is at least 60 pixels wide, but obviously you would have to add some checks to confirm this, because a user might not upload an image in the right size.





相关问题
How to make logging.debug work on Appengine?

I m having a tough time getting the logging on Appengine working. the statement import logging is flagged as an unrecognized import in my PyDev Appengine project. I suspected that this was just an ...

gqlQuery returns object, want list of keys

Is there a way to convert the GqlQuery object to an array of keys, or is there a way to force the query to return an array of keys? For example: items = db.GqlQuery("SELECT __key__ FROM Items") ...

Integrating Google AppEngine with a Thick Client

I want to make a multi-user client-server solution with Java Swing thick client as a front-end and Google AppEngine (Java one) as a back-end. The problem is that GAE provides only web-based forms for ...

sorl.thumbnail : thumbnail is not a valid tag library?

I am trying to install sorl.thumbnail but am getting the following error message: thumbnail is not a valid tag library: Could not load template library from django.templatetags.thumbnail, No module ...

热门标签