【问题标题】:Create square thumbnails with Python + MagickWand使用 Python + MagickWand 创建方形缩略图
【发布时间】:2014-07-10 13:39:41
【问题描述】:

如何使用 Python 和 Wand 创建方形缩略图?我正在尝试从任何大小的源图像制作方形缩略图。重要的是缩略图与原始的纵横比相同,裁剪是可以的,并且应该填满缩略图的空间。

【问题讨论】:

    标签: python magickwand wand


    【解决方案1】:

    下面的crop_center() 函数使给定的图像变成正方形。

    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】:
      • 不裁剪。
      • 用颜色填充空白区域(在本例中:白色)。
      • 保持纵横比
      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
      

      【讨论】:

        猜你喜欢
        • 2011-02-10
        • 1970-01-01
        • 2011-02-06
        • 2015-08-31
        • 2018-05-10
        • 2015-01-21
        • 2011-12-31
        • 2013-01-21
        • 2010-12-18
        相关资源
        最近更新 更多