Skip to content

Instantly share code, notes, and snippets.

@timkeller
Last active August 29, 2015 14:03
Show Gist options
  • Save timkeller/715dfe6cf0060ecd126b to your computer and use it in GitHub Desktop.
Save timkeller/715dfe6cf0060ecd126b to your computer and use it in GitHub Desktop.
Export EXIF data as a simple string
# EXIF Exporter
# ==============
# Exports EXIF data in a nice string format. For example:
# "Canon EOS 650D, EF-S18-55mm f/3.5-5.6 IS II, 1/200, f/9, ISO 100"
#
# It will also copy it to your clipboard
#
# Make sure you have PIL or Pillow installed
# This probably means you'll need to run:
# pip install Pillow
from PIL import Image
import sys
def get_formatted_exif(file_path):
img = Image.open(file_path)
exif = img._getexif()
return "{}, {}, {}/{}, f/{}, ISO {}".format( exif[272],
exif[42036],
exif[33434][0],
exif[33434][1],
exif[33437][0],
exif[34866])
# Pyperclip
# Copyright (c) 2010, Albert Sweigart
# All rights reserved.
#
# BSD-style license:
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the pyperclip nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY Albert Sweigart "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL Albert Sweigart BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import platform, os
from subprocess import call, Popen, PIPE
def winGetClipboard():
ctypes.windll.user32.OpenClipboard(0)
pcontents = ctypes.windll.user32.GetClipboardData(1) # 1 is CF_TEXT
data = ctypes.c_char_p(pcontents).value
#ctypes.windll.kernel32.GlobalUnlock(pcontents)
ctypes.windll.user32.CloseClipboard()
return data
def winSetClipboard(text):
text = str(text)
GMEM_DDESHARE = 0x2000
ctypes.windll.user32.OpenClipboard(0)
ctypes.windll.user32.EmptyClipboard()
try:
# works on Python 2 (bytes() only takes one argument)
hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text))+1)
except TypeError:
# works on Python 3 (bytes() requires an encoding)
hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text, 'ascii'))+1)
pchData = ctypes.windll.kernel32.GlobalLock(hCd)
try:
# works on Python 2 (bytes() only takes one argument)
ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text))
except TypeError:
# works on Python 3 (bytes() requires an encoding)
ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text, 'ascii'))
ctypes.windll.kernel32.GlobalUnlock(hCd)
ctypes.windll.user32.SetClipboardData(1, hCd)
ctypes.windll.user32.CloseClipboard()
def macSetClipboard(text):
text = str(text)
p = Popen(['pbcopy', 'w'], stdin=PIPE)
try:
# works on Python 3 (bytes() requires an encoding)
p.communicate(input=bytes(text, 'utf-8'))
except TypeError:
# works on Python 2 (bytes() only takes one argument)
p.communicate(input=bytes(text))
def macGetClipboard():
p = Popen(['pbpaste', 'r'])
stdout, stderr = p.communicate()
return bytes.decode(stdout)
def gtkGetClipboard():
return gtk.Clipboard().wait_for_text()
def gtkSetClipboard(text):
global cb
text = str(text)
cb = gtk.Clipboard()
cb.set_text(text)
cb.store()
def qtGetClipboard():
return str(cb.text())
def qtSetClipboard(text):
text = str(text)
cb.setText(text)
def xclipSetClipboard(text):
p = Popen(['xclip', '-selection', 'c'], stdin=PIPE)
try:
# works on Python 3 (bytes() requires an encoding)
p.communicate(input=bytes(text, 'utf-8'))
except TypeError:
# works on Python 2 (bytes() only takes one argument)
p.communicate(input=bytes(text))
def xclipGetClipboard():
p = Popen(['xclip', '-selection', 'c', '-o'], stdout=PIPE)
stdout, stderr = p.communicate()
return bytes.decode(stdout)
def xselSetClipboard(text):
p = Popen(['xsel', '-i'], stdin=PIPE)
try:
# works on Python 3 (bytes() requires an encoding)
p.communicate(input=bytes(text, 'utf-8'))
except TypeError:
# works on Python 2 (bytes() only takes one argument)
p.communicate(input=bytes(text))
def xselGetClipboard():
p = Popen(['xsel', '-o'], stdin=PIPE)
stdout, stderr = p.communicate()
return bytes.decode(stdout)
if __name__ == "__main__":
if len(sys.argv) == 2:
# Pyperclip Platform
if os.name == 'nt' or platform.system() == 'Windows':
import ctypes
getcb = winGetClipboard
setcb = winSetClipboard
elif os.name == 'mac' or platform.system() == 'Darwin':
getcb = macGetClipboard
setcb = macSetClipboard
elif os.name == 'posix' or platform.system() == 'Linux':
xclipExists = call(['which', 'xclip'],
stdout=PIPE, stderr=PIPE) == 0
if xclipExists:
getcb = xclipGetClipboard
setcb = xclipSetClipboard
else:
xselExists = call(['which', 'xsel'],
stdout=PIPE, stderr=PIPE) == 0
if xselExists:
getcb = xselGetClipboard
setcb = xselSetClipboard
try:
import gtk
getcb = gtkGetClipboard
setcb = gtkSetClipboard
except Exception:
try:
import PyQt4.QtCore
import PyQt4.QtGui
app = PyQt4.QApplication([])
cb = PyQt4.QtGui.QApplication.clipboard()
getcb = qtGetClipboard
setcb = qtSetClipboard
except:
raise Exception('Pyperclip requires the gtk or PyQt4 module installed, or the xclip command.')
copy = setcb
paste = getcb
exif_string = get_formatted_exif(sys.argv[1])
copy(exif_string)
print exif_string
print "... has been copied to your clipboard"
else:
print "Usage: python " + sys.argv[0] + " <file>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment