-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathprocess-images.py
74 lines (60 loc) · 1.94 KB
/
process-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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import imageio
import glob
from tqdm import tqdm
from PIL import Image
import os
import logging
import logging.config
#SOURCE = "/Users/jheaton/Downloads/kaggle-blocks"
#TARGET = "/Users/jheaton/Downloads/kaggle-convert"
#SOURCE = "/mnt/d/data/scifi/70sscifiart"
#TARGET = "/mnt/d/data/scifi/scifi-crop"
SOURCE = "/mnt/d/data/minecraft/1_sampled/"
TARGET = "/mnt/d/data/minecraft/2_scaled_1024"
def crop_square(image):
width, height = image.size
# Crop the image, centered
new_width = min(width,height)
new_height = new_width
left = (width - new_width)/2
top = (height - new_height)/2
right = (width + new_width)/2
bottom = (height + new_height)/2
return image.crop((left, top, right, bottom))
def scale(img, scale_width, scale_height):
# Scale the image
img = img.resize((
scale_width,
scale_height),
Image.ANTIALIAS)
return img
def standardize(image):
rgbimg = Image.new("RGB", image.size)
rgbimg.paste(image)
return rgbimg
def fail_below(image, check_width, check_height):
width, height = image.size
assert width == check_width
assert height == check_height
logging.config.fileConfig("logging.properties")
files = glob.glob(os.path.join(SOURCE,"*.jpg"))
for file in tqdm(files):
try:
target = ""
name = os.path.basename(file)
filename, _ = os.path.splitext(name)
img = Image.open(file)
img = standardize(img)
img = crop_square(img)
img = scale(img, 1024, 1024)
#fail_below(img, 1024, 1024)
target = os.path.join(TARGET,filename+".jpg")
img.save(target, quality=25)
except KeyboardInterrupt:
print("Keyboard interrupt")
break
except AssertionError:
print("Assertion")
break
except:
logging.warning(f"Unexpected exception while processing image source: {file}, target: {target}" , exc_info=True)