resize photos
This commit is contained in:
parent
ba4c6901f9
commit
77cca6c6d7
|
|
@ -29,21 +29,22 @@ class Command(BaseCommand):
|
|||
def optimize(self, text, alias, max_size):
|
||||
"""optimize news images"""
|
||||
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:
|
||||
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(
|
||||
file_=image,
|
||||
**settings.SORL_THUMBNAIL_ALIASES[alias],
|
||||
quality=100,
|
||||
geometry_string=f'{width}x{height}',
|
||||
upscale=False
|
||||
).url
|
||||
text = text.replace(image, optimized_image)
|
||||
self.stdout.write(self.style.SUCCESS(f'Optimized {image} -> {optimized_image}\n'
|
||||
f'Quality [{quality}%]\n'))
|
||||
f'Quality [{percents}%]\n'))
|
||||
|
||||
return text
|
||||
|
||||
|
|
@ -58,11 +59,11 @@ class Command(BaseCommand):
|
|||
self.optimize('https://upload.wikimedia.org/wikipedia/commons/b/b9/Pizigani_1367_Chart_1MB.jpg',
|
||||
'news_description', size)
|
||||
|
||||
for news in News.objects.all():
|
||||
if not isinstance(news.description, dict):
|
||||
continue
|
||||
news.description = {
|
||||
locale: self.optimize(text, alias, size)
|
||||
for locale, text in news.description.items()
|
||||
}
|
||||
news.save()
|
||||
# for news in News.objects.all():
|
||||
# if not isinstance(news.description, dict):
|
||||
# continue
|
||||
# news.description = {
|
||||
# locale: self.optimize(text, alias, size)
|
||||
# for locale, text in news.description.items()
|
||||
# }
|
||||
# news.save()
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ import logging
|
|||
import random
|
||||
import re
|
||||
import string
|
||||
from io import BytesIO
|
||||
from PIL import Image
|
||||
from collections import namedtuple
|
||||
|
||||
import requests
|
||||
|
|
@ -175,7 +177,9 @@ def get_url_images_in_text(text):
|
|||
"""Find images urls in text"""
|
||||
return re.findall(r'(?:http:|https:)?//.*\.(?:png|jpg|svg)', text)
|
||||
|
||||
def get_page_size_by_url(url):
|
||||
"""Get page size by Content-Length header"""
|
||||
response = requests.head(url)
|
||||
return int(response.headers.get('content-length'))
|
||||
def get_page_size_by_url(url) -> (int, int, int):
|
||||
"""Returns image size (bytes, width, height)"""
|
||||
image_raw = requests.get(url)
|
||||
image = Image.open(BytesIO(image_raw.content))
|
||||
width, height = image.size
|
||||
return int(image_raw.headers.get('content-length')), width, height
|
||||
|
|
@ -5,13 +5,11 @@ from sorl.thumbnail.engines.pil_engine import Engine as PILEngine
|
|||
class GMEngine(PILEngine):
|
||||
|
||||
def create(self, image, geometry, options):
|
||||
"""
|
||||
Processing conductor, returns the thumbnail as an image engine instance
|
||||
"""
|
||||
image = self.cropbox(image, geometry, options)
|
||||
image = self.orientation(image, geometry, options)
|
||||
image = self.colorspace(image, geometry, options)
|
||||
image = self.remove_border(image, options)
|
||||
image = self.scale(image, geometry, options)
|
||||
image = self.crop(image, geometry, options)
|
||||
image = self.rounded(image, geometry, options)
|
||||
image = self.blur(image, geometry, options)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user