示例#1
0
文件: gen.py 项目: grune/Utilities
def main():
	from os import listdir, mkdir
	from os.path import isfile, join, exists
	from Image import open as Image_open

	subject = "fireworks"
	base = "/var/www/html/pg/"
	begin = base + "gen/begin.html"
	end = base + "gen/end.html"

	subject_base = base + subject + "/"
	img_dir = base + "images/%s/" % subject
	th_dir = base + "images/th/%s/" % subject
	new_index = base + subject + ".html" 

	html_images_path = "./images/%s/" % subject
	html_th_images_path = "./images/th/%s/" % subject

	size = (102, 76)

	with open(begin) as f:
	    begin_html = f.read()

	with open(end) as f:
	    end_html = f.read()

	# Working with files only.
	images = [f for f in listdir(img_dir) if isfile(join(img_dir, f))]

	img_html = ""

	if not exists(th_dir):
		mkdir(th_dir)

	for filename in images:
		#Generate thumbnail
		infile = img_dir + filename
		outfile = th_dir + "th_" + filename
		im = Image_open(infile)
		im.thumbnail(size)
		im.save(outfile, "JPEG")

		# Generate image html
        img_html += """
		    <a href="%s%s" title="%s" data-gallery>
		        <img src="%s%s" width=102 height=76 alt="%s">
		    </a>
		""" % (html_images_path, filename, filename, html_th_images_path, filename, filename)

	f = open(new_index, "w")
	# Delete previous index.html
	f.seek(0)
	f.truncate()

	f.write(begin_html)
	f.write(img_html)
	f.write(end_html)
	f.close()
示例#2
0
class Image(): # {{{

    def __init__(self, fqpn, options): # {{{
        try:
            self.image = Iopen(fqpn)
        except IOError:
            raise

        (self.filePath, self.fileName) = split(fqpn)
        self.format = self.image.format
        self.mode = self.image.mode
        self.size = self.image.size
        self.options = options

    # }}}

    def __unicode__(self): return self.fileName

    def save(self, fqpn = None, type = None): # {{{
        if fqpn is not None:
            if type == 'MS':
                self.image.save(fqpn)
            elif type == 'T':
                self.image.save(fqpn)
            else: 
                self.image.save(fqpn)
        else:
            if type == 'MS':
                self.image.save(self.filePath + '/MS_' + self.fileName)
            elif type == 'T':
                self.image.save(self.filePath + '/T_' + self.fileName)
            else: 
                self.image.save(self.filePath + '/' + self.fileName)
    # }}}
    
    def resize(self): # {{{

        '''Method resizes the picture to the maximum allowed size if size in
        horisontal plane is bigger than maximum defined in config file.

        We do this because Gallery has limits in terms of estetics the horisontal
        way, but not in the vertical way.
        '''

        if int(self.size[0]) > int(self.options['maxsize']):

            # diffValue will make sure the aspect ratio is kept for the resize
            # We force result to be an int since we only want "whole" numbers.
            diffValue = self.size[0] / float(self.options['maxsize'])
            sizeHor = int(self.size[0] / diffValue)
            sizeVer = int(self.size[1] / diffValue)
        
            self.image = self.image.resize((sizeHor,sizeVer), ANTIALIAS)
            self.size = self.image.size
            return self.image

        return self.image

    # }}}

    def thumbnail(self): # {{{
        self.image.thumbnail(
            (
                int(self.options['thumbnailx']),
                int(self.options['thumbnaily'])
            ),
            ANTIALIAS
        )
        self.size = self.image.size
        return self.image
    # }}}

    def rotate(self, angle): # {{{
        self.image = self.image.rotate(int(angle))
        self.size = self.image.size
        return self.image