crasch (crasch) wrote,
crasch
crasch

GEEK: simple python script to turn image filenames into img elements

It currently doesn’t take any options, though I plan to modify it to read from a filename in addition to stdin. Also, I should modify it to use imagemagick to get the image width and height values, since I don’t think there’s a guarantee that the metadata will store the image dimensions.

#!/usr/bin/env python

import sys
import optparse
import os

def getImageSize(imageName):
    imageData = os.popen('exiftool -ImageSize %s' % imageName).read().split(' ').pop().strip().split('x')
    width=imageData[0]
    height=imageData[1]
    return width,height

def main():
    """Runs program and handles command line options"""
    parser = optparse.OptionParser(description=' Given a base url, and a list of image filenames from standard input, convert the filenames to html img src html elements',
                                   prog='picturepost',
                                   version='picturepost 0.1',
                                   usage='%prog [BASE URL]')

#     parser.add_option("-f","--file",action="store",type="string",dest="inputFilename")
#     parser.add_option("-b","--base-url",action="store",type="string",dest="baseURL")

    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.print_help()
        sys.exit()
    else:
        baseURL = sys.argv[1]
        while True:
            fileName = sys.stdin.readline()
            if fileName.strip() != '':
                width,height = getImageSize(fileName)
                getImageSize(fileName)
                url = "%s/%s" % (baseURL, fileName.strip())
                img_src = '<img src="%s" border="1" hspace="2" vspace="2" width="%s" height="%s"/>n' % (url, width, height)
                sys.stdout.write(img_src)
            else:
                sys.stdout.flush()
                break

if __name__ == "__main__":
    main()

Original: craschworks - comments

Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 1 comment