mirror of
https://github.com/prise6/smart-iss-posts
synced 2024-05-18 13:26:33 +02:00
Ajout du script resize
This commit is contained in:
parent
c54226eca5
commit
ded9753af9
5
Makefile
5
Makefile
|
@ -33,6 +33,11 @@ data: requirements
|
||||||
sync_collections:
|
sync_collections:
|
||||||
$(PYTHON_INTERPRETER) src/data/sync_collections.py
|
$(PYTHON_INTERPRETER) src/data/sync_collections.py
|
||||||
|
|
||||||
|
## Resize collection
|
||||||
|
resize_collections:
|
||||||
|
$(PYTHON_INTERPRETER) src/data/resize_collections.py
|
||||||
|
|
||||||
|
|
||||||
## Delete all compiled Python files
|
## Delete all compiled Python files
|
||||||
clean:
|
clean:
|
||||||
find . -type f -name "*.py[co]" -delete
|
find . -type f -name "*.py[co]" -delete
|
||||||
|
|
48
src/data/resize_collections.py
Normal file
48
src/data/resize_collections.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import click
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from dotenv import find_dotenv, load_dotenv
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
load_dotenv(find_dotenv())
|
||||||
|
|
||||||
|
@click.command()
|
||||||
|
@click.argument('RESIZE_WIDTH', type=int, default=os.getenv('RESIZE_WIDTH'))
|
||||||
|
@click.argument('RESIZE_HEIGHT', type=int, default=os.getenv('RESIZE_HEIGHT'))
|
||||||
|
def main(resize_width, resize_height):
|
||||||
|
""" Resize image
|
||||||
|
"""
|
||||||
|
|
||||||
|
logger.info('Resize collections to {}x{}'.format(resize_width, resize_height))
|
||||||
|
|
||||||
|
try:
|
||||||
|
imgs_path = os.path.join(str(project_dir), 'data', 'external', 'collections')
|
||||||
|
[resize_one_img(os.path.join(imgs_path, img_path), resize_width, resize_height) for img_path in os.listdir(imgs_path)]
|
||||||
|
except:
|
||||||
|
logger.error(sys.exc_info()[0])
|
||||||
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
def resize_one_img(img_path, resize_width, resize_height):
|
||||||
|
|
||||||
|
logger.info('Resize {}'.format(img_path))
|
||||||
|
size = (resize_width, resize_height)
|
||||||
|
outfile = os.path.join(str(project_dir), 'data', 'interim', 'collections', os.path.basename(img_path))
|
||||||
|
try:
|
||||||
|
im = Image.open(os.path.join(str(project_dir), img_path))
|
||||||
|
im.thumbnail(size)
|
||||||
|
im.save(outfile, "JPEG")
|
||||||
|
except IOError:
|
||||||
|
logger.info('Cannot resize {}'.format(img_path))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
log_fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||||
|
logging.basicConfig(level=logging.INFO, format=log_fmt)
|
||||||
|
|
||||||
|
project_dir = Path(__file__).resolve().parents[2]
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
main()
|
|
@ -25,7 +25,6 @@ def main(r_collections_project):
|
||||||
logger.error(sys.exc_info()[0])
|
logger.error(sys.exc_info()[0])
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
return(1)
|
|
||||||
|
|
||||||
def get_unique_imgs(r_collections_project):
|
def get_unique_imgs(r_collections_project):
|
||||||
|
|
||||||
|
@ -58,8 +57,6 @@ def cp_imgs(r_collections_project, imgs):
|
||||||
os.path.join(str(project_dir), "data", "external", "collections", img)
|
os.path.join(str(project_dir), "data", "external", "collections", img)
|
||||||
)
|
)
|
||||||
|
|
||||||
return(1)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
log_fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
log_fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||||
|
|
Loading…
Reference in a new issue