This code automatically converts images in a directory to PNG format for transparency support, while using existing JPGs for non-transparent images to maintain quality. It uses shell commands and image processing tools to handle the conversions efficiently.
npm run import -- "convert nonalpha"
var path = require('path')
var fs = require('fs')
var importer = require('../Core')
var {glob} = importer.import("glob files")
var execCmd = importer.import("spawn child process")
var mkdirpSync = importer.import("mkdirp")
var { chext, chroot } = importer.import("changing file names")
var {imageTypes} = importer.import("quake 3 file whitelist")
async function convertNonAlpha(root) {
var result = []
var output = path.join(path.dirname(root), path.basename(root) + '-converted')
var files = await glob(imageTypes.map(a => '**/*' + a), root)
for(var i = 0; i < files.length; i++) {
var outFile, inFile = files[i]
var alphaCmd = await execCmd(`identify -format '%[opaque]' '${inFile}'`, {quiet: true})
// if it is alpha
if(alphaCmd[0].localeCompare('False') === 0) {
// convert everything else to png to support transparency
outFile = chroot(chext(inFile, '.png'), root, output)
} else {
// if a jpg already exists, use that file for convert
if(fs.existsSync(chext(inFile, '.jpg'))) {
inFile = chext(inFile, '.jpg')
}
// transfer low quality jpeg instead
outFile = chroot(chext(inFile, '.jpg'), root, output)
}
mkdirpSync(path.dirname(outFile))
// convert, baseq3 already includes jpg
await execCmd(`convert -strip -interlace Plane -sampling-factor 4:2:0 -quality 50% "${inFile}" "${outFile}"`, {quiet: true})
result.push(outFile)
}
return result
}
module.exports = convertNonAlpha
/**
* Convert all non-alphabetic images to their respective alpha-enabled formats.
*
* @param {string} root The root directory to scan for images.
* @returns {Promise<string[]>} An array of converted image paths.
*/
const path = require('path');
const fs = require('fs');
const importer = require('../Core');
const { glob } = importer.import('glob files');
const { execCmd } = importer.import('spawn child process');
const { mkdirpSync } = importer.import('mkdirp');
const { chext, chroot } = importer.import('changing file names');
const { imageTypes } = importer.import('quake 3 file whitelist');
async function convertNonAlpha(root) {
// Define the output directory
const outputDir = path.join(path.dirname(root), path.basename(root) + '-converted');
// Define the image file types to search for
const imageFiles = await glob(imageTypes.map((a) => `**/*${a}`), root);
// Initialize an array to store the converted image paths
const result = [];
// Loop through each image file
for (const { dir: inFile } of imageFiles) {
// Check if the image has alpha
const alpha = await execCmd(`identify -format '%[opaque]' '${inFile}'`, { quiet: true });
// If the image does not have alpha, convert it
if (alpha[0] === 'False') {
// Get the output file path
const outFile = chroot(chext(inFile, '.png'), root, outputDir);
// Create the output directory if it does not exist
mkdirpSync(path.dirname(outFile));
// Convert the image to PNG with low quality
await execCmd(`convert -strip -interlace Plane -sampling-factor 4:2:0 -quality 50% "${inFile}" "${outFile}"`, { quiet: true });
// Add the converted image path to the result array
result.push(outFile);
} else {
// If the image has alpha, check if a JPEG version exists
const jpegFile = chext(inFile, '.jpg');
// If the JPEG version exists, use it
if (fs.existsSync(jpegFile)) {
const outFile = chroot(jpegFile, root, outputDir);
mkdirpSync(path.dirname(outFile));
await execCmd(`convert -strip -interlace Plane -sampling-factor 4:2:0 -quality 50% "${jpegFile}" "${outFile}"`, { quiet: true });
result.push(outFile);
} else {
// If the JPEG version does not exist, convert the image to JPEG with low quality
const outFile = chroot(chext(inFile, '.jpg'), root, outputDir);
mkdirpSync(path.dirname(outFile));
await execCmd(`convert -strip -interlace Plane -sampling-factor 4:2:0 -quality 50% "${inFile}" "${outFile}"`, { quiet: true });
result.push(outFile);
}
}
}
// Return the array of converted image paths
return result;
}
module.exports = convertNonAlpha;
This code defines an asynchronous function convertNonAlpha
that converts image files within a specified directory to PNG format, ensuring transparency support.
Here's a breakdown:
Imports:
convertNonAlpha
Function:
root
directory path as input.root
directory and its subdirectories.identify
command.convert
command to perform the conversion, ensuring transparency support and setting specific image quality parameters.Export:
convertNonAlpha
function for use in other parts of the application.In essence, this code automates the conversion of images to a consistent format (PNG for transparency, JPG for non-transparent images) within a given directory, ensuring compatibility and quality control.