문제

이미지를 특정 너비와 높이로 크기를 조정하고자를 필요가 있습니다. 정사각형 썸네일을 만들 수있는 메소드를 구성 할 수 있었지만 원하는 썸네일이 사각형이 아닌 경우이를 적용하는 방법은 확실하지 않습니다.

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

더 좋은 방법이 있다면 알려주십시오. 모든 도움은 대단히 감사하겠습니다.

다음은 원하는 프로세스를 설명하는 다이어그램입니다.crop_diagram

감사,

카일

도움이 되었습니까?

해결책

비슷한 문제가있었습니다 (스크린 샷은 매우 유용했습니다). 이것은 내 해결책입니다.

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

다른 팁

둘 다 지정할 수 있습니다 height 그리고 width 매개 변수 resize - 종횡비를 변경하지 않습니다 (Gae 's로는 할 수 없습니다. images 모듈), 그러나 두 차원 각각이 <= 당신이 지정하는 해당 값 (실제로 하나는 지정한 값과 정확히 동일하고 다른 하나는 <=).

왜 먼저 자르고 나중에 크기를 조정하는지 잘 모르겠습니다. 다른 방법으로 일을 해야하는 것 같습니다 ... 원래 이미지의 많은 부분이 가능한 것처럼 "적합한"것에 맞게 크기를 조정 한 다음 자르기 위해 자르십시오. 정확한 결과 차원. (따라서 크기 조정에 대한 원래 제공된 높이와 너비 값을 사용하지 않을 것입니다. 요구 사항을 올바르게 이해하면 결과 이미지가 "일명"공백 "이되지 않도록 확장 할 수 있습니다). 따라서 필요한 것을 정확하게 이해하지 못할 수도 있습니다. 예제를 제공 할 수 있습니다 (프로세스 전에 보이는 이미지, 처리를 돌보아야하는 방법 및 통과 할 매개 변수의 세부 사항에 대한 URL). ?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top