Skip to content

Commit

Permalink
Support for medium sized files
Browse files Browse the repository at this point in the history
  • Loading branch information
r0x0r committed Dec 17, 2014
1 parent f5fc1c2 commit 79be260
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.5
- `New` Support for medium sized files introduced in Lychee 2.7
- `Improved` Decreased the quality of generated thumbnails for smaller thumbnail sizes

## 1.2
- `New` Support for importing directly from iPhoto / Aperture libraries

Expand Down
6 changes: 3 additions & 3 deletions database.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,18 @@ def addFileToAlbum(self, photo):
"size, star, " +
"thumbUrl, album, iso, aperture, make, " +
"model, shutter, focal, takestamp, " +
"description, title, checksum) " +
"description, title, checksum, medium) " +
"values " +
"({}, '{}', {}, '{}', {}, {}, " +
"'{}', {}, " +
"'{}', '{}', '{}', '{}', '{}', " +
"'{}', '{}', '{}', '{}', " +
"'{}', '{}', '{}')"
"'{}', '{}', '{}', {})"
).format(photo.id, photo.url, conf.public, photo.type, photo.width, photo.height,
photo.size, photo.star,
photo.url, photo.albumid, photo.exif.iso, photo.exif.aperture, photo.exif.make,
photo.exif.model, photo.exif.shutter, photo.exif.focal, photo.datetime.strftime("%s"),
photo.description, photo.originalname, photo.checksum)
photo.description, photo.originalname, photo.checksum, 1)

cur = self.db.cursor()
cur.execute(query)
Expand Down
2 changes: 1 addition & 1 deletion lycheeupload.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/python
# -*- coding: utf-8 -*-
"""
lychee upload v1.2
lychee upload v1.2.5
(C) 2014 Roman Sirokov
Imports images from a location on hard drive to the Lychee installation on a remote server via SSH.
Expand Down
18 changes: 17 additions & 1 deletion photo.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class LycheePhoto:

SMALL_THUMB_SIZE = (200, 200)
BIG_THUMB_SIZE = (400, 400)
MEDIUM_SIZE = (1920.0, 1080.0)
JPG_QUALITY = 80


def __init__(self, full_path, album_id):
Expand Down Expand Up @@ -140,11 +142,24 @@ def __init__(self, full_path, album_id):

self.thumbnailfullpath = self.generateThumbnail(self.SMALL_THUMB_SIZE)
self.thumbnailx2fullpath = self.generateThumbnail(self.BIG_THUMB_SIZE)
self.medium_path = self.generateMediumRes(self.MEDIUM_SIZE)

# Generate SHA1 hash
self.checksum = self.generateHash(self.srcfullpath)


def generateMediumRes(self, res):
with tempfile.NamedTemporaryFile(delete=False) as temp_image:
img = Image.open(self.srcfullpath)
ratio = min(res[0] / img.size[0], res[1] / img.size[1])
new_size = tuple(int(ratio * size) for size in img.size)

medium_img = img.resize(new_size, Image.ANTIALIAS)
medium_img.save(temp_image.name, "JPEG", quality=self.JPG_QUALITY)

return temp_image.name


def generateThumbnail(self, res):
"""
Create the thumbnail of a given photo
Expand Down Expand Up @@ -173,7 +188,7 @@ def generateThumbnail(self, res):
img = Image.open(self.srcfullpath)
img = img.crop((left, upper, right, lower))
img.thumbnail(res, Image.ANTIALIAS)
img.save(destimage, "JPEG", quality=99)
img.save(destimage, "JPEG", quality=self.JPG_QUALITY)
return destimage


Expand All @@ -182,6 +197,7 @@ def cleanup(self):
Delete thumbnail files from the local disk. Called after the photo was successfully uploaded.
"""
try:
os.remove(self.medium_path)
os.remove(self.thumbnailfullpath)
os.remove(self.thumbnailx2fullpath)
except Exception:
Expand Down
13 changes: 8 additions & 5 deletions upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,23 @@ def uploadPhoto(self, photo):
file_name = os.path.basename(photo.srcfullpath)

try:
thumbnailPath = os.path.join(conf.path, "uploads", "thumb")
thumbnail_path = os.path.join(conf.path, "uploads", "thumb")
medium_path = os.path.join(conf.path, "uploads", "medium")

# upload photo
self.ssh.put(photo.srcfullpath, photo.destfullpath)
self.ssh.put(photo.thumbnailfullpath, os.path.join(thumbnailPath, photo.url))
self.ssh.put(photo.thumbnailx2fullpath, os.path.join(thumbnailPath, photo.thumb2xUrl))
self.ssh.put(photo.medium_path, os.path.join(medium_path, photo.url))
self.ssh.put(photo.thumbnailfullpath, os.path.join(thumbnail_path, photo.url))
self.ssh.put(photo.thumbnailx2fullpath, os.path.join(thumbnail_path, photo.thumb2xUrl))

if self.dao.addFileToAlbum(photo):
logger.info("Uploaded file {}/{}".format(album_name, file_name))
return True
else:
self.ssh.remove(photo.destfullpath)
self.ssh.remove(os.path.join(thumbnailPath, photo.url))
self.ssh.remove(os.path.join(thumbnailPath, photo.thumb2xUrl))
self.ssh.remove(os.path.join(medium_path, photo.url))
self.ssh.remove(os.path.join(thumbnail_path, photo.url))
self.ssh.remove(os.path.join(thumbnail_path, photo.thumb2xUrl))

return False

Expand Down

0 comments on commit 79be260

Please sign in to comment.