HTTPImageServer
A naive http-based tool to show the image in a given directory.
1. Dependency
- python3
- Pillow
Install pillow via
pip install pillow
2. How to use
A simple command would do the job:
python server.py -p <port> -r <webroot>
where port
is a int number of which port to use (default: 80), and webroot
is the directory that containing the images(default: current directory).
for more details, run python server.py -h
to see all the avaliable arguments.
3. Mostly asked questions
Q1. KeyError: webp
Ans. We use webp format to encode the image and your system have no libwebp installed.
-
you can change the format to others by add argument --thumbnail jpeg.
-
you can install
libwebp-dev
and reinstall pillow package for webp support.
4. Structure.
The server.py mainly provides two api.
- To access filelist under the given directory, please access:
/directory?path=relative/path/to/file
params:
- path: the directory that you want to list. return:
{
"dirs": ["list", "of", "subdirs"],
"imgs": ["image", "file", "under", "the", "directory"],
}
- to access the image:
/img?path=relative/path/to/file&height=100&width=200
params:
-
path: the image that you want to access.
-
height: image max height.
-
width: image max width.
Note: if no height and width are provided, the original image will be returned.
return:
- the image stream.
For more details about the structure, please see the network.py.
5. Ps
-
The http server are written starting from socket, i do not use the http.server module in python. Therefore, there might be some bugs and may cause some problems, be sure not to use in production enviroment.
-
The index.html is gabbage, i have never write javascript before. You can try to re-write it if you are disgusting with that.