resize photos

This commit is contained in:
Kuroshini 2020-01-10 17:47:23 +03:00
parent ba4c6901f9
commit 77cca6c6d7
3 changed files with 23 additions and 20 deletions

View File

@ -29,21 +29,22 @@ class Command(BaseCommand):
def optimize(self, text, alias, max_size): def optimize(self, text, alias, max_size):
"""optimize news images""" """optimize news images"""
for image in get_url_images_in_text(text): for image in get_url_images_in_text(text):
size = get_page_size_by_url(image) size, width, height = get_page_size_by_url(image)
if size < max_size: if size < max_size:
continue continue
quality = round(max_size / (size * 0.01)) percents = round(max_size / (size * 0.01))
width = round(width * percents / 100)
height = round(height * percents / 100)
optimized_image = get_thumbnail( optimized_image = get_thumbnail(
file_=image, file_=image,
**settings.SORL_THUMBNAIL_ALIASES[alias], geometry_string=f'{width}x{height}',
quality=100,
upscale=False upscale=False
).url ).url
text = text.replace(image, optimized_image) text = text.replace(image, optimized_image)
self.stdout.write(self.style.SUCCESS(f'Optimized {image} -> {optimized_image}\n' self.stdout.write(self.style.SUCCESS(f'Optimized {image} -> {optimized_image}\n'
f'Quality [{quality}%]\n')) f'Quality [{percents}%]\n'))
return text return text
@ -58,11 +59,11 @@ class Command(BaseCommand):
self.optimize('https://upload.wikimedia.org/wikipedia/commons/b/b9/Pizigani_1367_Chart_1MB.jpg', self.optimize('https://upload.wikimedia.org/wikipedia/commons/b/b9/Pizigani_1367_Chart_1MB.jpg',
'news_description', size) 'news_description', size)
for news in News.objects.all(): # for news in News.objects.all():
if not isinstance(news.description, dict): # if not isinstance(news.description, dict):
continue # continue
news.description = { # news.description = {
locale: self.optimize(text, alias, size) # locale: self.optimize(text, alias, size)
for locale, text in news.description.items() # for locale, text in news.description.items()
} # }
news.save() # news.save()

View File

@ -3,6 +3,8 @@ import logging
import random import random
import re import re
import string import string
from io import BytesIO
from PIL import Image
from collections import namedtuple from collections import namedtuple
import requests import requests
@ -175,7 +177,9 @@ def get_url_images_in_text(text):
"""Find images urls in text""" """Find images urls in text"""
return re.findall(r'(?:http:|https:)?//.*\.(?:png|jpg|svg)', text) return re.findall(r'(?:http:|https:)?//.*\.(?:png|jpg|svg)', text)
def get_page_size_by_url(url): def get_page_size_by_url(url) -> (int, int, int):
"""Get page size by Content-Length header""" """Returns image size (bytes, width, height)"""
response = requests.head(url) image_raw = requests.get(url)
return int(response.headers.get('content-length')) image = Image.open(BytesIO(image_raw.content))
width, height = image.size
return int(image_raw.headers.get('content-length')), width, height

View File

@ -5,13 +5,11 @@ from sorl.thumbnail.engines.pil_engine import Engine as PILEngine
class GMEngine(PILEngine): class GMEngine(PILEngine):
def create(self, image, geometry, options): def create(self, image, geometry, options):
"""
Processing conductor, returns the thumbnail as an image engine instance
"""
image = self.cropbox(image, geometry, options) image = self.cropbox(image, geometry, options)
image = self.orientation(image, geometry, options) image = self.orientation(image, geometry, options)
image = self.colorspace(image, geometry, options) image = self.colorspace(image, geometry, options)
image = self.remove_border(image, options) image = self.remove_border(image, options)
image = self.scale(image, geometry, options)
image = self.crop(image, geometry, options) image = self.crop(image, geometry, options)
image = self.rounded(image, geometry, options) image = self.rounded(image, geometry, options)
image = self.blur(image, geometry, options) image = self.blur(image, geometry, options)