어떻게 파이썬과 완드로 사각형 섬네일을 만들 수 있습니까? 어떤 크기의 소스 이미지에서 정사각형 축소판을 만들려고합니다. 축소판의 원본 가로 세로 비율이 동일해야하며 잘라내 기가 좋습니다. 미리보기의 크기를 채워야합니다.Python + MagickWand로 사각형 섬네일 만들기
0
A
답변
2
다음 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()
작은 정사각형을 만들 수 있습니다.
0
- 자르기 없음.
- 빈 칸을 색상으로 채 웁니다 (이 경우 : 흰색).
- 종횡비 유지
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