strip exif, add cli, add resize img

migrate-new-models
lza_menace 1 year ago
parent c5d0b4a9ff
commit 7010790739

@ -85,7 +85,7 @@ class Post(Model):
def get_image_path(self, thumbnail=False):
save_path_base = path.join(config.DATA_FOLDER, "uploads")
if thumbnail:
save_path = path.join(save_path_base, self.get_thumbnail_name())
save_path = path.join(save_path_base, self.thumbnail)
else:
save_path = path.join(save_path_base, self.image_name)
return save_path
@ -99,8 +99,34 @@ class Post(Model):
return True
except:
return False
def strip_exif(self):
try:
image = Image.open(self.get_image_path())
data = image.getdata()
image_without_exif = Image.new(image.mode, image.size)
image_without_exif.putdata(data)
image_without_exif.save(self.get_image_path())
image_without_exif.close()
image.close()
except:
return False
def resize_image(self):
try:
with Image.open(self.get_image_path()) as img:
img.thumbnail((1800,1800))
img.save(self.get_image_path())
except:
return False
@property
def resized(self):
s = path.splitext(self.image_name)
return s[0] + '.resized' + s[1]
def get_thumbnail_name(self):
@property
def thumbnail(self):
s = path.splitext(self.image_name)
return s[0] + '.thumbnail' + s[1]
@ -121,7 +147,7 @@ class Post(Model):
'user': self.user.username,
'image_name': self.image_name,
'image_path': self.get_image_path(),
'thumbnail_name': self.get_thumbnail_name(),
'thumbnail_name': self.thumbnail,
'thumbnail_path': self.get_image_path(True),
'account_index': self.account_index,
'address_index': self.address_index,

@ -2,7 +2,7 @@ from os import makedirs, getenv
from random import choice
from datetime import datetime
import lorem
import lorem, click
from flask import Blueprint
from suchwow._models import db, User, Post, AuditEvent, TipSent, TipReceived, Vote
@ -102,7 +102,15 @@ def payout_users():
wownero.from_atomic(sent), wownero.from_atomic(to_send)
))
@bp.cli.command('fix_image')
@click.argument('post_id')
def fix_image(post_id):
p = Post.filter(id=post_id).first()
if p:
p.strip_exif()
p.resize_image()
else:
print("That post doesn't exist")
@bp.cli.command('rescan')
def rescan():

@ -27,7 +27,7 @@
Your browser does not support the video tag.
</video>
{% else %}
<img alt="SuchWow #{{ post.id }} - {{ post.title }} by {{ post.user.username }}" src="{{ url_for('post.uploaded_file', filename=post.get_thumbnail_name()) }}" />
<img alt="SuchWow #{{ post.id }} - {{ post.title }} by {{ post.user.username }}" src="{{ url_for('post.uploaded_file', filename=post.thumbnail) }}" />
{% endif %}
</a>
</div>