-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize.py
More file actions
80 lines (64 loc) · 1.98 KB
/
resize.py
File metadata and controls
80 lines (64 loc) · 1.98 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
70
71
72
73
74
75
76
77
78
79
80
import os
import constants
import numpy as np
from scipy import misc, ndimage
import os
ORIGINAL = 'dataset-original'
BRAND_NEW = 'dataset-resized'
FILE_NAME = 'test_img'
cwd = os.getcwd()
img_path = cwd + '/' + FILE_NAME + '.jpg'
def single_img_resize(image):
if len(image) > 4 and image[-4:] == '.jpg':
pic = misc.imread(image)
dim1 = len(pic)
dim2 = len(pic[0])
if dim1 > dim2:
pic = np.rot90(pic)
picResized = resize(pic,constants.DIM1, constants.DIM2)
misc.imsave(image, picResized)
def resize(image, dim1, dim2):
return misc.imresize(image, (dim1, dim2))
def resize_all_in_dir():
prepath = os.path.join(os.getcwd(), original)
glassDir = os.path.join(prepath, 'glass')
paperDir = os.path.join(prepath, 'paper')
cardboardDir = os.path.join(prepath, 'cardboard')
plasticDir = os.path.join(prepath, 'plastic')
metalDir = os.path.join(prepath, 'metal')
trashDir = os.path.join(prepath, 'trash')
destPath = os.path.join(os.getcwd(), brand_new)
try:
os.makedirs(destPath)
except OSError:
if not os.path.isdir(destPath):
raise
#GLASS
file_walk(glassDir, os.path.join(destPath, 'glass'))
#PAPER
file_walk(paperDir, os.path.join(destPath, 'paper'))
#CARDBOARD
file_walk(cardboardDir, os.path.join(destPath, 'cardboard'))
#PLASTIC
file_walk(plasticDir, os.path.join(destPath, 'plastic'))
#METAL
file_walk(metalDir, os.path.join(destPath, 'metal'))
#TRASH
file_walk(trashDir, os.path.join(destPath, 'trash'))
def file_walk(directory, destPath):
try:
os.makedirs(destPath)
except OSError:
if not os.path.isdir(destPath):
raise
for subdir, dirs, files in os.walk(directory):
for file in files:
if len(file) <= 4 or file[-4:] != '.jpg':
continue
pic = misc.imread(os.path.join(subdir, file))
dim1 = len(pic)
dim2 = len(pic[0])
if dim1 > dim2:
pic = np.rot90(pic)
picResized = resize(pic,constants.DIM1, constants.DIM2)
misc.imsave(os.path.join(destPath, file), picResized)