Unverified Commit 1be5933b authored by captin411's avatar captin411 Committed by GitHub
Browse files

auto cropping now works with non square crops

parent 0ddaf8d2
Loading
Loading
Loading
Loading
+269 −240
Original line number Diff line number Diff line
@@ -11,14 +11,31 @@ RED = "#F00"

def crop_image(im, settings):
  """ Intelligently crop an image to the subject matter """
  if im.height > im.width:
      im = im.resize((settings.crop_width, settings.crop_height * im.height // im.width))
  elif im.width > im.height:
      im = im.resize((settings.crop_width * im.width // im.height, settings.crop_height))
  else:
      im = im.resize((settings.crop_width, settings.crop_height))

  if im.height == im.width:
  scale_by = 1
  if is_landscape(im.width, im.height):
    scale_by = settings.crop_height / im.height
  elif is_portrait(im.width, im.height):
    scale_by = settings.crop_width / im.width
  elif is_square(im.width, im.height):
    if is_square(settings.crop_width, settings.crop_height):
      scale_by = settings.crop_width / im.width
    elif is_landscape(settings.crop_width, settings.crop_height):
      scale_by = settings.crop_width / im.width
    elif is_portrait(settings.crop_width, settings.crop_height):
      scale_by = settings.crop_height / im.height

  im = im.resize((int(im.width * scale_by), int(im.height * scale_by)))

  if im.width == settings.crop_width and im.height == settings.crop_height:
    if settings.annotate_image:
      d = ImageDraw.Draw(im)
      rect = [0, 0, im.width, im.height]
      rect[2] -= 1
      rect[3] -= 1
      d.rectangle(rect, outline=GREEN)
      if settings.destop_view_image:
        im.show()
    return im

  focus = focal_point(im, settings)
@@ -214,6 +231,18 @@ def poi_average(pois, settings):
    return PointOfInterest(avg_x, avg_y)


def is_landscape(w, h):
  return w > h


def is_portrait(w, h):
  return h > w


def is_square(w, h):
  return w == h


class PointOfInterest:
  def __init__(self, x, y, weight=1.0, size=10):
    self.x = x