本文整理匯總了Python中google.appengine.api.images.Image方法的典型用法代碼示例。如果您正苦於以下問題:Python images.Image方法的具體用法?Python images.Image怎麽用?Python images.Image使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類google.appengine.api.images
的用法示例。
在下文中一共展示了images.Image方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get
# 需要導入模塊: from google.appengine.api import images [as 別名]
# 或者: from google.appengine.api.images import Image [as 別名]
def get(self):
blob_key = self.request.get("blob_key")
if blob_key:
blob_info = blobstore.get(blob_key)
if blob_info:
img = images.Image(blob_key=blob_key)
img.resize(width=80, height=100)
img.im_feeling_lucky()
thumbnail = img.execute_transforms(output_encoding=images.JPEG)
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(thumbnail)
return
# Either "blob_key" wasn't provided, or there was no value with that ID
# in the Blobstore.
self.error(404)
# [END thumbnailer]
示例2: get
# 需要導入模塊: from google.appengine.api import images [as 別名]
# 或者: from google.appengine.api.images import Image [as 別名]
def get(self):
if self.request.get("id"):
photo = Photo.get_by_id(int(self.request.get("id")))
if photo:
img = images.Image(photo.full_size_image)
img.resize(width=80, height=100)
img.im_feeling_lucky()
thumbnail = img.execute_transforms(output_encoding=images.JPEG)
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(thumbnail)
return
# Either "id" wasn't provided, or there was no image with that ID
# in the datastore.
self.error(404)
# [END thumbnailer]
示例3: validate_django_image
# 需要導入模塊: from google.appengine.api import images [as 別名]
# 或者: from google.appengine.api.images import Image [as 別名]
def validate_django_image(uploaded_file):
"""Tries to convert an image from Django to an Images API object.
This is essentially a variant of validate_image, above, but for images that
are uploaded to a Django handler (which gives us something different than
webapp2 did). If the image is validated, it returns an Image object (from
the App Engine Images API). Otherwise, it returns False.
"""
try:
image = None
if uploaded_file:
image = images.Image(uploaded_file.read())
image.width
return image
except:
return False
示例4: test_upload_photos_with_transformation
# 需要導入模塊: from google.appengine.api import images [as 別名]
# 或者: from google.appengine.api.images import Image [as 別名]
def test_upload_photos_with_transformation(self):
"""Uploads both profile photo and note photo and verifies the images are
properly transformed and served on the server i.e., jpg is converted to
png and a large image is resized to match MAX_IMAGE_DIMENSION."""
# Create a new person record with a profile photo and a note photo.
with open('tests/testdata/small_image.png') as photo:
with open('tests/testdata/large_image.png') as note_photo:
original_image = images.Image(photo.read())
doc = self.submit_create(photo=photo, note_photo=note_photo)
# Verify the images are uploaded and displayed on the view page.
photos = doc.cssselect('img.photo')
assert len(photos) == 2
# Verify the profile image is converted to png.
doc = self.s.go(photos[0].get('src'))
image = images.Image(doc.content_bytes)
assert image.format == images.PNG
assert image.width == original_image.width
assert image.height == original_image.height
# Verify the note image is resized to match MAX_IMAGE_DIMENSION.
doc = self.s.go(photos[1].get('src'))
image = images.Image(doc.content_bytes)
assert image.format == images.PNG
assert image.width == MAX_IMAGE_DIMENSION
assert image.height == MAX_IMAGE_DIMENSION
示例5: test_skip_thumbnail_for_small_enough_images
# 需要導入模塊: from google.appengine.api import images [as 別名]
# 或者: from google.appengine.api.images import Image [as 別名]
def test_skip_thumbnail_for_small_enough_images(self):
"""Tests that a thumbnail isn't generated for small enough images."""
with open('tests/testdata/tiny_image.png') as image_file:
photo = model.Photo.create('haiti', image_data=image_file.read())
photo.save()
self.go('/haiti/tasks/thumbnail_preparer')
db_photo = model.Photo.get_by_key_name(photo.key().name())
# tiny_image.png is 40x40, so it shouldn't bother generating a
# thumbnail.
assert not db_photo.thumbnail_data
doc = self.s.go('/haiti/photo?id=%s&thumb=true' %
photo.key().name().split(':')[1])
image = images.Image(doc.content_bytes)
assert image.format == images.PNG
assert image.height == 40
assert image.width == 40
示例6: import_image
# 需要導入模塊: from google.appengine.api import images [as 別名]
# 或者: from google.appengine.api.images import Image [as 別名]
def import_image(self, filename, original_filename, bytes, date, email_attachment_content_id):
content_type = self.get_content_type(original_filename)
self.original_size_key = filename
filestore.write(self.original_size_key, bytes, content_type)
MAX_SIZE = 500
image = images.Image(bytes)
if image.width <= MAX_SIZE and image.width <= MAX_SIZE:
logging.info('%s is only %sx%s, no resizing will be done' % (original_filename, image.width, image.height))
resized_bytes = bytes
else:
if image.width > image.height:
new_width = MAX_SIZE
new_height = int(float(MAX_SIZE) / image.width * image.height)
else:
new_height = MAX_SIZE
new_width = int(float(MAX_SIZE) / image.height * image.width)
logging.info('Resizing %s from %sx%s to %sx%s' % (original_filename, image.width, image.height, new_width, new_height))
resized_bytes = images.resize(bytes, MAX_SIZE, MAX_SIZE)
self.serving_size_key = self.get_small_image_name(filename)
filestore.write(self.serving_size_key, resized_bytes, content_type)
self.original_filename = original_filename
self.filename = filename
self.email_attachment_content_id = email_attachment_content_id
self.date = date
示例7: write_blob
# 需要導入模塊: from google.appengine.api import images [as 別名]
# 或者: from google.appengine.api.images import Image [as 別名]
def write_blob(self, data, info):
key = urllib.quote(info['type'].encode('utf-8'), '') +\
'/' + str(hash(data)) +\
'/' + urllib.quote(info['name'].encode('utf-8'), '')
try:
memcache.set(key, data, time=EXPIRATION_TIME)
except: #Failed to add to memcache
return (None, None)
thumbnail_key = None
if IMAGE_TYPES.match(info['type']):
try:
img = images.Image(image_data=data)
img.resize(
width=THUMB_MAX_WIDTH,
height=THUMB_MAX_HEIGHT
)
thumbnail_data = img.execute_transforms()
thumbnail_key = key + THUMB_SUFFIX
memcache.set(
thumbnail_key,
thumbnail_data,
time=EXPIRATION_TIME
)
except: #Failed to resize Image or add to memcache
thumbnail_key = None
return (key, thumbnail_key)
示例8: validate_image
# 需要導入模塊: from google.appengine.api import images [as 別名]
# 或者: from google.appengine.api.images import Image [as 別名]
def validate_image(bytestring):
try:
image = None
if bytestring:
image = images.Image(bytestring)
image.width
return image
except:
return False
示例9: create_photo
# 需要導入模塊: from google.appengine.api import images [as 別名]
# 或者: from google.appengine.api.images import Image [as 別名]
def create_photo(image, repo, url_builder):
"""Creates a new Photo entity for the provided image of type images.Image
after resizing it and converting to PNG. It may throw a PhotoError on
failure, which comes with a localized error message appropriate for
display."""
if image == False: # False means it wasn't valid (see validate_image)
raise FormatUnrecognizedError()
if max(image.width, image.height) <= MAX_IMAGE_DIMENSION:
# No resize needed. Keep the same size but add a transformation to
# force re-encoding.
image.resize(image.width, image.height)
elif image.width > image.height:
image.resize(MAX_IMAGE_DIMENSION,
image.height * MAX_IMAGE_DIMENSION / image.width)
else:
image.resize(image.width * MAX_IMAGE_DIMENSION / image.height,
MAX_IMAGE_DIMENSION)
try:
image_data = image.execute_transforms(output_encoding=images.PNG)
except RequestTooLargeError:
raise SizeTooLargeError()
except Exception:
# There are various images.Error exceptions that can be raised, as well
# as e.g. IOError if the image is corrupt.
raise PhotoError()
photo = model.Photo.create(repo, image_data=image_data)
photo_url = get_photo_url(photo, repo, url_builder)
return (photo, photo_url)
示例10: set_thumbnail
# 需要導入模塊: from google.appengine.api import images [as 別名]
# 或者: from google.appengine.api.images import Image [as 別名]
def set_thumbnail(photo):
"""Sets thumbnail data for a photo.
Args:
photo: the Photo object to set the thumbnail for
"""
image = images.Image(photo.image_data)
if max(image.width, image.height) <= MAX_THUMBNAIL_DIMENSION:
# Don't need a thumbnail, it's small enough already.
return
elif image.width > image.height:
image.resize(MAX_THUMBNAIL_DIMENSION,
image.height * MAX_THUMBNAIL_DIMENSION / image.width)
else:
image.resize(image.width * MAX_THUMBNAIL_DIMENSION / image.height,
MAX_THUMBNAIL_DIMENSION)
try:
thumbnail_data = image.execute_transforms(output_encoding=images.PNG)
except RequestTooLargeError:
raise SizeTooLargeError()
except Exception:
# There are various images.Error exceptions that can be raised, as well
# as e.g. IOError if the image is corrupt.
raise PhotoError()
photo.thumbnail_data = thumbnail_data
photo.save()
示例11: post
# 需要導入模塊: from google.appengine.api import images [as 別名]
# 或者: from google.appengine.api.images import Image [as 別名]
def post(self):
if not (self.auth and self.auth.domain_write_permission):
self.info(
403,
message='Missing or invalid authorization key',
style='plain')
return
# Check for empty body
if not self.request.body:
self.error(400, "Request body must not be empty")
return
# Size check for uploaded file
if len(self.request.body) > PHOTO_UPLOAD_MAX_SIZE:
self.error(400, "Size of uploaded file is greater than 10MB")
return
try:
photo_img = images.Image(self.request.body)
photo, photo_url = create_photo(
photo_img, self.repo, self.transitionary_get_url)
except PhotoError, e:
self.error(400, e.message)
# If we reached this point, it means photo is filled properly
# So feel free to use it right away!
示例12: test_set_thumbnail
# 需要導入模塊: from google.appengine.api import images [as 別名]
# 或者: from google.appengine.api.images import Image [as 別名]
def test_set_thumbnail(self):
"""Tests that a thumbnail is generated."""
with open('tests/testdata/small_image.png') as image_file:
photo = model.Photo.create('haiti', image_data=image_file.read())
photo.save()
self.go('/haiti/tasks/thumbnail_preparer')
doc = self.s.go('/haiti/photo?id=%s&thumb=true' %
photo.key().name().split(':')[1])
image = images.Image(doc.content_bytes)
assert image.format == images.PNG
assert image.height == MAX_THUMBNAIL_DIMENSION
assert image.width == MAX_THUMBNAIL_DIMENSION