Skip to content

Latest commit

 

History

History
36 lines (27 loc) · 902 Bytes

images.md

File metadata and controls

36 lines (27 loc) · 902 Bytes
layout title
default
Serving Images

Serving Images

Basic Directory Set-up

First let your urls extend beyound images:

import web

urls = (
'/images/(.*)', 'images' #this is where the image folder is located....
)

Basic Image Class

Now create the class that will handle them:

import os
class images:
    def GET(self,name):
        ext = name.split(".")[-1] # Gather extension
        
        cType = {
            "png":"images/png",
            "jpg":"image/jpeg",
            "gif":"image/gif",
            "ico":"image/x-icon"            }

        if name in os.listdir('images'):  # Security
            web.header("Content-Type", cType[ext]) # Set the Header
            return open('images/%s'%name,"rb").read() # Notice 'rb' for reading images
        else:
            raise web.notfound()