本文整理汇总了Python中pgmagick.Image.trim方法的典型用法代码示例。如果您正苦于以下问题:Python Image.trim方法的具体用法?Python Image.trim怎么用?Python Image.trim使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pgmagick.Image
的用法示例。
在下文中一共展示了Image.trim方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: emotion_to_avatar
# 需要导入模块: from pgmagick import Image [as 别名]
# 或者: from pgmagick.Image import trim [as 别名]
def emotion_to_avatar(emotion, font_path, color=Color("black")):
"""使用颜文字(文本)来生成一张方形图片
:type emotion: string
:param emotion: 需要绘制在图像上的文本
:type font_path: string
:param font_path: 字体文件路径
:type color: Color
:param color: 绘制的文本颜色
返回结果是一个 Image 对象,并叫将会被规整到 AVATAR_SIZE 设定的大小
"""
font_size = 128
max_size = len(emotion.decode('utf-8')) * font_size
img = Image(Geometry(max_size, max_size), Color("white"))
img.font(font_path)
img.fontPointsize(font_size)
img.annotate(emotion, GravityType.CenterGravity)
img.trim()
img.write('tmp.png')
height = img.rows()
width = img.columns()
origin_pimg = PImage.open('tmp.png')
new_pimg = PImage.new("RGB", (max(height, width), max(height, width)), "white")
if height > width:
new_pimg.paste(origin_pimg, ((height - width) / 2, 0))
else:
new_pimg.paste(origin_pimg, (0, (width - height) / 2))
return new_pimg.resize(AVATAR_SIZE, PImage.ANTIALIAS)
示例2: merge_overlay
# 需要导入模块: from pgmagick import Image [as 别名]
# 或者: from pgmagick.Image import trim [as 别名]
def merge_overlay(self, overlay):
self.open()
overlay.open()
background_image = Image(self.image)
# remove any transparent areas around the logo
overlay_image = Image(Geometry(overlay.image.size().width() + 2, overlay.image.size().height() + 2), 'transparent')
overlay_image.composite(overlay.image, GravityType.CenterGravity, CompositeOperator.OverCompositeOp)
overlay_image.trim()
border_width = int(math.ceil(background_image.size().width() * PIconBackground.BORDER_RATIO))
overlay_image.zoom(Geometry(background_image.size().width() - (border_width * 2), background_image.size().height() - (border_width * 2)))
# we need to calculate exact position since we get 1-pixel error
# if the height of the logo is odd and using GravityType.CenterGravity
if overlay_image.size().width() + (2 * border_width) == background_image.size().width():
x = border_width
y = int(math.ceil((background_image.size().height() / 2.0) - (overlay_image.size().height() / 2.0)))
else:
x = int(math.ceil((background_image.size().width() / 2.0) - (overlay_image.size().width() / 2.0)))
y = border_width
background_image.composite(overlay_image, x, y, CompositeOperator.OverCompositeOp)
return PIcon(background_image)