-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
158 lines (127 loc) · 4.32 KB
/
main.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
#!/usr/bin/env python
# Author: Christopher Bull.
# Affiliation: Climate Change Research Centre and ARC Centre of Excellence for Climate System Science.
# Level 4, Mathews Building
# University of New South Wales
# Sydney, NSW, Australia, 2052
# Contact: [email protected]
# www: christopherbull.com.au
# Date created: Tue, 02 Feb 2016 16:02:13
# Machine created on: ccrc165
#
#see: https://github.com/docopt/docopt
#round brackets mean required square are optional
#download docopt from...
#https://raw.githubusercontent.com/docopt/docopt/master/docopt.py
"""
Tiny python package to help save metadata to a file such that you know what script created the file!
Usage:
main.py -h
main.py PNGPATH...
Options:
-h,--help : show this help message
PNGPATH : path to pngfile(s) you want the metadata for
Examples:
1] python main.py examples/test.png
2] See examples/pyeg.py
"""
from PIL import Image
from PIL import PngImagePlugin
import os
class AddTrkr(object):
"""
Class to add some image metadata too
Parameters
----------
pngpath:
metadata (optional): whatever metadata you want to record to the file, if left blank, cpath is required. If blank will record name of file, machine run on and time, will also save the figure for you!
cpath (required if metadata={}, otherwise leave empty): specify path of file that called this function
Returns
-------
Notes
-------
Example
--------
>>> #using custom metadata
>>> import imgtrkr as it
>>> it.AddTrkr('/home/nfs/z3457920/hdrive/repos/test.png',{'moogy':'sdfasdf'})
>>>
>>> #using automated metadata (will save the png too!)
>>> import imgtrkr as it
>>> it.AddTrkr('/home/nfs/z3457920/hdrive/repos/test.png',{},cpath=os.path.realpath(__file__))
"""
def __init__(self, pngpath,metadata={},cpath=''):
#super(AddTrkr, self).__init__()
self.pngpath = pngpath
self.metadata = metadata
self.cpath = cpath
self.addmet()
def addmet(self):
"""function that actually adds the metadata
:returns: @todo
"""
#f = "test.png"
#METADATA = {"version":"1.0", "OP":"ihuston"}
## Create a sample image
#import pylab as plt
#import numpy as np
#X = np.random.random((50,50))
#plt.imshow(X)
#plt.savefig(f)
# Use PIL to save some image metadata
#adding custom metadata
if self.metadata!={}:
im = Image.open(self.pngpath)
meta = PngImagePlugin.PngInfo()
for x in self.metadata:
meta.add_text(x, self.metadata[x])
im.save(self.pngpath, "png", pnginfo=meta)
else:
import matplotlib.pyplot as plt
import datetime
import socket
self.metadata={'Created with':self.cpath,'time':datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),'machine':socket.gethostname()}
plt.savefig(self.pngpath,dpi=300)
im = Image.open(self.pngpath)
meta = PngImagePlugin.PngInfo()
for x in self.metadata:
meta.add_text(x, self.metadata[x])
im.save(self.pngpath, "png", pnginfo=meta)
return
class RdTrkr(object):
"""
Class to add some image metadata too
Parameters
----------
pngpath:
metadata:
Returns
-------
Notes
-------
Example
--------
>>> import imgtrkr as it
>>> it.RdTrkr('/home/nfs/z3457920/hdrive/repos/test.png')
"""
def __init__(self, pngpath):
self.pngpath = pngpath
im2 = Image.open(self.pngpath)
#_lg.info(im2.info)
# import pdb; pdb.set_trace()
_lg.info("File: "+os.path.basename(self.pngpath)+" has metadata: "+str(im2.info))
# print im2.info
if __name__ == "__main__":
from docopt import docopt
arguments = docopt(__doc__)
from _cblogger import _LogStart
_lg=_LogStart().setup()
#if using argpasser
if len(arguments['PNGPATH'])==1:
RdTrkr(arguments['PNGPATH'][0])
elif len(arguments['PNGPATH'])>1:
for pngp in arguments['PNGPATH']:
RdTrkr(pngp)
else:
from ._cblogger import _LogStart
_lg=_LogStart().setup()