Letterbox for sorl-thumbnail

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
def ltbx(im, requested_size, opts):
	img_x, img_y   = [float(v) for v in im.size]
	dest_x, dest_y = [float(v) for v in requested_size]

	if 'ltbx' in opts and im.size != requested_size:
		img_ratio = img_x/img_y
		dest_ratio = dest_x/dest_y
		if dest_ratio > img_ratio:
			im = im.resize(( int(dest_y*img_ratio), dest_y ) , resample=Image.ANTIALIAS)
		else:
			im = im.resize(( dest_x, int(dest_x/img_ratio) ) , resample=Image.ANTIALIAS)
		canvas = Image.new("RGB", requested_size, THUMBNAIL_PADDING_COLOR)
		left = floor((requested_size[0] - im.size[0]) / 2)
		top = floor((requested_size[1] - im.size[1]) / 2)
		canvas.paste(im, (left, top))
		im = canvas
 
	return im

ltbx.valid_options = ('ltbx', )

More like this

  1. sorl.thumbnail processor: white background by izibi 4 years, 9 months ago
  2. Template Tag of Django Image Thumb Creator by ayang23 2 years, 3 months ago
  3. [UPDATE]Filter to resize a ImageField on demand + RATIO OPTION + CLEANUP by karlsberg 2 years, 9 months ago
  4. Custom filename example with sorl-thumbnail by sajal 2 years, 3 months ago
  5. Image model with thumbnail by clawlor 2 years, 10 months ago

Comments

(Forgotten your password?)