Browse Source

Add ability to rotate a photo

pull/2/head
Noah Petherbridge 5 years ago
parent
commit
200befc65f
3 changed files with 60 additions and 2 deletions
  1. +40
    -2
      rophako/model/photo.py
  2. +6
    -0
      rophako/modules/photo.py
  3. +14
    -0
      rophako/www/photos/edit.html

+ 40
- 2
rophako/model/photo.py View File

@@ -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

+ 6
- 0
rophako/modules/photo.py View File

@@ -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))


+ 14
- 0
rophako/www/photos/edit.html View File

@@ -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>


Loading…
Cancel
Save