Last active
September 17, 2017 04:23
-
-
Save mvineetmenon/c0cb5b3a50b18233e06432b1ec36f859 to your computer and use it in GitHub Desktop.
Compiles images in a folder to a simple barebone HTML page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import re | |
def main_prog(): | |
if sys.argv.__len__ <=2: | |
print "Usage: ", sys.argv[0], "<image folder path> <html file to create>" | |
else: | |
img_folder = sys.argv[1] | |
output_html = sys.argv[2] | |
print "Image folder selected ", img_folder | |
print "Output file ", output_html | |
html_f = open(output_html, "w+") | |
print "Generating HTML output..." | |
html_f.write(generateHTML(img_folder)) | |
def generateHTML(img_folder): | |
output = "<html><head></head><body>" | |
dir_content_list = os.listdir(img_folder) | |
dir_content_list.sort(key=natural_keys) | |
for filename in dir_content_list: | |
if filename.endswith(".jpg") or filename.endswith(".gif") or filename.endswith(".png") or filename.endswith(".JPG") or filename.endswith(".GIF") or filename.endswith(".PNG"): | |
output += "<img src='" + img_folder + filename +"'></img>" | |
output += "</body></html>" | |
return output | |
def atoi(text): | |
return int(text) if text.isdigit() else text | |
def natural_keys(text): | |
return [ atoi(c) for c in re.split('(\d+)', text) ] | |
if __name__=="__main__": | |
main_prog() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment