首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Python + MagickWand创建方形缩略图

用Python + MagickWand创建方形缩略图
EN

Stack Overflow用户
提问于 2014-07-10 13:39:41
回答 2查看 741关注 0票数 0

如何使用Python和魔杖创建方形缩略图?我试着用任何大小的原始图片制作正方形缩略图。重要的是,缩略图有相同的纵横比与原始,裁剪是可以的,它应该填补缩略图的边部。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-11 05:27:30

下面的crop_center()函数使给定的图像平方。

代码语言:javascript
复制
from __future__ import division

from wand.image import Image


def crop_center(image):
    dst_landscape = 1 > image.width / image.height
    wh = image.width if dst_landscape else image.height
    image.crop(
        left=int((image.width - wh) / 2),
        top=int((image.height - wh) / 2),
        width=int(wh),
        height=int(wh)
    )

首先,您需要使图像方形,然后您可以resize()的方块更小。

票数 2
EN

Stack Overflow用户

发布于 2017-03-30 20:22:01

  • 不收庄稼。
  • 用颜色填充空格(在本例中为白色)。
  • 维持高宽比

代码语言:javascript
复制
from math import ceil
from wand.image import Color


def square_image(img):
    width = float(img.width)
    height = float(img.height)
    if width == height:
        return img
    border_height = 0
    border_width = 0
    if width > height:
        crop_size = int(width)
        border_height = int(ceil((width - height)/2))
    else:
        crop_size = int(height)
        border_width = int(ceil((height - width)/2))
    img.border(color=Color('white'), height=border_height, width=border_width)
    img.crop(top=0, left=0, width=crop_size, height=crop_size)
    return img
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24678080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档