简体   繁体   中英

reduce image size when displaying in template

I've already used PIL to reduce image size when saving an image,some thing like this:

image = Image.open(filename)
image.thumbnail(size, Image.ANTIALIAS)
image.save(filename)

now I want to show an image list in template,list of images is created in views.py :

for company in companies:
   list+='<img src="'+company.logo.url+'" />'
return render_to_response('index',{'list':list})

in template :

<span>{{list|safe}}</span>

Is it possible to reduce the size of images when showing them in template to load them quickly?

Is it possible to reduce the size of images when showing them in template to load them quickly?

Doing this dynamically will almost certainly be slower than just sending the image. When you save the image, just save another version that's smaller and serve that version in your list.

Maybe this can be useful for what you are seeking. With this snippet you can solve the problem of resizing the images at template level. Also you can use a version that has done Ryan Fugger, which can be reached here . Hope it can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM