Crop existent image in GCS

76 views
Skip to first unread message

Handerson Contreras

unread,
Jun 14, 2016, 6:36:39 PM6/14/16
to Google App Engine
Hello,

I want to know how can i crop an existent image in gcs and save it?

until now, i have achieved to crop the image by using 

img = images.Image(filename=obj_fileinfo.gs_object_name)
img.crop(0.0, 0.0, (200.0 / 300.0), (150.0 / 300.0))
img.im_feeling_lucky()
thumbnail = img.execute_transforms(output_encoding=images.JPEG)

and serve the image 

self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(thumbnail)

but, I need to create an url to serve the image already cropped. I suppose that i need to save the cropped image in GCS and create it a serving url, but i don't know how can i do this. 

Can someone help me, please?


Ayush Verma

unread,
Jun 16, 2016, 8:45:29 AM6/16/16
to Google App Engine
You do not need to save the cropped image.
What I would suggest is that, you save the original image and get a serving url for it using  getServingUrl() method (for java).

You can resize and crop the image dynamically by specifying the arguments in the URL. The available arguments are:

  • =sxx where xx is an integer from 0–1600 representing the length, in pixels, of the image's longest side. For example, adding =s32 resizes the image so its longest dimension is 32 pixels.
  • =sxx-c where xx is an integer from 0–1600 representing the cropped image size in pixels, and -c tells the system to crop the image.
# Resize the image to 32 pixels (aspect-ratio preserved)
http://lhx.ggpht.com/randomStringImageId=s32

# Crop the image to 32 pixels
http://lhx.ggpht.com/randomStringImageId=s32-c
There are many more transformations possible. Check them out here.

P.S this probably doesn't exactly answer your question, but how you get the serving url is runtime dependent. For java, the documentation is here.

How its done in java. (gcsFilename would be your image stored in GCS)
public static String getServingURL(GcsFilename gcsFilename) {

 
final String bucketName = gcsFilename.getBucketName();
 
final String objectName = gcsFilename.getObjectName();

 
final ServingUrlOptions servingUrlOptions =
 
ServingUrlOptions.Builder.withGoogleStorageFileName("/gs/" + bucketName + "/" + objectName);
 
return ImagesServiceFactory.getImagesService().getServingUrl(servingUrlOptions);
}

Reply all
Reply to author
Forward
0 new messages