【问题标题】:App Engine Cropping to a Specific Width and HeightApp Engine 裁剪到特定的宽度和高度
【发布时间】:2012-04-24 05:41:12
【问题描述】:

我需要调整图像大小并将其裁剪为特定的宽度和高度。我能够构建一个创建方形缩略图的方法,但是当所需的缩略图不是方形时,我不确定如何应用它。

def rescale(data, width, height):
"""Rescale the given image, optionally cropping it to make sure the result image has the specified width and height."""
from google.appengine.api import images

new_width = width
new_height = height

img = images.Image(data)

org_width, org_height = img.width, img.height

# We must determine if the image is portrait or landscape
# Landscape
if org_width > org_height:
    # With the Landscape image we want the crop to be centered. We must find the
    # height to width ratio of the image and Convert the denominater to a float
    # so that ratio will be a decemal point. The ratio is the percentage of the image
    # that will remain.
    ratio = org_height / float(org_width)
    # To find the percentage of the image that will be removed we subtract the ratio
    # from 1 By dividing this number by 2 we find the percentage that should be
    # removed from each side this is also our left_x coordinate
    left_x = (1- ratio) / 2
    # By subtract the left_x from 1 we find the right_x coordinate
    right_x = 1 - left_x
    # crop(image_data, left_x, top_y, right_x, bottom_y), output_encoding=images.PNG)
    img.crop(left_x, 0.0, right_x, 1.0)
    # resize(image_data, width=0, height=0, output_encoding=images.PNG)
    img.resize(height=height)
# Portrait
elif org_width < org_height:
    ratio = org_width / float(org_height)
    # crop(image_data, left_x, top_y, right_x, bottom_y), output_encoding=images.PNG)
    img.crop(0.0, 0.0, 1.0, ratio)
    # resize(image_data, width=0, height=0, output_encoding=images.PNG)
    img.resize(width=witdh)

thumbnail = img.execute_transforms()
return thumbnail

如果有更好的方法,请告诉我。任何帮助将不胜感激。

这是一个解释所需过程的图表。

谢谢,

凯尔

【问题讨论】:

    标签: google-app-engine resize image-manipulation resize-image dynamic-resizing


    【解决方案1】:

    我遇到了类似的问题(您的屏幕截图非常有用)。这是我的解决方案:

    def rescale(img_data, width, height, halign='middle', valign='middle'):
      """Resize then optionally crop a given image.
    
      Attributes:
        img_data: The image data
        width: The desired width
        height: The desired height
        halign: Acts like photoshop's 'Canvas Size' function, horizontally
                aligning the crop to left, middle or right
        valign: Verticallly aligns the crop to top, middle or bottom
    
      """
      image = images.Image(img_data)
    
      desired_wh_ratio = float(width) / float(height)
      wh_ratio = float(image.width) / float(image.height)
    
      if desired_wh_ratio > wh_ratio:
        # resize to width, then crop to height
        image.resize(width=width)
        image.execute_transforms()
        trim_y = (float(image.height - height) / 2) / image.height
        if valign == 'top':
          image.crop(0.0, 0.0, 1.0, 1 - (2 * trim_y))
        elif valign == 'bottom':
          image.crop(0.0, (2 * trim_y), 1.0, 1.0)
        else:
          image.crop(0.0, trim_y, 1.0, 1 - trim_y)
      else:
        # resize to height, then crop to width
        image.resize(height=height)
        image.execute_transforms()
        trim_x = (float(image.width - width) / 2) / image.width
        if halign == 'left':
          image.crop(0.0, 0.0, 1 - (2 * trim_x), 1.0)
        elif halign == 'right':
          image.crop((2 * trim_x), 0.0, 1.0, 1.0)
        else:
          image.crop(trim_x, 0.0, 1 - trim_x, 1.0)
    
      return image.execute_transforms()
    

    【讨论】:

    • 谢谢。这正是我想要的。
    • 非常感谢。这正是我想要的。
    • 这正是我所需要的。节省了我几个小时的工作!谢谢!
    【解决方案2】:

    您可以同时指定height width 参数为resize - 它不会改变纵横比(你不能用GAE 的images 模块做到这一点),但它将确保两个维度中的每一个都是&lt;=您指定的对应值(实际上,一个将完全等于您指定的值,另一个将是&lt;=)。

    我不确定您为什么要先裁剪然后再调整大小 - 似乎您应该以相反的方式做事...调整大小以尽可能多地“适合”原始图像,然后裁剪以确保精确的结果尺寸。 (所以你不会使用原始提供的高度和宽度值来调整大小——如果我正确理解你的要求,你会放大它们,这样生成的图像就不会“浪费”也就是“空白”)。因此,也许我不完全了解您的要求-您能否提供一个示例(处理前图像的 URL,处理后的图像应该如何看待,以及您将要传递的参数的详细信息) ?

    【讨论】:

    • 谢谢 Alex,比如说我有一个宽度为 500 像素、高度为 300 像素的图像。我希望裁剪后的图像(缩略图)的宽度为 150 像素,高度为 100 像素。我相信您首先调整大小的权利。我确信整个函数非常简单,我一直在努力编写代码。这是描述该过程的图表的链接。 farm3.static.flickr.com/2543/4205690696_b3821a12e9_o.gif
    猜你喜欢
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 2012-10-15
    • 2013-10-04
    • 1970-01-01
    相关资源
    最近更新 更多