本文整理汇总了Python中pgmagick.Image.sample方法的典型用法代码示例。如果您正苦于以下问题:Python Image.sample方法的具体用法?Python Image.sample怎么用?Python Image.sample使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pgmagick.Image
的用法示例。
在下文中一共展示了Image.sample方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_sized_image
# 需要导入模块: from pgmagick import Image [as 别名]
# 或者: from pgmagick.Image import sample [as 别名]
def get_sized_image(filename, size):
if not os.path.exists(DIR):
os.mkdir(DIR)
cache_hash = sha1('{0}\0{1}'.format(filename, size)).hexdigest() + '.jpg'
cache_path = os.path.join(DIR, cache_hash)
if not os.path.exists(cache_path):
im = Image(filename.encode('utf-8'))
im.sample(size)
im.write(cache_path)
return '/'.join(PARTS + (cache_hash,))
示例2: generate
# 需要导入模块: from pgmagick import Image [as 别名]
# 或者: from pgmagick.Image import sample [as 别名]
def generate(self, source_path, small_thumb_path, large_thumb_path):
img = Image(source_path)
width = img.size().width()
height = img.size().height()
# Detect if we need to rotate the image by reading EXIF data
orientation = 0
if img.attribute("EXIF:Orientation") != "unknown":
try:
orientation = int(img.attribute("EXIF:Orientation"))
except ValueError:
print ("Invalid EXIF orientation, using default")
# Detect if we need to resize the large thumbnail
if width > LARGE_MAX_WIDTH:
height = int((float(height) / width) * LARGE_MAX_WIDTH)
width = LARGE_MAX_WIDTH
elif height > LARGE_MAX_HEIGHT:
width = int((float(width) / height) * LARGE_MAX_HEIGHT)
height = LARGE_MAX_HEIGHT
# Rescale the large thumbnail if dimensions doesn't match
if width != img.size().width() or height != img.size().height():
img.sample("!%sx%s" % (width, height))
# Rotate the image if needed
if orientation == 6:
img.rotate(90)
elif orientation == 8:
img.rotate(-90)
self.write_image(img, large_thumb_path)
# Crop the small thumbnail and then resize it to the correct size
img.crop("%sx%s" % (min(width, height), min(width, height)))
img.sample("%sx%s" % (SMALL_WIDTH_AND_HEIGHT, SMALL_WIDTH_AND_HEIGHT))
self.write_image(img, small_thumb_path)