Add ability to rotate a photo

pull/2/head
Noah 2014-06-20 21:25:15 -07:00
bovenliggende 95da4b19a1
commit 200befc65f
3 gewijzigde bestanden met toevoegingen van 60 en 2 verwijderingen

Bestand weergeven

@ -195,12 +195,47 @@ def edit_photo(key, data):
album = index["map"][key]
logger.info("Completely deleting the photo {} from album {}".format(key, album))
logger.info("Updating data for the photo {} from album {}".format(key, album))
index["albums"][album][key].update(data)
write_index(index)
def rotate_photo(key, rotate):
"""Rotate a photo 90 degrees to the left or right."""
photo = get_photo(key)
if not photo: return
# Degrees to rotate.
degrees = None
if rotate == "left":
degrees = 90
elif rotate == "right":
degrees = -90
else:
degrees = 180
new_names = dict()
for size in ["large", "thumb", "avatar"]:
fname = os.path.join(config.PHOTO_ROOT_PRIVATE, photo[size])
logger.info("Rotating image {} by {} degrees.".format(fname, degrees))
# Give it a new name.
filetype = fname.split(".")[-1]
outfile = random_name(filetype)
new_names[size] = outfile
img = Image.open(fname)
img = img.rotate(degrees)
img.save(os.path.join(config.PHOTO_ROOT_PRIVATE, outfile))
# Delete the old name.
os.unlink(fname)
# Save the new image names.
edit_photo(key, new_names)
def delete_photo(key):
"""Delete a photo."""
index = get_index()
@ -216,7 +251,9 @@ def delete_photo(key):
# Delete all the images.
for size in ["large", "thumb", "avatar"]:
logger.info("Delete: {}".format(photo[size]))
os.unlink(os.path.join(config.PHOTO_ROOT_PRIVATE, photo[size]))
fname = os.path.join(config.PHOTO_ROOT_PRIVATE, photo[size])
if os.path.isfile(fname):
os.unlink(fname)
# Delete it from the sort list.
index["photo-order"][album].remove(key)
@ -543,6 +580,7 @@ def write_index(index):
def random_name(filetype):
"""Get a random available file name to save a new photo."""
filetype = filetype.lower()
outfile = random_hash() + "." + filetype
while os.path.isfile(os.path.join(config.PHOTO_ROOT_PRIVATE, outfile)):
outfile = random_hash() + "." + filetype

Bestand weergeven

@ -195,7 +195,13 @@ def edit(key):
if request.method == "POST":
caption = request.form.get("caption", "")
rotate = request.form.get("rotate", "")
Photo.edit_photo(key, dict(caption=caption))
# Rotating the photo?
if rotate in ["left", "right", "180"]:
Photo.rotate_photo(key, rotate)
flash("The photo has been updated.")
return redirect(url_for(".view_photo", key=key))

Bestand weergeven

@ -12,6 +12,20 @@
<strong>Photo Caption:</strong><br>
<input type="text" size="40" name="caption" value="{{ photo['caption'] }}"><p>
Rotate:
<label>
<input type="radio" name="rotate" value="" checked> Leave alone
</label>
<label>
<input type="radio" name="rotate" value="left"> Left 90&deg;
</label>
<label>
<input type="radio" name="rotate" value="right"> Right 90&deg;
</label>
<label>
<input type="radio" name="rotate" value="180"> 180&deg;
</label><p>
<button type="submit">Save Changes</button>
</form>