本文整理汇总了Python中wand.image.Image.sample方法的典型用法代码示例。如果您正苦于以下问题:Python Image.sample方法的具体用法?Python Image.sample怎么用?Python Image.sample使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wand.image.Image
的用法示例。
在下文中一共展示了Image.sample方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_thumb
# 需要导入模块: from wand.image import Image [as 别名]
# 或者: from wand.image.Image import sample [as 别名]
def create_thumb(size, blob):
'''Creates proportionally scaled thumbnail and save to destination. Returns thumbnaill location and source mimetype'''
mimetype = None
with Image(blob=blob) as source:
image = Image(source.sequence[0]) if source.sequence else source
mimetype = image.mimetype
if mimetype == None:
raise InvalidImageException('Invalid image mimetype!')
w, h = image.size
if w > size[0]:
h = int(max(h * size[0] / w, 1))
w = int(size[0])
if h > size[1]:
w = int(max(w * size[1] / h, 1))
h = int(size[1])
size = w, h
if size == image.size:
#image.save(filename=destination + '.' + extension)
return image, mimetype
image.sample(*size)
#image.save(filename=destination + '.' + extension)
return image, mimetype
示例2: process_upload
# 需要导入模块: from wand.image import Image [as 别名]
# 或者: from wand.image.Image import sample [as 别名]
def process_upload(upload, collection='uploads', image_size=(1280, 720),
thumbnail=True):
"""Processes the uploaded images in the posts and also the users avatars.
This should be extensible in future to support the uploading of videos,
audio, etc...
:param _id: The ID for the post, user or anything you want as the filename
:type _id: str
:param upload: The uploaded Werkzeug FileStorage object
:type upload: ``Werkzeug.datastructures.FileStorage``
:param collection: The GridFS collection to upload the file too.
:type collection: str
:param image_size: The max height and width for the upload
:type image_size: Tuple length 2 of int
:param thumbnail: Is the image to have it's aspect ration kept?
:type thumbnail: bool
"""
try:
# StringIO to take the uploaded image to transport to GridFS
output = io.BytesIO()
# All images are passed through Wand and turned in to PNG files.
# Unless the image is a GIF and its format is kept
# Will change if they need thumbnailing or resizing.
img = Image(file=upload)
# If the input file if a GIF then we need to know
gif = True if img.format == 'GIF' else False
animated_gif = True if gif and len(img.sequence) > 1 else False
# Check the exif data.
# If there is an orientation then transform the image so that
# it is always looking up.
try:
exif_data = {
k[5:]: v
for k, v in img.metadata.items()
if k.startswith('exif:')
}
orientation = exif_data.get('Orientation')
orientation = int(orientation)
if orientation: # pragma: no branch
if orientation == 2:
img.flop()
elif orientation == 3:
img.rotate(180)
elif orientation == 4:
img.flip()
elif orientation == 5:
img.flip()
img.rotate(90)
elif orientation == 6:
img.rotate(90)
elif orientation == 7:
img.flip()
img.rotate(270)
elif orientation == 8:
img.rotate(270)
except (AttributeError, TypeError, AttributeError):
pass
if thumbnail:
# If the GIF was known to be animated then save the animated
# then cycle through the frames, transforming them and save the
# output
if animated_gif:
animated_image = Image()
for frame in img.sequence:
frame.transform(resize='{0}x{1}>'.format(*image_size))
# Directly append the frame to the output image
animated_image.sequence.append(frame)
animated_output = io.BytesIO()
animated_output.format = 'GIF'
animated_image.save(file=animated_output)
animated_output.seek(0)
img.transform(resize='{0}x{1}>'.format(*image_size))
else:
# Just sample the image to the correct size
img.sample(*image_size)
# Turn off animated GIF
animated_gif = False
img.format = 'PNG'
uuid = get_uuid()
filename = '{0}.{1}'.format(uuid, 'png')
# Return the file pointer to the start
img.save(file=output)
output.seek(0)
# Place file inside GridFS
m.save_file(filename, output, base=collection)
animated_filename = ''
#.........这里部分代码省略.........
示例3: int
# 需要导入模块: from wand.image import Image [as 别名]
# 或者: from wand.image.Image import sample [as 别名]
## calls liquid-rescale iteratively, only 50 pixels at a time, for a better result
fname = sys.argv[1]
out_fname = sys.argv[2]
dims = sys.argv[3]
# fname = 'small/Pelagia_noctiluca_(Sardinia).jpg'
# fname = 'small/92547.jpg'
# out_fname = 'test.png'
# dims = '1600x400'
step = 125
width_out, height_out = dims.split('x')
height_out = int(height_out)
width_out = int(width_out)
img = Image(filename=fname)
width, height = img.size
ratio = min(width_out/width, height_out/height)
img.sample(int(width * ratio), int(height * ratio))
while img.width < width_out or img.height < height_out:
print(img.size)
diffw = min(width_out - img.width, step)
diffh = min(height_out - img.height, step)
img.liquid_rescale(img.width+diffw, img.height+diffh, delta_x=6)
img.save(filename=out_fname)