(sorta) Fixed images

This commit is contained in:
some weird guy
2023-07-24 21:20:53 -07:00
parent c02fed1eb3
commit 7ad2003b2c
+32 -35
View File
@@ -81,43 +81,40 @@ def recaptcha_verify(request, key):
return True return True
ImageFile.LOAD_TRUNCATED_IMAGES = True ImageFile.LOAD_TRUNCATED_IMAGES = True
# This image upload code is fucked now thanks to pillow. I gotta go through it and refine it.
def image_upload(img, stream=False, drawing=False): def image_upload(img, stream=False, drawing=False):
try: # Decode the image
# Decode the image decodedimg = img.read() if stream else base64.b64decode(img)
decodedimg = img.read() if stream else base64.b64decode(img) # Open the image
# Open the image im = Image.open(io.BytesIO(decodedimg))
im = Image.open(io.BytesIO(decodedimg)) # Check for EXIF data and rotate the image if necessary
# Check for EXIF data and rotate the image if necessary if hasattr(im, '_getexif'):
if hasattr(im, '_getexif'): orientation = 0x0112
orientation = 0x0112 exif = im._getexif()
exif = im._getexif() if exif is not None:
if exif is not None: orientation = exif.get(orientation)
orientation = exif.get(orientation) rotations = {
rotations = { 3: Image.ROTATE_180,
3: Image.ROTATE_180, 6: Image.ROTATE_270,
6: Image.ROTATE_270, 8: Image.ROTATE_90
8: Image.ROTATE_90 }
} if orientation in rotations:
if orientation in rotations: im = im.transpose(rotations[orientation])
im = im.transpose(rotations[orientation]) # Resize the image
# Resize the image im.thumbnail((800, 800))
im.thumbnail((800, 800), Image.ANTIALIAS) # Check the aspect ratio if this is a drawing
# Check the aspect ratio if this is a drawing if drawing and ((im.size[0] / im.size[1]) < 0.30):
if drawing and ((im.size[0] / im.size[1]) < 0.30):
return 1
# Generate a hash of the image
imhash = sha1(im.tobytes()).hexdigest()
# Set the file format and location
target = 'webp'
floc = imhash + '.' + target
# Save the file if it doesn't already exist
if not os.path.exists(settings.MEDIA_ROOT + floc):
im.save(settings.MEDIA_ROOT + floc, target, quality=80, method=6)
# Return the URL of the file
return settings.MEDIA_URL + floc
# Catch any errors
except (OSError, SyntaxError, ValueError):
return 1 return 1
# Generate a hash of the image
imhash = sha1(im.tobytes()).hexdigest()
# Set the file format and location
target = 'webp'
floc = imhash + '.' + target
# Save the file if it doesn't already exist
if not os.path.exists(settings.MEDIA_ROOT + floc):
im.save(settings.MEDIA_ROOT + floc, target, quality=80, method=6)
# Return the URL of the file
return settings.MEDIA_URL + floc
# Todo: Put this into post/comment delete thingy method # Todo: Put this into post/comment delete thingy method
def image_rm(image_url): def image_rm(image_url):