close image files after ur done

master
Aevann1 2022-10-25 17:39:57 +02:00
parent eafa7b8e05
commit 3b16c8f3e3
3 changed files with 17 additions and 14 deletions

View File

@ -170,15 +170,18 @@ def process_image(filename=None, resize=0, trim=False, uploader=None, patron=Fal
if resize == 400 and img in ('256.webp','585.webp'): continue if resize == 400 and img in ('256.webp','585.webp'): continue
img_path = f'{path}/{img}' img_path = f'{path}/{img}'
if img_path == filename: continue if img_path == filename: continue
img = Image.open(img_path)
i_hash = str(imagehash.phash(img)) with Image.open(img_path) as i:
i_hash = str(imagehash.phash(i))
if i_hash in hashes.keys(): if i_hash in hashes.keys():
print(hashes[i_hash], flush=True) print(hashes[i_hash], flush=True)
print(img_path, flush=True) print(img_path, flush=True)
else: hashes[i_hash] = img_path else: hashes[i_hash] = img_path
i = Image.open(filename) with Image.open(filename) as i:
i_hash = str(imagehash.phash(i)) i_hash = str(imagehash.phash(i))
if i_hash in hashes.keys(): if i_hash in hashes.keys():
os.remove(filename) os.remove(filename)
abort(409, "Image already exists!") abort(409, "Image already exists!")

View File

@ -242,7 +242,7 @@ if SITE not in ('pcmemes.net', 'watchpeopledie.tv'):
highquality = f'/asset_submissions/hats/{name}' highquality = f'/asset_submissions/hats/{name}'
file.save(highquality) file.save(highquality)
i = Image.open(highquality) with Image.open(highquality) as i:
if i.width > 100 or i.height > 130: if i.width > 100 or i.height > 130:
os.remove(highquality) os.remove(highquality)
return error("Images must be 100x130") return error("Images must be 100x130")

View File

@ -16,7 +16,7 @@ from files.classes import *
from flask import * from flask import *
from io import BytesIO from io import BytesIO
from files.__main__ import app, limiter, cache, db_session from files.__main__ import app, limiter, cache, db_session
from PIL import Image as PILimage from PIL import Image
from .front import frontlist from .front import frontlist
from urllib.parse import ParseResult, urlunparse, urlparse, quote, unquote from urllib.parse import ParseResult, urlunparse, urlparse, quote, unquote
from os import path from os import path
@ -607,8 +607,8 @@ def thumbnail_thread(pid):
if image_req.headers.get("Content-Type","").startswith("image/svg"): if image_req.headers.get("Content-Type","").startswith("image/svg"):
continue continue
image = PILimage.open(BytesIO(image_req.content)) with Image.open(BytesIO(image_req.content)) as i:
if image.width < 30 or image.height < 30: if i.width < 30 or i.height < 30:
continue continue
break break
@ -621,13 +621,13 @@ def thumbnail_thread(pid):
elif x.headers.get("Content-Type","").startswith("image/"): elif x.headers.get("Content-Type","").startswith("image/"):
image_req=x image_req=x
image = PILimage.open(BytesIO(x.content)) with Image.open(BytesIO(x.content)) as i:
size = len(i.fp.read())
else: else:
db.close() db.close()
return return
size = len(image.fp.read())
if size > 8 * 1024 * 1024: if size > 8 * 1024 * 1024:
db.close() db.close()
return return