APIs missing: exporting Images
matteogratton opened this issue · 1 comments
matteogratton commented
Exporting images at the moment is pretty complex.
- We don't have the original Image name
Images are stored only with an ID: it would be nice to have also their original name
- We don't have an easy way to read them
a. To read an image from a pattern fill:
let patternImage = fill._object.image().treeAsDictionary().sha1;
b. To find the ID of an image from an image layer:
let patternImageID = fill._object.image().treeAsDictionary().sha1;
it is easier if it is an image layer or an override of an image layer
let imageID = layer.image.id
let imageID
if (override.property == "image") {
imageID = String(override.value.id)
}
- We don't have an easy way to export them
To export an image is more complex, as the stored image values are a native NSImage object.
I found a few ways to do it, the easiest one is the one coming from Automate (and export them as PNGs or JPEGs based on the function of choice:
let imageData = layer.image; [or layer.fills[0].pattern.image]
let path = [path of a folder of choice]
function exportImageDataAsPng(imageData, path) {
var rep = NSBitmapImageRep.imageRepWithData(imageData.nsdata);
var png = rep.representationUsingType_properties(
NSBitmapImageFileTypePNG,
{}
);
png.writeToFile_atomically(path, "YES");
}
function exportImageDataAsJpg(imageData, path, quality) {
var rep = NSBitmapImageRep.imageRepWithData(imageData.nsdata);
var jpg = rep.representationUsingType_properties(
NSBitmapImageFileTypeJPEG,
{ NSImageCompressionFactor: quality || 0.75 }
);
jpg.writeToFile_atomically(path, "YES");
}
It would be nice to be able to export images with their original name (I understand it will not possible to export them with the original filetype):
let imageToExport = layer.image.export(path);
or
let imageToExport = layer.fills[0].pattern.export(path);
rodionovd commented
Thank for filing, would be great to see improvements in this area! See also this thread about current struggles with default exportOptions for ideas.