strip exif, add cli, add resize img

This commit is contained in:
lza_menace 2023-01-27 00:07:42 -08:00
parent c5d0b4a9ff
commit 7010790739
3 changed files with 40 additions and 6 deletions

View file

@ -85,7 +85,7 @@ class Post(Model):
def get_image_path(self, thumbnail=False): def get_image_path(self, thumbnail=False):
save_path_base = path.join(config.DATA_FOLDER, "uploads") save_path_base = path.join(config.DATA_FOLDER, "uploads")
if thumbnail: if thumbnail:
save_path = path.join(save_path_base, self.get_thumbnail_name()) save_path = path.join(save_path_base, self.thumbnail)
else: else:
save_path = path.join(save_path_base, self.image_name) save_path = path.join(save_path_base, self.image_name)
return save_path return save_path
@ -99,8 +99,34 @@ class Post(Model):
return True return True
except: except:
return False 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
def get_thumbnail_name(self): @property
def resized(self):
s = path.splitext(self.image_name)
return s[0] + '.resized' + s[1]
@property
def thumbnail(self):
s = path.splitext(self.image_name) s = path.splitext(self.image_name)
return s[0] + '.thumbnail' + s[1] return s[0] + '.thumbnail' + s[1]
@ -121,7 +147,7 @@ class Post(Model):
'user': self.user.username, 'user': self.user.username,
'image_name': self.image_name, 'image_name': self.image_name,
'image_path': self.get_image_path(), 'image_path': self.get_image_path(),
'thumbnail_name': self.get_thumbnail_name(), 'thumbnail_name': self.thumbnail,
'thumbnail_path': self.get_image_path(True), 'thumbnail_path': self.get_image_path(True),
'account_index': self.account_index, 'account_index': self.account_index,
'address_index': self.address_index, 'address_index': self.address_index,

View file

@ -2,7 +2,7 @@ from os import makedirs, getenv
from random import choice from random import choice
from datetime import datetime from datetime import datetime
import lorem import lorem, click
from flask import Blueprint from flask import Blueprint
from suchwow._models import db, User, Post, AuditEvent, TipSent, TipReceived, Vote 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) 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') @bp.cli.command('rescan')
def rescan(): def rescan():

View file

@ -27,7 +27,7 @@
Your browser does not support the video tag. Your browser does not support the video tag.
</video> </video>
{% else %} {% 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 %} {% endif %}
</a> </a>
</div> </div>