Convert all .CR2 (camera raw) files to .TIF files in a directory ?

I’m puzzled.
After reading KniazidisR’s script I decided to try to edit a script which used FileManager and replace the dedicated code by a call to perform search.

The result is bad.

The original script does the job in 127 seconds but the new one requires 160 seconds.

Maybe it’s because the scanned folder is not on the boot disk but on an attached one.
I guess that only Shane may explain that.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 3 août 2019 17:44:15

I wish to test perform search to list files whose entire path contain a given string but doesn’t find the correct syntax.

set thePaths to perform search in folders {posixPath} predicate string "kMDItemFSName CONTAINS[c] 'Barbara'"  # Here, posixPath is a single path so brackets are required

is OK to retrieve files whose name contain the string.

I assumed that replacing kMDItemFSName by kMDItemPath would do the job but I was wrong.

I tested with :

----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use script "Metadata Lib" version "2.0.2"
use scripting additions
----------------------------------------------------------------
(*
# My setting. Macintosh HD is NOT my boot volume
set myUserName to do shell script "users"
set targetDir to "Macintosh HD:Users:" & myUserName & ":Musique:iTunes:iTunes Media:Apple Music:" 
*)
set targetDir to (path to music folder as text) & "iTunes:iTunes Media:Apple Music:" # For standard installation
set posixPath to POSIX path of targetDir
set thePaths1 to perform search in folders {posixPath} predicate string "kMDItemPath CONTAINS[c] 'Barbara'"
set thePaths2 to perform search in folders {posixPath} predicate string "kMDItemFSName CONTAINS[c] 'Barbara'"
{thePaths1, linefeed, linefeed, thePaths2}

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 3 août 2019 18:06:20

Yvan,

it’s not possible to use the path in a Spotlight query.

Thank you Stefan.
I will continue to work with the code using FileManagerLib.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 3 août 2019 19:33:03

What do you mean? The following script works with Posix path and with multiple Posix paths:


use AppleScript version "2.5" -- macOS 10.11 or later
use scripting additions
use framework "AppKit"
use script "Metadata Lib" version "2.0.1"
property NSImage : a reference to NSImage of current application

on run
	set myTime to current date
	set chosenFolders to (choose folder with prompt "CHOOSE FOLDER WITH CR2 IMAGES" with multiple selections allowed)
	my processItems(chosenFolders)
	set myTime to (current date) - myTime
end run

on open droppedItems
	my processItems(droppedItems)
end open

on processItems(chosenFolders)
	repeat with theFolder in chosenFolders
		set theFolder to POSIX path of theFolder
		set thePaths to perform search in folders {theFolder} predicate string "kMDItemFSName ENDSWITH[c] '.cr2'"
		repeat with oneItem in thePaths
			set targetPath to (text 1 thru -4 of oneItem) & "TIF"
			set theImage to (NSImage's alloc()'s initWithContentsOfFile:oneItem)
			set tiffImageData to theImage's TIFFRepresentation()
			(tiffImageData's writeToFile:targetPath atomically:false)
		end repeat
	end repeat
end processItems

The kMDItemPath attribute cannot be used in a Spotlight query

Yes, I already understood what you meant. This type of metadata does not exist, and kMDItemFSName makes it possible to search only in the base name of the file system element.

That’s quite possible. I think using Spotlight searches is generally best left for where there are very significant gains to be made.

If you do any more tests, have Activity Monitor open and see what happens with memory use.

Shane.

I increased the size of my test folder by a factor of four and then ran Yvan’s script in post 11 with the memory tab of the activity monitor displayed. The largest user of memory was Script Editor and it started at 3.5GB and increased over time to a maximum of 13.0GB. The next largest user of memory was kernel_task with 364MB of memory.

I then ran my script on a second copy of the same folder. The largest user of memory was sips, which started at 1.57GB and never exceeded 1.6GB. The second largest user of memory was a second instance of sips with a maximum memory usage of 1.1GB. The third was kernel_task at 252MB.

My computer has 16GB of memory.

Hi,

I don’t have the system command knowledge of the earlier posters so apologise if this is a foolish question: is it possible to read/display/extract the jpeg thumbnail or preview that most raw files contain using applescript?

best wishes

Simon K.

I don’t think so, although it depends on how the previews are included.

The sips command-line utility and Applescript’s Image Events are often used to manipulate image files but I don’t believe they will do what you want. There may be other options, but if so I’m not familiar with them.

An alternative that often works (depending on the raw image) is the exiftool command-line utility. Just by way of example, I ran the following in a terminal window, and it extracted a 2.4 MB JPG preview image from a 25 MB Canon cr2 raw image.

You can change -PreviewImage to -ThumbnailImage and exiftool in my example extracts a 14 KB JPG thumbnail image.

Exiftool is a 3-MB free download at:

https://exiftool.org

The following is a simple AppleScript that incorporates exiftool and works on one file at a time. You should change imagePath to whatever you want. Also, you may have to change the path to exiftool and set permissions, depending on your version of macOS.

set imagePath to (path to desktop)

set imageFile to POSIX path of (choose file default location imagePath)

set text item delimiters to "."
set thumbnailFile to (text 1 thru text item -2 of imageFile) & ".jpg"
set text item delimiters to ""

try
	do shell script "/usr/local/bin/exiftool -b -PreviewImage " & quoted form of imageFile & " > " & quoted form of thumbnailFile
on error errorMessage
	display dialog "The following error was reported:" & linefeed & linefeed & errorMessage buttons {"OK"} cancel button 1 default button 1
end try

peavine, I suspect -jpgfromraw might be more the go with exiftool. I think -previewimage might actually (re-)sample the main image.

Thanks Shane. I’m not very knowledgeable on this topic and did some additional research.

Apparently, raw image files often contain up to three preview and one thumbnail images, and specific preview images can be extracted by exiftool with the following options:

-JpgFromRaw
-PreviewImage
-OtherImage

I tested these on a Canon raw CR2 file and the only option that extracted a preview image was PreviewImage. I also tested the options on a Nikon raw NEF file and all three options extracted preview images. So, it would seem advisable for Simon to experiment with his particular raw files and see what preview images are available.

The exiftool documentation contains a commandline which extracts all preview and thumbnail images from a raw file. This seems useful just to get an idea of what images are contained in a raw file.

set theFile to POSIX path of (choose file)
do shell script "/usr/local/bin/exiftool -a -b -W %d%f_%t%-c.%s -preview:all " & quoted form of theFile

I ran this on the Nikon test file–named “Nikon Test Image.nef” with 70.7 MB–and the extracted files were:

Nikon Test Image_JpgFromRaw.jpg - 6 MB
Nikon Test Image_OtherImage.jpg - 937 KB
Nikon Test Image_PreviewImage.jpg - 140 KB
Nikon Test Image_ThumbnailTIFF.tiff - 58 KB

The raw image files supported by exiftool can be found at:

https://exiftool.org/#supported