-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageCompression.py
More file actions
62 lines (45 loc) · 1.55 KB
/
ImageCompression.py
File metadata and controls
62 lines (45 loc) · 1.55 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
from multiprocessing.dummy import Process
import imutils
from importlib.resources import path
import PIL
from PIL import Image, ImageOps
import os
import tkinter as tk
from tkinter import filedialog
from pathlib import Path
import multiprocessing
from multiprocessing import Pool
import glob
import time
from itertools import repeat
import shutil
root = tk.Tk()
root.withdraw()
inputfilepath = filedialog.askdirectory()
inputfolder = inputfilepath + '/'
outputfolder = Path(inputfolder).parent / 'Output/'
print(outputfolder)
outputfolder.mkdir(exist_ok = True)
print(inputfolder)
# Image compression quality (70 appears to be the best), lower the value, lower the size and quality and vice versa
quality = 70
# Image compression function
def compress_images(inputfile):
# Open every image:
img = Image.open(inputfile)
img = ImageOps.exif_transpose(img)
filename = inputfile.split('/')[-1]
# Compress every image and save it with a new name:
img.save(outputfolder / filename, optimize=True, quality=quality)
# Using the multithreading module
if __name__ == '__main__':
filelst = []
for filename in glob.iglob(inputfolder + '/**', recursive=True):
if os.path.isfile(filename) and filename.endswith('jpg'):
filelst.append(filename)
print(filelst)
with multiprocessing.Pool(multiprocessing.cpu_count()) as pool:
st = time.time()
pool.map(compress_images, filelst)
et = time.time()
print('Time Taken Map:', et - st)