def importAll(srv, folderId): storagePath = os.path.join('quickbudget', 'data', 'receipt') if not os.path.exists(storagePath): os.makedirs(storagePath) q = "'%s' in parents" % (folderId,) allRecipeImages = gdrive.retrieve_all_files(srv, q) result = { 'new': 0, 'total': 0, 'duplicate': 0 } for img in allRecipeImages: print 'OMG', img['title'], '::', img['id'] # , '::', img['embedLink'] result['total'] += 1 try: savedImage = ReceiptImage.query.filter(ReceiptImage.uid == img['id']).one() print 'FILE ID', img['id'], 'already known', savedImage.crc, '::', savedImage.md5 except NoResultFound: content = gdrive.download_file(srv, img['downloadUrl']) md5 = unicode(hashlib.md5(content).hexdigest()) crc = crcContent(content) try: savedDuplicate = ReceiptImage.query.filter(ReceiptImage.crc == crc and ReceiptImage.md5 == md5).one() print 'PROPABLY A DUPLICATE', img['id'], ':: Other one:', savedDuplicate.uid result['duplicate'] += 1 except NoResultFound: filename = '%s-%s' % (img['id'], img['title']) fullPath = os.path.join(storagePath, filename) with open(fullPath, 'w') as f: f.write(content) print 'NEW FILE', img['id'], 'at', fullPath image = ReceiptImage(img['id'], filename) image.crc = crc image.md5 = md5 db_session.add(image) db_session.commit() result['new'] += 1 print print return result
def receipt_rotate(receipt_uid, direction): img = ReceiptImage.get(receipt_uid) imgPath = os.path.join(RECEIPT_IMAGE_FOLDER, img.contentPath) angle = -90 if direction == 'right' else 90 #rotated = None with open(imgPath, 'r') as f: im = Image.open(f) rotated = im.rotate(angle) for x in glob.glob('%s.*' % (imgPath, )): os.unlink(x) rotated.save(imgPath) return ''
def receipt_thumb(receipt_uid, size): img = ReceiptImage.get(receipt_uid) imgPath = os.path.join(RECEIPT_IMAGE_FOLDER, img.contentPath) if size != 'thumb': return Response(file(imgPath, 'r'), direct_passthrough=True) thumbSize = 100 thumbPath = '%s.%s-thumb' % (imgPath, thumbSize) if not os.path.exists(thumbPath): im = Image.open(imgPath) im.thumbnail((thumbSize, thumbSize), Image.ANTIALIAS) im.save(thumbPath, "JPEG") r = Response(file(thumbPath, 'r'), mimetype="image/jpeg", direct_passthrough=True) #r.cache_control.max_age = 60*60*24 #import datetime #r.expires = datetime.datetime(2014,1,1) return r
def main(): receipts = Receipt.allWithoutImage() images = ReceiptImage.allWithoutReceipt() return render_template('index.html', receipts=receipts, images=images[:15])