From c9ba6add63a945814815b473a0012f7c29327fbf Mon Sep 17 00:00:00 2001 From: Xevion Date: Wed, 7 Aug 2019 23:54:31 -0500 Subject: [PATCH] restructure --- main.py | 26 ++--------- package/__pycache__/app.cpython-37.pyc | Bin 0 -> 852 bytes package/app.py | 26 +++++++++++ package/xmp.py | 57 +++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 22 deletions(-) create mode 100644 package/__pycache__/app.cpython-37.pyc create mode 100644 package/app.py create mode 100644 package/xmp.py diff --git a/main.py b/main.py index ee4a4b3..9974a52 100644 --- a/main.py +++ b/main.py @@ -1,23 +1,5 @@ -import io, sys, os -# Imports the Google Cloud client library -from google.cloud import vision -from google.cloud.vision import types - -# Instantiates a client -client = vision.ImageAnnotatorClient() - -# The name of the image file to annotate -path = os.path.join(sys.path[0], 'DSC_6315.jpg') - -# Loads the image into memory -with io.open(path, 'rb') as image_file: - content = image_file.read() - -image = types.Image(content=content) - -# Performs label detection on the image file -response = client.label_detection(image=image) -labels = response.label_annotations - -print([label.description for label in labels]) \ No newline at end of file +if __name__ == "__main__": + import os + from package.app import run + os.exit(app.run()) \ No newline at end of file diff --git a/package/__pycache__/app.cpython-37.pyc b/package/__pycache__/app.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f7a99d58648b3420e972583fc09709aa105eae5c GIT binary patch literal 852 zcmYjP&2H2%5VjpBO}D!(w3YaIgFUbUA&@|;70^luAytJvfUK>mG&X5M5*yn^+A4dZ zSA=+j_6TplJMaX#a^e+wVkWDKjx=L?Jf8W!@w^xedIaS6&({aP7$JY~a3ydi&p_QS z7)c~8iCC^w(C*9xgO!y*#RaE$7nETYiHH)#&sQ#ph$WZd84;_JPRTI(3z^8?khan1 z+~vB4UFhdVxfCx@BG6UP9Z+Y$cv8@F=7WMtdIySKum$nFAzvsZK0J>aD0;!ZtuJUp zvE+ii10Zk6Tk@XxxL9qN-m?_y zp7wXAJBN=SK6o@LOeUB;0Ad6+T3_#&ssLRFTyf8Ql&-aowB>f0DW{EK)Gr#PO%iQ@?S7ve-ksz#yoZhl0^=Mq`G%4K#HdisjLCdsj zxk>zS%L|>?ZQ$mv4UNrf-?Ch{TpLxl+^R%cBq+jHNW6{GGKWrxjSl^xR9>Y>O59vJ zlDgJDL5~|ob`4+lx|SV&$V+88IBbaC*eYirN{Og@>Hc@Z#t_g303N5*-2g*^Z5jvb zbd&XiK8={&0^hJ_H(;>?G#D!SS=X#lTIv&Nu?u4Bx@)wYza^vXj?wcaU~P9gVirHK d%YF?{_eUM5cwA|DQmP3KfJ4`9z|#+6_74-G(Psbv literal 0 HcmV?d00001 diff --git a/package/app.py b/package/app.py new file mode 100644 index 0000000..69ba3dd --- /dev/null +++ b/package/app.py @@ -0,0 +1,26 @@ +import io, sys, os + +# Imports the Google Cloud client library +from google.cloud import vision +from google.cloud.vision import types + +# Instantiates a client +client = vision.ImageAnnotatorClient() + +def run(): + + # The name of the image file to annotate + path = os.path.join(sys.path[0], 'DSC_6315.jpg') + print(path) + + # Loads the image into memory + with io.open(path, 'rb') as image_file: + content = image_file.read() + + image = types.Image(content=content) + + # Performs label detection on the image file + response = client.label_detection(image=image) + labels = response.label_annotations + + print([label.description for label in labels]) \ No newline at end of file diff --git a/package/xmp.py b/package/xmp.py new file mode 100644 index 0000000..7a5cc0c --- /dev/null +++ b/package/xmp.py @@ -0,0 +1,57 @@ +# from libxmp import file_to_dict +import os, sys, re + +basepath = "E:\\Photography\\Colorado 2019\\" +ext_filter = lambda file : file.endswith('.NEF') +mainPattern = r'\n\s+\n(?: .+<\/rdf:li>\n)*\s+<\/rdf:Bag>\n\s+<\/dc:subject>\n' +subPattern = r'(.*)\n' +subFormatPattern = ' {}\n' + +def getXMP(file, basepath=None): + xmpFile = file.rfind('.') + xmpFile = file[:xmpFile] + '.xmp' + if basepath: + xmpFile = os.path.join(basepath, xmpFile) + return xmpFile + +def writeTags(tags, filename): + fullpath = os.path.join(basepath, getXMP(filename)) + if not os.path.exists(fullpath): + raise FileNotFoundError(f'No XMP file found for {filename}.') + data = open(fullpath, 'r').read() + + # Detect and find the \n \n \n """ + look = data.find('') + 13 + data = data[:look] + addition + data[look:] + match = re.search(mainPattern, data) + + print(os.path.join(basepath, filename)) + print(fullpath) + print('. . .') + + # Get last matching tag + submatch = None + for submatch in re.finditer(subPattern, match.group(0)): + pass + # Calculate very end + spanend = match.span()[0] + (submatch.span()[1] if submatch else 0) + # add tags to end + data = data[:spanend] + ''.join(subFormatPattern.format(tag) for tag in tags) + data[spanend:] + # Write file to disk + open(fullpath, 'w').write( data) + +files = os.listdir(basepath) +print(f'Total Files: {len(files)}') +files = list(filter(ext_filter, files)) +print(f'Total Files after .NEF filter: {len(files)}') + +xmps = [file for file in files if os.path.exists(getXMP(file, basepath=basepath))] +print(f'Total .NEF files with .XMP pair: {len(xmps)}') +print(f'{len(xmps)} x 2 == {len(xmps) * 2}') + +select = files[43] +writeTags(['helpgod', 'ohfuck'], select) +print(select) \ No newline at end of file