-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-size-sort.py
More file actions
executable file
·67 lines (44 loc) · 1.79 KB
/
Copy pathimage-size-sort.py
File metadata and controls
executable file
·67 lines (44 loc) · 1.79 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
#! /usr/local/bin/python3
# https://www.geeksforgeeks.org/filtering-images-based-size-attributes-python/
from PIL import Image
from shutil import copyfile
import os, os.path
from pathlib import Path
def filterImages(path, thresholdWidth, thresholdHeight):
print("Welcome")
# array for identifying image files
imgs = []
# supported extensions
valid_images = [".jpg",".JPG", ".jpeg", ".JPEG", ".png", ".PNG"]
# read images into array
for f in os.listdir(path):
ext = os.path.splitext(f)[1]
if ext.lower() not in valid_images:
continue
imgs.append(f)
# check for and/or create destination folder
directory = os.path.dirname(path) + '/archived-trash/'
# something failing here. didn't investigate yet. not sure why but it was silently
# failing to make the directory. i just created manually.
if not os.path.exists(directory):
os.makedirs(directory)
# array for filtered images
filteredImages = []
for i in imgs:
image = Image.open(os.path.join(path, i))
# store dimensions
width, height = image.size
if (width < thresholdWidth and height < thresholdHeight):
copyfile(os.path.join(path, i), os.path.join(path + '/archived-trash', i))
elif (width < thresholdWidth and height >= thresholdHeight):
copyfile(os.path.join(path, i), os.path.join(path + '/archived-trash', i))
elif (width >= thresholdWidth and height < thresholdHeight):
copyfile(os.path.join(path, i), os.path.join(path + '/archived-trash', i))
# so you know its working
print(image.filename + " " + str(image.size))
filteredImages.append(i)
return filteredImages
if __name__ == '__main__':
filteredImages = []
# defaults. should probably take these as paramaters by cba atm.
filteredImages = filterImages("/Users/warrenkopp/githubs/desktops", 2560, 1440)