Skip to content

Instantly share code, notes, and snippets.

@xkikeg
Last active December 11, 2015 20:08
Show Gist options
  • Save xkikeg/4652937 to your computer and use it in GitHub Desktop.
Save xkikeg/4652937 to your computer and use it in GitHub Desktop.
Search specified directory and convert the adjacent images equal to each other into hardlink.
#!/usr/bin/python
import os
import os.path
import sys
import Image
class ImageFile(object):
def __init__(self, path):
self.path = path
self._data = None
def empty(self):
return self.path is None
def inode(self):
return os.stat(self.path).st_ino
def data(self):
if not self._data is None: return self._data
if self.empty(): return None
try:
im = Image.open(self.path, "r")
except IOError as e:
# continue if f is not an image file.
print >>sys.stderr, self.path, "cannot be opened."
return None
self._data = im.tostring()
return self._data
def __eq__(self, other):
return (not self.empty() and not other.empty()
and (self.path == other.path
or self.inode() == other.inode()))
def __ne__(self, other):
return not self == other
def equal_image(self, other):
return self.data() == other.data()
# check only adjacents
def adjacent_image_scan(path_list):
lastfile = ImageFile(None)
for curpath in path_list:
curfile = ImageFile(curpath)
if curfile == lastfile: continue
if curfile.equal_image(lastfile):
print "rm", curfile.path, "; ln", lastfile.path, curfile.path
os.unlink(curfile.path)
os.link(lastfile.path, curfile.path)
else:
lastfile = curfile
def main():
if len(sys.argv) == 1:
print >>sys.stderr, "Usage:", sys.argv[0], "dir | files..."
elif len(sys.argv) == 2:
for root, dirs, files in os.walk(sys.argv[1]):
adjacent_image_scan(os.path.join(root, f) for f in sorted(files))
else:
adjacent_image_scan(sys.argv[1:])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment