Skip to content

Instantly share code, notes, and snippets.

@JamesHenry
Created April 6, 2022 09:17
Show Gist options
  • Save JamesHenry/12b7aa0f7d8628e84104bd04f3a92e0b to your computer and use it in GitHub Desktop.
Save JamesHenry/12b7aa0f7d8628e84104bd04f3a92e0b to your computer and use it in GitHub Desktop.
//Menu: Convert HEIC Images to JPEG or PNG
const FileType = await npm('file-type');
const convert = await npm('heic-convert');
// Seems to be no way to get this from the env...
const SCRIPT_NAME = 'convert-heic';
const SCRIPT_KIT_OUTPUTS_DIR = '/Users/james/ScriptKit Outputs';
// NOTE: Not rendering custom placeholder, reported here: https://github.com/johnlindquist/kit/discussions/332#discussioncomment-960487
const dropData = await drop('Drop HEIC image(s)');
// Ensure the selected files are all HEIC files
const images = await Promise.all(
dropData.map(async ({ path: imagePath, name: rawNameIncludingExtension }) => {
const buffer = await readFile(imagePath);
const { ext, mime } = await FileType.fromBuffer(buffer);
if (ext !== 'heic' || mime !== 'image/heic') {
throw new Error(
`The selected file "${rawNameIncludingExtension}" is not an HEIC file: ${JSON.stringify(
{
ext,
mime,
}
)}`
);
}
const { dir, name: imageName } = path.parse(imagePath);
return {
buffer,
imageName,
dir,
};
})
);
const format = await arg(
'Which format would you like to convert the HEIC to:',
async () => {
const options = ['JPEG', 'PNG'];
return options.map((opt) => {
return {
name: opt,
value: opt,
};
});
}
);
let qualityInput = await arg('Quality % (between 0 and 100)');
let qualityPercentage = 100;
try {
qualityPercentage = parseInt(qualityInput || 100, 10);
} catch {}
// Create unique output dir for the run
const outputDir = path.join(
SCRIPT_KIT_OUTPUTS_DIR,
SCRIPT_NAME,
new Date()
.toISOString()
.replace(/T/g, '_')
.replace(/:/g, '-')
.replace(/\./g, '-')
);
mkdir('-p', outputDir);
setHint('Converting, this will take a little while...');
for (const { buffer, imageName } of images) {
const outputBuffer = await convert({
buffer,
format,
quality: qualityPercentage / 100,
});
const outputFilePath = path.resolve(
path.join(outputDir, `${imageName}.${format}`)
);
await writeFile(outputFilePath, outputBuffer);
}
// Launch finder with the results
$`open ${outputDir}`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment