本文整理汇总了Python中models.Image.create方法的典型用法代码示例。如果您正苦于以下问题:Python Image.create方法的具体用法?Python Image.create怎么用?Python Image.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Image
的用法示例。
在下文中一共展示了Image.create方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: transfer_s3_structure_to_db
# 需要导入模块: from models import Image [as 别名]
# 或者: from models.Image import create [as 别名]
def transfer_s3_structure_to_db(name, path, parent_directory, is_dir=False):
if parent_directory is not None:
par_dir = Directory.get(Directory.key == parent_directory)
else:
par_dir = FakeDirectory()
par_dir.id = None
if is_dir:
try:
Directory.get(Directory.key == path)
return False
except DoesNotExist:
Directory.create(
name = name,
key = path,
parent_directory = par_dir.id
)
return True
else:
try:
Image.get(Image.key == path)
return False
except DoesNotExist:
Image.create(
name = name,
key = path,
directory = par_dir.id
)
return True
示例2: test_blob_creation
# 需要导入模块: from models import Image [as 别名]
# 或者: from models.Image import create [as 别名]
def test_blob_creation(self):
import imghdr, StringIO
data = 'some_image_data'
when(StringIO).StringIO(data).thenReturn('data_file')
when(imghdr).what('data_file').thenReturn('jpeg')
image = Image.create(data=data)
reader = BlobReader(image.blob_key)
self.assertEqual(data, reader.read())
image = image.key.get()
self.assertEqual('image/jpeg', image.format)
示例3: test_thumbnail_creation
# 需要导入模块: from models import Image [as 别名]
# 或者: from models.Image import create [as 别名]
def test_thumbnail_creation(self):
data = 'big_image_data'
compressed_data = 'cmprsd_data'
image = Image.create(data=data)
mock_service_image = mock()
when(images).Image(blob_key=image.blob_key).thenReturn(mock_service_image)
when(mock_service_image).execute_transforms(output_encoding = images.JPEG).thenReturn(compressed_data)
options = Options(dict(width=420))
thumbnail = Image.thumbnail(image.short_key, options)
verify(mock_service_image).resize(width=420)
self.assertEqual(compressed_data, read_blob(thumbnail.blob_key))
self.assertEqual(thumbnail.key, key.Key(Image, options.key, parent = image.key))
original_blob_key = thumbnail.blob_key
thumbnail = Image.thumbnail(image.short_key, options)
self.assertEqual(original_blob_key, thumbnail.blob_key)
示例4: create_new_image
# 需要导入模块: from models import Image [as 别名]
# 或者: from models.Image import create [as 别名]
def create_new_image(self, imguri, imgsize):
global gl_image
""" Creates a new image instance and queries
metadata thats already included in the file
resulting in new fragments, metadata and annotations
"""
imageob = Image.create(width=imgsize[0], height=imgsize[1],)
imageob.save()
imageuri = Imageuri.create(
image=imageob,
uri=imguri,
)
imageuri.save()
gl_image = imageob
self.extract_def_metadata(imguri)
return imageob
示例5: upload
# 需要导入模块: from models import Image [as 别名]
# 或者: from models.Image import create [as 别名]
def upload(request):
image = Image.create(data = request.get('image'))
return send(dict(image_key = image.short_key))
示例6: getImages
# 需要导入模块: from models import Image [as 别名]
# 或者: from models.Image import create [as 别名]
def getImages(post_id, makeThumb=True):
# Image HTTP content types
# TODO: Add others -- this should be a standard function
extensions = {'image/gif': 'gif', 'image/jpeg': 'jpg', 'image/pjpeg': 'jpg', 'image/png': 'png'}
post = Post.get(Post.id == post_id)
html = post.content
feed = post.feed_id
soup = BS(html)
img_num = 0
# Attempt to get images in HTML fragment
for tag in soup.find_all('img'):
image_url = tag['src']
try:
f = urllib.request.urlopen(image_url) # TODO: Replace with Request
http = f.info()
content_type = http.type # Get HTTP Content-Type to determine file extension
except urllib.error.HTTPError:
continue
# If unrecognised content-type, skip this URL
if content_type not in extensions:
continue
# TODO: Check image size, skip below size limit (avoids saving 1x1 pixel bugs)
# Reasonable default would be existing thumbnail size
# Check to see if URL already exists in Image table
url_num = Image.select().where(Image.url == image_url).count()
# If not found, add to cache
if url_num == 0:
log.info("Found image %s, writing to cache", image_url)
# Read image data from url
image_data = f.read()
# Create feed directory as required
image_path = '{0}{1}'.format(IMAGE_PATH, str(feed))
if not os.path.exists(image_path):
os.makedirs(image_path)
# Use HTTP content-type to decide extension
# IMAGE_PATH/[feed_id]/[post_id]_[image_number].[ext]
image_file = '{0}{1}/img_{2}.{3}'.format(IMAGE_PATH, str(feed), str(img_num), extensions[content_type])
with open(image_file, 'wb') as img:
img.write(image_data)
img.close()
# Add to Image database table
Image.create(post_id=post_id, feed_id=feed, url=image_url, path=image_file)
if makeThumb:
# Create corresponding thumbnail using Pillow, add to thumbnail cache dir
thumb_file = '{0}{1}/thumb_{2}.{3}'.format(THUMB_PATH, str(feed), str(img_num), extensions[content_type])
try:
thumb = Im.open(image_file)
thumb.thumbnail(THUMB_SIZE)
thumb.save(thumb_file, "JPEG")
except IOError:
log.error("Cannot create thumbnail for %s", image_file)
# increment image counter
img_num += 1