-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompressModule.py
More file actions
69 lines (57 loc) · 2.39 KB
/
Copy pathcompressModule.py
File metadata and controls
69 lines (57 loc) · 2.39 KB
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
59
60
61
62
63
64
65
66
67
68
69
import os, math
from PIL import Image, ImageFile, ImageOps
import pillow_heif
#antisipasi file rusak
ImageFile.LOAD_TRUNCATED_IMAGES = True
#untuk gambar HEIF/HEIC
pillow_heif.register_heif_opener()
def compressImage(fPath, outDir: os.PathLike,
max_pixels:int=12_000_000, quality:int=85, delOriFile:bool=False):
"""Return namaFile, sizeAkhir, Dihemat, Status, DisimpanDi, DIhapus (0 stip 1 centang 2 silang)"""
# default to 12mp 85% quality
namaFile = ""
statusKompresi = 0
fileSizeAwal = 0
fileSizeAkhir = 0
penghematanSize = 0
outPath = False
fileDeleted = 0
try:
fileSizeAwal = os.path.getsize(fPath)
namaFile = os.path.basename(fPath)
namaFileSplit = os.path.splitext(namaFile)
outPath = os.path.join(outDir,
f"{namaFileSplit[0]}-Compressed{namaFileSplit[1].lower()}")
imgObj = Image.open(fPath)
imgObj = ImageOps.exif_transpose(imgObj)
if imgObj.mode in ('RGBA', 'LA') or (imgObj.mode == 'P' and 'transparency'
in imgObj.info):
alpha = imgObj.convert('RGBA').split()[-1]
emptyBg = Image.new("RGB", imgObj.size, (255,255,255))
emptyBg.paste(imgObj, mask=alpha)
imgObj = emptyBg
elif imgObj.mode != 'RGB':
imgObj = imgObj.convert('RGB')
w,h = imgObj.size
currentPixels = w*h
if currentPixels > max_pixels:
rasio = math.sqrt(max_pixels/currentPixels)
widthBaru = int(w*rasio)
heightBaru = int(h*rasio)
imgObj = imgObj.resize((widthBaru, heightBaru),
Image.Resampling.LANCZOS)
imgObj.save(outPath, 'JPEG',
quality=quality, optimize=True)
imgObj.close()
statusKompresi = 1
fileSizeAkhir = os.path.getsize(outPath)
penghematanSize = fileSizeAwal - fileSizeAkhir
if delOriFile == True:
try:
os.remove(fPath)
fileDeleted = 1
except (FileNotFoundError, PermissionError, OSError, IsADirectoryError):
fileDeleted = 2
return (namaFile, statusKompresi, fileSizeAkhir, penghematanSize, outPath, fileDeleted)
except Exception as e:
return (namaFile, statusKompresi, fileSizeAkhir, penghematanSize, outPath, fileDeleted)