示例#1
0
 def test_img_md5(self):
     from pygall.lib.helpers import img_md5
     src = 'python.jpg'
     md5 = img_md5(src)
     self.assertEqual(md5, '065c540533e7621f6fc37fb9ab297b3f')
     # test img_md5 of a file object
     with open(src, 'rb') as f:
         md5 = img_md5(f)
         self.assertEqual(md5, '065c540533e7621f6fc37fb9ab297b3f')
示例#2
0
文件: photos.py 项目: bbinet/PyGall
    def _import(self, abspath):
        # check same image has not already been imported
        hash = img_md5(abspath)
        photo = DBSession.query(Photo).filter_by(md5sum=hash)
        if photo.count() > 0:
            log.info("Same md5sum already exists in database")
            return photo.first()

        # process and import photos to public/data/photos dir
        info = ip.process_image(abspath, md5sum=hash)
        os.unlink(abspath)

        # import image in db
        photo = Photo()
        photo.uri = info["uri"]
        photo.md5sum = hash
        photo.time = info["date"]
        DBSession.add(photo)
        DBSession.flush()

        return info
示例#3
0
def process(row, msgs):
    fspot_id = row.id
    insert = False
    photo = DBSession.query(Photo).filter_by(fspot_id=fspot_id).first()
    if photo is None:
        insert = True
        photo = Photo()
        src = decode_fspot_uri(
                row.uri if row.last_version is None else row.last_version.uri)
        md5sum = img_md5(src)
        # copy and scale image if needed
        info = IP.process_image(src, md5sum=md5sum)
        # set photo db record
        photo.fspot_id = fspot_id
        photo.uri = info['uri']
        photo.md5sum = md5sum
        photo.time = info['date']

    if insert or not OPTIONS['skip-existing']:
        # update row in db
        if row.description:
            photo.description = row.description
        photo.rating = row.rating
        photo.tags = get_tags([t.name for t in row.tags])

    #TODO: detect if photo version has changed and update photo accordingly

    try:
        uri = photo.uri # keep track of photo.uri outside the DBSession
        DBSession.add(photo)
        transaction.commit()
        if insert:
            msgs.append("Photo %s has been imported in PyGall" % uri)
    except IntegrityError:
        #print "Photo %s already exists in db" % uri
        transaction.abort()
        #TODO: make it possible to update record

    return fspot_id
示例#4
0
def get_info(img, info=None):
    """
    Get infos about the given image
    """
    if info is None: info = {}
    loc = seek(img, 0)
    im = Image.open(img)
    if 'date' not in info:
        try:
            exif = get_exif(im._getexif())
            info['date'] = datetime.strptime(
                    exif['DateTimeOriginal'], '%Y:%m:%d %H:%M:%S')
        except:
            info['date'] = None
    if 'md5sum' not in info:
        info['md5sum'] = img_md5(im)
    if 'ext' not in info:
        info['ext'] = im.format.lower()
    if 'size' not in info:
        info['size'] = get_size(img)
    if loc is not None: seek(img, loc)

    return info