-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.py
58 lines (52 loc) · 1.76 KB
/
images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import logging
import os
from PIL import Image
from pdf2image import convert_from_path # import library
class ImagesManager:
def clean_all_min_webp():
"""
Clean all WebP files in path
"""
os.system("rm -rf data/**/*.webp")
def __pdf_to_webp(pdf_file):
"""
Convert a PDF file to a WebP file using PIL
"""
images = convert_from_path(pdf_file) # Read pdf file
for image in images:
image.save(os.path.splitext(pdf_file)[0] + ".webp", "WEBP")
def __image_to_webp(png_file):
"""
Convert an files to a WebP file using PIL
"""
image = Image.open(png_file)
image.save(os.path.splitext(png_file)[0] + ".webp", "WEBP")
@classmethod
def convert_to_webp(cls, file):
"""
Convert a file to a WebP file using PIL
"""
try:
if file.endswith(".pdf"):
cls.__pdf_to_webp(file)
# else if end with png or jpg or jpeg or gif
elif file.endswith(".png") or file.endswith(".jpg") or file.endswith(".jpeg") or file.endswith(".gif"):
cls.__image_to_webp(file)
else:
logging.info("File type not supported")
except Exception as e:
logging.error(e)
@classmethod
def compress_webp(cls, file):
"""
Compress a WebP file using PIL to 512x512 with a quality of 80%
"""
try:
if file.endswith(".webp"):
image = Image.open(file)
image.thumbnail((512, 512), Image.ANTIALIAS)
image.save(file, "WEBP", quality=80)
else:
logging.info("File type not supported")
except Exception as e:
logging.error(e)