remove debug info. Cleanup code. Set default quality
This commit is contained in:
parent
77cca6c6d7
commit
710eb274ed
|
|
@ -1,23 +1,16 @@
|
||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.conf import settings
|
|
||||||
|
|
||||||
from utils.methods import get_url_images_in_text, get_page_size_by_url
|
from utils.methods import get_url_images_in_text, get_image_meta_by_url
|
||||||
from news.models import News
|
from news.models import News
|
||||||
from sorl.thumbnail import get_thumbnail
|
from sorl.thumbnail import get_thumbnail
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
SORL_THUMBNAIL_ALIASES = 'news_description'
|
|
||||||
IMAGE_MAX_SIZE_IN_BYTES = 1048576 # ~ 1mb
|
IMAGE_MAX_SIZE_IN_BYTES = 1048576 # ~ 1mb
|
||||||
|
IMAGE_QUALITY_PERCENTS = 50
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
parser.add_argument(
|
|
||||||
'-a',
|
|
||||||
'--alias',
|
|
||||||
default=self.SORL_THUMBNAIL_ALIASES,
|
|
||||||
help='Ключ для параметров оптимизации',
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-s',
|
'-s',
|
||||||
'--size',
|
'--size',
|
||||||
|
|
@ -25,11 +18,18 @@ class Command(BaseCommand):
|
||||||
help='Максимальный размер файла в байтах',
|
help='Максимальный размер файла в байтах',
|
||||||
type=int
|
type=int
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'-q',
|
||||||
|
'--quality',
|
||||||
|
default=self.IMAGE_QUALITY_PERCENTS,
|
||||||
|
help='Качество изображения',
|
||||||
|
type=int
|
||||||
|
)
|
||||||
|
|
||||||
def optimize(self, text, alias, max_size):
|
def optimize(self, text, max_size, max_quality):
|
||||||
"""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, width, height = get_page_size_by_url(image)
|
size, width, height = get_image_meta_by_url(image)
|
||||||
|
|
||||||
if size < max_size:
|
if size < max_size:
|
||||||
continue
|
continue
|
||||||
|
|
@ -40,7 +40,8 @@ class Command(BaseCommand):
|
||||||
optimized_image = get_thumbnail(
|
optimized_image = get_thumbnail(
|
||||||
file_=image,
|
file_=image,
|
||||||
geometry_string=f'{width}x{height}',
|
geometry_string=f'{width}x{height}',
|
||||||
upscale=False
|
upscale=False,
|
||||||
|
quality=max_quality
|
||||||
).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'
|
||||||
|
|
@ -49,21 +50,14 @@ class Command(BaseCommand):
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
alias = options['alias']
|
|
||||||
size = options['size']
|
size = options['size']
|
||||||
|
quality = options['quality']
|
||||||
|
|
||||||
if alias not in settings.SORL_THUMBNAIL_ALIASES:
|
for news in News.objects.all():
|
||||||
self.stdout.write(self.style.ERROR(f'{alias} not found in alias'))
|
if not isinstance(news.description, dict):
|
||||||
return
|
continue
|
||||||
|
news.description = {
|
||||||
self.optimize('https://upload.wikimedia.org/wikipedia/commons/b/b9/Pizigani_1367_Chart_1MB.jpg',
|
locale: self.optimize(text, size, quality)
|
||||||
'news_description', size)
|
for locale, text in news.description.items()
|
||||||
|
}
|
||||||
# for news in News.objects.all():
|
news.save()
|
||||||
# if not isinstance(news.description, dict):
|
|
||||||
# continue
|
|
||||||
# news.description = {
|
|
||||||
# locale: self.optimize(text, alias, size)
|
|
||||||
# for locale, text in news.description.items()
|
|
||||||
# }
|
|
||||||
# news.save()
|
|
||||||
|
|
|
||||||
|
|
@ -177,7 +177,7 @@ 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) -> (int, int, int):
|
def get_image_meta_by_url(url) -> (int, int, int, str):
|
||||||
"""Returns image size (bytes, width, height)"""
|
"""Returns image size (bytes, width, height)"""
|
||||||
image_raw = requests.get(url)
|
image_raw = requests.get(url)
|
||||||
image = Image.open(BytesIO(image_raw.content))
|
image = Image.open(BytesIO(image_raw.content))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user