X-Git-Url: https://source.tree.se/git?p=treecutter.git;a=blobdiff_plain;f=treecutter%2Fimage.py;fp=treecutter%2Fimage.py;h=7d01462ac3f8ae38907c98993f89d45c04707eaf;hp=0000000000000000000000000000000000000000;hb=b011c30d2ca867e3e68451c7a3bfced704216fc1;hpb=369be7201d9703391d90d488cca8862b903ef15e diff --git a/treecutter/image.py b/treecutter/image.py new file mode 100644 index 0000000..7d01462 --- /dev/null +++ b/treecutter/image.py @@ -0,0 +1,52 @@ +#!/usr/bin/python + +from PIL import Image as PIL_Image +from libxmp import consts +from libxmp import XMPFiles +from treecutter.tools import sizeof_fmt + + +class Image(): + """Class representing an image""" + def __init__(self,filename): + self._filename = filename + self._format = {} + + def infostr(self): + image = PIL_Image.open(self._filename) + w,d = image.size + image.close() + byte = os.path.getsize(self._filename) + return "[%dx%d (%s)]" % (w,d,sizeof_fmt(byte)) + + def resize(self,x,y): + size = (x, y) + outfile, ext = os.path.splitext(self._filename) + outfile = "%s.%dx%d.%s" % (outfile, size[0], size[1], ext) + if not os.path.exists(outfile): + im = PIL_Image.open(infile) + im.thumbnail(size, PIL_Image.ANTIALIAS) + bg = PIL_Image.new('RGBA', size, (0, 0, 0, 0)) + bg.paste(im,((size[0]-im.size[0])/2, (size[1]-im.size[1])/2)) + im.save(outfile) + self._format[size] = outfile + return outfile + + def set_generated(self): + print self._filename + + def generated(self): + print self._filename + + def thumbnail(self): + return image.resize(50,50) + + def slider(self): + return image.resize(700,438) + + def caption(self): + xmpfile = XMPFiles(file_path=self._filename) + xmp = xmpfile.get_xmp() + cap = xmp.get_property(consts.XMP_NS_DC, 'description[1]' ) + xmpfile.close_file() + return cap