
Untitled
By: a guest on
Jun 30th, 2012 | syntax:
None | size: 1.20 KB | hits: 10 | expires: Never
App Engine: Calculating the dimensions of thumbnails to be generated by serving thumbnails from the blobstore
from __future__ import division
def resized_size(original_width, original_height, width, height):
original_ratio = float(original_width) / float(original_height)
resize_ratio = float(width) / float(height)
if original_ratio >= resize_ratio:
return int(width), int(round(float(width) / float(original_ratio)))
else:
return int(round(float(original_ratio) * float(height))), int(height)
import math
def rint(x):
x_int = int(x)
x_rem = x - x_int # this is problematic
if (x_int % 2) == 1:
return round(x)
else:
if x_rem <= 0.5:
return math.floor(x)
else:
return math.ceil(x)
import math
def rint(x):
x_int = int(x)
x_rem = x - x_int
if (x_int % 2) == 1:
return round(x)
else:
if x_rem - 0.5 < 0.001:
return math.floor(x)
else:
return math.ceil(x)
// In Java
double aspectRatio = originalWidth / originalHeight;
output_width = 800;
output_height = (int)Math.round(800.0 / aspectRatio);
aspectRatio == 1.3063063063063063
output_width == 32
output_height == Math.round(32.0 / 1.3063063063063063) == 24