0 means fit the other argument only.
This commit is contained in:
parent
c777c65886
commit
3842cccf68
1 changed files with 26 additions and 1 deletions
|
@ -18,8 +18,33 @@ else:
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print('you did it wrong')
|
print('you did it wrong')
|
||||||
quit()
|
quit()
|
||||||
|
|
||||||
|
def fit_into_bounds(image_width, image_height, frame_width, frame_height):
|
||||||
|
'''
|
||||||
|
Given the w+h of the image and the w+h of the frame,
|
||||||
|
return new w+h that fits the image into the frame
|
||||||
|
while maintaining the aspect ratio.
|
||||||
|
|
||||||
|
(1920, 1080, 400, 400) -> (400, 225)
|
||||||
|
'''
|
||||||
|
width_ratio = frame_width / image_width
|
||||||
|
height_ratio = frame_height / image_height
|
||||||
|
ratio = min(width_ratio, height_ratio)
|
||||||
|
|
||||||
|
new_width = int(image_width * ratio)
|
||||||
|
new_height = int(image_height * ratio)
|
||||||
|
|
||||||
|
return (new_width, new_height)
|
||||||
|
|
||||||
|
(image_width, image_height) = i.size
|
||||||
|
|
||||||
|
if new_x == 0:
|
||||||
|
(new_x, new_y) = fit_into_bounds(image_width, image_height, 10000000, new_y)
|
||||||
|
if new_y == 0:
|
||||||
|
(new_x, new_y) = fit_into_bounds(image_width, image_height, new_x, 10000000)
|
||||||
|
|
||||||
print(i.size, new_x, new_y)
|
print(i.size, new_x, new_y)
|
||||||
i = i.resize( (new_x, new_y) )
|
i = i.resize( (new_x, new_y), Image.ANTIALIAS)
|
||||||
if newname is None:
|
if newname is None:
|
||||||
if '.' in filename:
|
if '.' in filename:
|
||||||
suffix = '_{width}x{height}'.format(width=new_x, height=new_y)
|
suffix = '_{width}x{height}'.format(width=new_x, height=new_y)
|
||||||
|
|
Loading…
Reference in a new issue