App Engine剪裁到特定的宽度和高度 [英] App Engine Cropping to a Specific Width and Height

查看:147
本文介绍了App Engine剪裁到特定的宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  def rescale(数据,宽度,高度):
重新缩放给定的图像,可选择裁剪它以确保结果图像具有指定的宽度和高度。
from google.appengine.api import images

new_width = width
new_height = height

img = images.Image(data)

org_width,org_height = img.width,img.height

#我们必须确定图像是纵向还是横向
#横向
if org_width> org_height:
#使用横向图像我们希望作物居中。我们必须找到图像的
#高宽比,然后将分母转换为浮点数
#,这样比例就成为了一个不错的点。该比例是将保留的图像
#的百分比。
ratio = org_height / float(org_width)
#要找到要移除的图像的百分比,我们从1减去比例
#通过除以2得到的百分比应该是从每边删除
#这也是我们的left_x坐标
left_x =(1-比率)/ 2
#通过从1中减去left_x,我们找到right_x坐标
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)
#肖像
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()
返回缩略图

如果有更好的方法可以做到这一点,请让我知道。任何帮助将不胜感激。



以下是解释所需过程的图表。



谢谢,



Kyle

解决方案

我有类似的问题)。这是我的解决方案:

$ def $(defg) :
调整大小然后可以选择裁剪给定的图像

属性:
img_data:图像数据
宽度:所需宽度
height:理想高度
halign:像photoshop的Canvas Size功能水平
将裁剪对齐到左,中或右
valign:垂直将裁剪对齐到顶部,中间或底部


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,然后裁剪到高度
image.resize(width = width)
image.execute_transforms()
trim_y =(float(image.height - height )/ 2)/ image.height
如果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,然后裁剪为宽度
image.resize(height = height)
image.execute_transforms()
trim_x =(float(image.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()


I need to resize and crop an image to a specific width and height. I was able to construct a method that will create a square thumbnail, but I'm unsure on how to apply this, when the desired thumbnail is not square.

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

If there is a better way to do this please let me know. Any help would be greatly appreciated.

Here's a diagram explaining the desired process.

Thanks,

Kyle

解决方案

I had a similar problem (your screenshot was very useful). This is my solution:

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()

这篇关于App Engine剪裁到特定的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆