From 3b16c8f3e3373c1d25c8f9f9536e95860f75710a Mon Sep 17 00:00:00 2001 From: Aevann1 Date: Tue, 25 Oct 2022 17:39:57 +0200 Subject: [PATCH] close image files after ur done --- files/helpers/media.py | 11 +++++++---- files/routes/asset_submissions.py | 8 ++++---- files/routes/posts.py | 12 ++++++------ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/files/helpers/media.py b/files/helpers/media.py index 07710640b..a3460f8ee 100644 --- a/files/helpers/media.py +++ b/files/helpers/media.py @@ -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 img_path = f'{path}/{img}' 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(): print(hashes[i_hash], flush=True) print(img_path, flush=True) else: hashes[i_hash] = img_path - i = Image.open(filename) - i_hash = str(imagehash.phash(i)) + with Image.open(filename) as i: + i_hash = str(imagehash.phash(i)) + if i_hash in hashes.keys(): os.remove(filename) abort(409, "Image already exists!") diff --git a/files/routes/asset_submissions.py b/files/routes/asset_submissions.py index 600be8d0a..fdb298e9b 100644 --- a/files/routes/asset_submissions.py +++ b/files/routes/asset_submissions.py @@ -242,10 +242,10 @@ if SITE not in ('pcmemes.net', 'watchpeopledie.tv'): highquality = f'/asset_submissions/hats/{name}' file.save(highquality) - i = Image.open(highquality) - if i.width > 100 or i.height > 130: - os.remove(highquality) - return error("Images must be 100x130") + with Image.open(highquality) as i: + if i.width > 100 or i.height > 130: + os.remove(highquality) + return error("Images must be 100x130") if len(list(Iterator(i))) > 1: price = 1000 else: price = 500 diff --git a/files/routes/posts.py b/files/routes/posts.py index ed41414c2..0ada6ba06 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -16,7 +16,7 @@ from files.classes import * from flask import * from io import BytesIO from files.__main__ import app, limiter, cache, db_session -from PIL import Image as PILimage +from PIL import Image from .front import frontlist from urllib.parse import ParseResult, urlunparse, urlparse, quote, unquote from os import path @@ -607,9 +607,9 @@ def thumbnail_thread(pid): if image_req.headers.get("Content-Type","").startswith("image/svg"): continue - image = PILimage.open(BytesIO(image_req.content)) - if image.width < 30 or image.height < 30: - continue + with Image.open(BytesIO(image_req.content)) as i: + if i.width < 30 or i.height < 30: + continue break @@ -621,13 +621,13 @@ def thumbnail_thread(pid): elif x.headers.get("Content-Type","").startswith("image/"): image_req=x - image = PILimage.open(BytesIO(x.content)) + with Image.open(BytesIO(x.content)) as i: + size = len(i.fp.read()) else: db.close() return - size = len(image.fp.read()) if size > 8 * 1024 * 1024: db.close() return