Get Larger Thumbnail/Image Preview (512px) from PSB with Code

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Chris S
Posts: 4
Joined: Sun Feb 05, 2023 1:12 am

Get Larger Thumbnail/Image Preview (512px) from PSB with Code

Post by Chris S »

Is it possible to extract a 512px image thumbnail/preview in under a second from large images (10GB+ PSBs) on MacOS without having to open in Photoshop/Bridge?

USE CASE
I need to quickly get an image preview/thumbnail (~512px) of a batch of large multi-GB images on import into FileMaker to accompany metadata that is accessed with the aid of ExifTool.

SOURCE IMAGES
The original images could be as large as 10GB+, 8-bit to 32-bit, RGB or CMYK, layered or flattened, with or without Alpha/Spot Changes, with or without background transparency, and are saved from Photoshop as either TIFs, PSDs, PSBs, PNGs, JPEGS, or HEICs with Thumbnails and Maximize Compatibility enabled.

EXPERIMENTATION:

ExifTool can generate 160px thumbnails from the embedded -PhotoshopThumbnails tag, but that’s smaller than I’d like:

Code: Select all

exiftool -b -PhotoshopThumbnail in_path > out_path
If the ExifTool output below captures everything, the only thumbnail tag in the metadata is -PhotoshopThumbnail, which again is too small:

Code: Select all

exiftool -a -b -W %d%f_%t%-c.%s -preview:all DIR
Simiarly, r-bin in the Adobe Community Forum* suggested JavaScript code, but this too just produced the 160px thumbnail:
https://community.adobe.com/t5/photosho ... 28#M772048

Code: Select all

var file = new File("F:\\((\\test.psb");
var jpg  = new File("F:\\((\\test.jpg");

main(file, jpg);

function main(file, jpg)
    {
    try { 
        file.open("r");
        file.encoding = "BINARY";

        var offset = 0;

        var hrd_len = 26;

        var buff = file.read(hrd_len);
        offset += hrd_len;

        var color_mode_len = get_integer(file.read(4));

        offset += 4 + color_mode_len;

        file.seek(offset, 0);

        var image_resource_len = get_integer(file.read(4));
        offset += 4 + image_resource_len;

        var buff = file.read(image_resource_len);

        file.close();

        var x = buff.indexOf("8BIM\x04\x0C\x00\x00");

        if (x < 0) { alert("Thumbnail not found"); return; }

        var len = get_integer(buff.substr(x+8, 4));

        buff = buff.substr(x+12);
        
        var format = get_integer(buff.substr(0, 4));
        var w      = get_integer(buff.substr(4, 4));
        var h      = get_integer(buff.substr(8, 4));

        buff = buff.substr(28, len-28);

        jpg.open("w");
        jpg.encoding = "BINARY";
        jpg.write(buff);
        jpg.close();    

        alert("Thumbnail " + w + "x" + h);
        }
    catch (e) { alert(e); throw(e); } 
    }

function get_integer(s)
    {
    try { 
        var len = s.length;            

        var mult = 1;
        var ret = 0;                 

        for (var i = len-1; i >=0; i--)
            {
            ret += s.charCodeAt(i) * mult;                 
            mult *= 256;
            }

        return ret;
        }
    catch (e) { alert(e); throw(e); } 
    }
QuickLook has limited success getting 512px images with this code:

Code: Select all

qlmanage -t source_path_to_file -s 512 -o out_path
Unfortunately, QuickLook has problems with larger PSBs (starts having challenges at 2.12GB, and >4.3GB+ will fail). It also doesn't always play well with TIFFs (particularly CMYK with Alpha and/or Spot Channels), or if they are over 2GB. The same is true of Sips, which is even more finicky about first converting to RGB color space, etc., and starts losing the ability to retrieve other metadata as the files get into the 4.3GB+ range.

ImageMagick is too slow when working with multi-GIG PSBs and it’s not feasible to generate from Photoshop.

Adobe Bridge seems to be able to get large previews quickly of 10GB+ files when set to process files this big. How does it do that/where does that data come from? Is there a way to replicate the creation of that outside of Bridge? JavaScript?

MacOS Finder Preview also is unable to generate previews of PSBs that get into the ~4.5GB range.

Any way to do this with JavaScript or Python in combination with FileMaker to quickly access/produce previews/thumbnails on 10GB+ PSBs in the 512px range on import?

Seems like reading composite data would be the fastest way to access? From there would need to ensure 8-bit, RGB, sRGB profile, remove all alpha channels except BKG transparency... essentially a thumbnail, but larger than 160px.