-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgtree.py
executable file
·172 lines (142 loc) · 4.59 KB
/
imgtree.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/env python
import os, sys, glob, getopt
from colorama import init, Fore, Back
from pprint import pprint
import ffmpeg
import subprocess
import shutil
from PIL import Image, ImageOps, UnidentifiedImageError, ImageFile
import proclib as p
import proclib
from proclib import prun, splitnonalpha, getID, procTime, getFilesize
import time
from colorama import Fore, init
init()
ImageFile.LOAD_TRUNCATED_IMAGES = True
timestamp = round(time.time())
def showhelp():
print("help")
rs = '''
-h, --help show help
-d, --dir dir
'''
print(rs)
exit()
locationpath = os.getcwd()
dirname = False
basename = False
verbose = False
# print("args:",len(sys.argv))
if len(sys.argv) == 0:
showhelp()
#^ if there is only one argument with no flags, then assume it is a filename
if len(sys.argv) <3:
# print("1args")
# pprint(sys.argv)
filename = sys.argv[0]
basename = os.path.basename(locationpath)
dirname = os.path.dirname(locationpath)
fspec = False
else:
print("multiargs")
pprint(sys.argv)
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv, "hvd:", [
"help",
"verbose",
"dir",
])
except Exception as e:
print(str(e))
for opt, arg in opts:
if opt in ("-h", "--help"):
showhelp();
if opt in ("-v", "--verbose"):
debug = True;
if opt in ("-d", "--dir"):
locationpath = True;
basename,dirname,fspec,srcfile = proclib.getFnames(locationpath)
p.errprint(f"dirname: {dirname}")
p.errprint(f"basename: {basename}")
p.errprint(f"srcfile:{srcfile}")
p.errprint(f"fspec:{fspec}")
id = getID(filename)
p.errprint(f"ID: [{id}]")
#-------------------------------------------------------------------------
#^ first make thumbs
css = """
<style>
img {
float: left;
width: 50px;
/* height: 50px;*/
/* prob. you dont need this */
padding: 5px;
margin: 5px;
border:1px solid red;
}
</style>
"""
dirs = [x[0] for x in os.walk(dirname) if x[0] != "."]
# all_imgs = False
ct = 1
border_color = "black"
topfile = open("ALL_ALLFILES.html","w")
for d in dirs:
png_imgs = glob.glob(f"{d}/*.png")
gif_imgs = glob.glob(f"{d}/*.gif")
jpg_imgs = glob.glob(f"{d}/*.jpg") #^ too many
all_imgs = png_imgs+jpg_imgs+gif_imgs
# all_imgs = png_imgs+jpg_imgs
#^ now have all images in this dir
# print(len(all_imgs))
cropped_imgs = []
for i in all_imgs:
if i.find("cropped") != -1:
cropped_imgs.append(i)
all_imgs = cropped_imgs
#^ we really onyl want to cropped images
if len(all_imgs) > 0:
if not os.path.exists(f"/tmp{d}"):
p.errprint(Fore.CYAN + f"making: /tmp{d}" + Fore.RESET)
try: os.makedirs(f"/tmp{d}")
except: pass
#^ create thumbs
page = open(f"{d}/_ALLIMGS.html","w")
page.write(css)
page.write(f"\n<h2>{d}</h2>\n")
all_imgs = sorted(all_imgs)
for img in all_imgs:
timg = f"/tmp{img}"
if timg.find(".png") != -1:
border_color="pink"
if timg.find(".gif") != -1:
border_color="green"
if timg.find(".jpg") != -1:
border_color="black"
if not os.path.exists(timg):
try:
pimg = Image.open(img)
pimg.thumbnail((50,50))
if verbose:
p.errprint(Fore.GREEN + f"({ct}) Saving to: {timg}" + Fore.RESET)
else:
p.errprint(ct,end="\r")
pimg.save(timg)
page.write(f"<a target='_blank' href='{img}'><img style='border:2px solid {border_color}' src='{timg}'></a>\n")
# print(f"<a target='_blank' href='{img}'><img style='border:2px solid {border_color}' src='{timg}'></a>",end="")
except UnidentifiedImageError:
os.remove(img)
p.errprint(Fore.MAGENTA+f"Removing: {img}"+Fore.RESET)
except Image.DecompressionBombError:
p.errprint(Fore.RED+f"Image too big: {img}"+Fore.RESET)
except Image.DecompressionBombWarning:
p.errprint(Fore.RED+f"Image too big: {img}"+Fore.RESET)
ct += 1
page.write("<div style='clear: both'></div>\n")
page.close()
p.errprint(Fore.WHITE + f"Wrote file {d}/_ALLIMGS.html" + Fore.RESET)
topfile.write(f"<a href='{d}/_ALLIMGS.html' target='_blank'>{d}</a></br>\n")
topfile.flush()
topfile.close()