-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamsort.py
executable file
·93 lines (83 loc) · 3.73 KB
/
camsort.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
"""
Script to rename and sort camera stills in current working directory based on timestamp
Also compresses using jpegtran if possible
i-ghost
"""
import os
import re
import calendar
import subprocess
from progressmeter import ProgressMeter
try:
from PIL import Image, ImageFont, ImageDraw
def txt_img(image, text, font="//usr//share//fonts//truetype//DroidSansMono.ttf", size=14):
BLACK = "#000000"
FONT = ImageFont.truetype(font, 14, encoding="unic")
LINEWIDTH = 20
img = Image.open(image)
imgbg = Image.new("RGBA", img.size, BLACK)
mask = Image.new("L", img.size, BLACK)
draw = ImageDraw.Draw(img)
drawmask = ImageDraw.Draw(mask)
drawmask.line((0, img.size[1] - (LINEWIDTH / 2), 85, img.size[1] - (LINEWIDTH / 2)), fill="#999999", width=LINEWIDTH)
img.paste(imgbg, mask=mask)
draw.text((10, ((img.size[1] - LINEWIDTH) + 2)), text, font=FONT, fill="#ffffff")
img.save(image, "JPEG", quality=100)
except:
txt_img = None
print("PIL not available: timestamps will not be written to images!")
if subprocess.call(["which", "jpegtran"], stdout=open(os.devnull, "w")) == 0:
jpegtran = True
total_files = len([i for i in os.listdir(os.getcwd()) if i.endswith(".jpg") and os.path.isfile(i)])
progressbar = ProgressMeter(total=total_files, unit="images")
for i in os.listdir(os.getcwd()):
progressbar.update(1)
total_files -= 1
if os.path.getsize(i) == 0:
# zero byte file, discard
os.remove(i)
continue
if i.endswith(".jpg"):
match = re.search(r"(\w+)(\(\w+\))\w(\d)\w(\d+)\w(\d+)", i, re.IGNORECASE)
#match.group(1) # Hex string ?
#match.group(2) # Camera name
#match.group(3) # Unknown bool
#match.group(4) # Timestamp (yyyymmddhhmmss)
#match.group(5) # Progressive counter, used for sorting
if match:
camera = {
"name" : match.group(2).lstrip("(").rstrip(")"),
# Timestamp
"year" : int(match.group(4)[:4]),
"month" : int(match.group(4)[4:6]),
"date" : match.group(4)[6:8],
"hour" : match.group(4)[8:10],
"minute" : match.group(4)[10:12],
"second" : match.group(4)[12:14],
"bool" : int(match.group(3))
}
# the actual weekday derived from date
camera["weekday"] = calendar.weekday(camera["year"], camera["month"], int(camera["date"]))
DIR_LEAF = "%s//%s//%d//%02d %s//%s_%s" % (
os.getcwd(),
camera["name"],
camera["year"],
camera["month"],
calendar.month_abbr[camera["month"]],
camera["date"],
calendar.day_abbr[camera["weekday"]]
)
final_file_name = "%s-%s-%s.jpg" % (camera["hour"], camera["minute"], camera["second"])
if not os.path.exists(DIR_LEAF):
os.makedirs(DIR_LEAF) # os.renames() provides this, but having it here saves having multiple conditionals below
if txt_img:
try:
txt_img(i, final_file_name[:-4].replace("-", ":"))
except IOError:
os.remove(i)
continue
if jpegtran:
subprocess.call(["jpegtran", "-o", "-copy", "none", "-progressive", "-outfile", os.path.join(DIR_LEAF, final_file_name), i], stdout=open(os.devnull, "w"), stderr=open(os.devnull, "w"))
os.remove(i)
else:
os.rename(i, os.path.join(DIR_LEAF, final_file_name))