AppleScript Photos export quality
In AppleScript, I can export photos from Apple Photos. There are two options:
- using originals, which discards all modifications I've made
- not using originals, which exports them at 100% quality.
The export at 100% quality is practically uncompressed, even when the original photo is more compressed. This ends up taking more space than the original, effectively upsampling the photo, at no added benefit.
Is it possible to keep the original quality, but not lose the edits made in Photos?
Note that a switch for quality exists in Photos, as per screenshot below.
EDIT
I ended up writing and using this. Note that this does still NOT solve my problem, as explained in the comments, but it gives me a suitable approximation.
(*
Export Apple Photos
===================
Exports all photos in Apple Photos library and reduces them to 90% quality.
The photos are in their current version (as opposed to the original).
Quality reduction is done with `mogrify` as setting the quality isn't
possible (nor it is possible to get the current quality -- everything
gets exported at 100%). For more on this issue see [1].
TODO
----
* Albums in the top folder (i.e. outside any folder) aren't exported.
Only albums in folders are exported. To list them run this::
tell application "Photos"
repeat with alb in albums
log (get name of alb)
end repeat
end tell
References
----------
[1] https://apple.stackexchange.com/questions/410229/applescript-photos-export-quality
*)
-- main
tell application "Photos"
repeat with topFolder in folders
my processFolder(topFolder, 0, (get name of topFolder))
end repeat
end tell
-- recursively scan folders and export albums
on processFolder(fold, level, dirpath)
tell application "Photos"
log "Folder " & (get name of fold)
repeat with subFolder in (get folders of fold)
set subPath to dirpath & "/" & (get name of subFolder)
my exportAlbum(subFolder, subPath)
my processFolder(subFolder, level + 1, subPath)
end repeat
my exportAlbum(fold, dirpath)
end tell
end processFolder
-- export all albums in folder `f`
on exportAlbum(f, relativePath)
set dest to "/Volumes/DATA/ApplePhotos/" as POSIX file as text -- the destination folder (use a valid path)
tell application "Photos"
repeat with i in (get albums of f)
set tFolder to (the POSIX path of (dest as string) & relativePath & "/" & (get name of i)) as POSIX file as text
repeat 1 times
tell application "Finder"
if exists tFolder then
log "Skipping album " & (get name of i)
exit repeat
end if
end tell
log "Album " & (get name of i) & " -> " & tFolder as POSIX file as text
my makeFolder(tFolder) -- create a folder named (the name of this album) in dest
with timeout of 120 * 60 seconds -- 2 hours
export (get media items of i) to (tFolder as alias) without using originals
my reduceSize(tFolder)
end timeout
end repeat
end repeat
end tell
end exportAlbum
-- util mkdir
on makeFolder(tPath)
do shell script "mkdir -p " & quoted form of POSIX path of tPath
end makeFolder
-- util shell script to reduce file size (quality 90)
-- note: it checks folder is not empty, otherwise mogrify will fail.
on reduceSize(tPath)
set dirpath to quoted form of POSIX path of tPath & "/*.jpeg"
do shell script "ls " & dirpath & "; if [ $? -eq 0 ]; then /usr/local/bin/mogrify -quality 90 " & dirpath & "; else echo No files in folder " & dirpath & "; fi"
end reduceSize
You could use the third-party tool ImageMagick, which can perform a wide variety of image transformations, including converting to a lower quality. You can install it with Homebrew with brew install imagemagick
. You would include it through the do shell script
command, for example do shell script 'convert input.png -quality 85% output.jpg'
.
There is no such thing as the "original" quality when you're dealing with JPEG's.
This answer on Photo.SE explains that when you open a JPEG, it's decoded and decompressed. When you apply changes, the application does this on the decompressed version in memory. Then, when you hit export, the application compresses the image again taking the JPEG quality into account.
This other answer on Photo.SE has more information on JPEG-to-JPEG exports.
You don't indicate explicitly why you want the "original" quality, but I think you are looking to approach the same file size as the original JPEG.
In that case, try out the different quality levels (low, medium, ...) to check which level gives you approximately the same file size as the JPEG file you started with.
Then use UI scripting to export the photos at the desired quality level.