Adobe Lightroom and non-catalogued photos

From time to time, when I intend to remove an image from Lightroom and delete it from disk, I hit a wrong button and end up to just remove it from Lightroom. The unwanted image file itself stays on my hard drive and reserves precious disk space.

It is very labourous to manually find and delete those orphaned files. I’m not aware of any Lightroom function or utility which would do it on my behalf. So I wrote this small and simple PHP-script to help the job.

  1. <?php
  2. /**
  3.  * This PHP5 CLI script iterates recursively through a directory tree and looks
  4.  * for image files, which it then tries to find in an Adobe Lightroom 2 catalog.
  5.  * If image is not found in Lightroom, its path and filename are printed out.
  6.  *
  7.  * Note that I have a dualbooting computer. I use Lightroom on Windows but I run
  8.  * this script on Linux.
  9.  * My Linux mount points '/media/win' and '/media/fat' equal to Windows drive
  10.  * letters 'C:' and 'E:'.
  11.  *
  12.  * @author  Teemu Mäntynen
  13.  * @link    http://www.mantynen.com/teemu/
  14.  * @license BSD http://www.opensource.org/licenses/bsd-license.php
  15.  *
  16.  * This software is provided "AS IS". NO WARRANTY. USE AT YOUR OWN RISK.
  17.  */
  18.  
  19. //Path to root of photo directories
  20. $path = realpath('/media/fat/kuvat/foto/raw');
  21.  
  22. //Adobe Lightroom SQLite3 catalog file
  23. $db = new PDO('sqlite:/media/win/Documents and Settings/teemu/My Documents/My Pictures/Lightroom/Lightroom 2 Catalog.lrcat');
  24.  
  25. //Image file filename extensions
  26. $imgFileExtensions = array(
  27. 'jpg', 'jpeg', 'tif', 'tiff',  'psd', 'bmp', '3fr', 'raf', 'crw', 'cr2', 'k25',
  28. 'kdc', 'dcs', 'dcr', 'drf', 'mrw', 'nef', 'nrw', 'orf', 'dng', 'ptx', 'pef',
  29. 'arw', 'srf', 'sr2', 'x3f', 'erf', 'mef', 'mos', 'raw', 'rw2', 'cap', 'iiq',
  30. 'r3d', 'fff', 'pxn', 'bay', 'rwz');
  31.  
  32. $ngImages  = 0; $okImages  = 0; $nonImages = 0;
  33.  
  34. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
  35.  
  36. foreach ($files as $name => $file) {
  37.     if (in_array(getExtension($file), $imgFileExtensions)) {
  38.         //Modify Linux path to Windows path ('/media/fat' is 'E:' and '/' is '\')
  39.         $pathname = str_replace('/', '\\', str_replace('/media/fat', 'E:', $name));
  40.         //'W' in the beginning of value of robustRepresentation stands for 'Windows'?
  41.         $sql = "SELECT COUNT(*) FROM AgLibraryFile WHERE robustRepresentation='W$pathname'";
  42.         if ($db->query($sql)->fetchColumn() == 0) {
  43.             //Image not found in Lightroom. Echo path and filename.
  44.             echo $file->getPathname()."\n";
  45.             //Be careful – unlink deletes the file! unlink($file->getPathname());
  46.             $ngImages++;
  47.         } else { $okImages++; }
  48.     } else {
  49.         //echo 'Not an image: '.$file->getPathname()."\n";
  50.         $nonImages++;
  51.     }
  52. }
  53.  
  54. //echo 'Finished. OK: '.$okImages.' NG: '.$ngImages.' Non image: '.$nonImages."\n";
  55. exit(0);
  56.  
  57. function getExtension(SplFileInfo $fileInfo)
  58. {
  59.     $name = $fileInfo->GetFilename();
  60.     $extension = strrpos($name, ".", 1) + 1;
  61.     if ($extension != false) {
  62.         return strtolower(substr($name, $extension, strlen($name)$extension));
  63.     } else { return ''; }
  64. }
  65. ?>

Vastaa

Sähköpostiosoitettasi ei julkaista. Pakolliset kentät on merkitty *

Voit käyttää näitä HTML-tageja ja attribuutteja: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>