fix path with spaces for blurred image generation (#52)

This commit is contained in:
Visual-Dawg 2024-03-07 13:36:15 -05:00 committed by GitHub
parent f455ac43d3
commit 6e36c713b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 38 deletions

View File

@ -33,13 +33,15 @@ const filePaths = await globby(imageDirectories, {
.map((filePath) => new URL(filePath, root)) .map((filePath) => new URL(filePath, root))
}) })
for (const filePath of filePaths) { for (const filePathUrl of filePaths) {
const extension = filePath.pathname.split('.').at(-1) const extension = filePathUrl.pathname.split('.').at(-1)
const generatedFileName = generatedPrefix + getFileNameWithoutExtension(filePath.href) + '.webp' const generatedFileName =
const outPath = new URL('.', filePath).pathname + generatedFileName generatedPrefix + getFileNameWithoutExtension(filePathUrl.href) + '.webp'
const outPath = decodeURIComponent(new URL('.', filePathUrl).pathname + generatedFileName)
const filePath = decodeURIComponent(filePathUrl.pathname)
const currentBrightness = Number( const currentBrightness = Number(
exec(`convert "${filePath.pathname}" -colorspace Gray -format "%[mean]" info: `) exec(`convert "${filePath}" -colorspace Gray -format "%[mean]" info: `)
) )
// Boost the brightness if the image is very dark // Boost the brightness if the image is very dark
@ -49,7 +51,7 @@ for (const filePath of filePaths) {
const svgCommands = extension === 'svg' ? '-background none -resize 2500x2500' : '' const svgCommands = extension === 'svg' ? '-background none -resize 2500x2500' : ''
exec( exec(
`magick convert ${svgCommands} -brightness-contrast ${brightnessIncrease}x40 -modulate 100,1000,100 ${filePath.pathname} ${outPath}` `magick convert ${svgCommands} -brightness-contrast ${brightnessIncrease}x40 -modulate 100,1000,100 "${filePath}" "${outPath}"`
) )
// Modify colors with LUT // Modify colors with LUT
@ -59,40 +61,22 @@ for (const filePath of filePaths) {
`magick convert -modulate 100,250,100 -scale 10% -gaussian-blur 0x20 -resize 500% -quality 50 "${outPath}" "${outPath}" ` `magick convert -modulate 100,250,100 -scale 10% -gaussian-blur 0x20 -resize 500% -quality 50 "${outPath}" "${outPath}" `
) )
console.log(`Blurred ${filePath.pathname}`) console.log(`Blurred ${filePath}\nOutpath: ${outPath}\n`)
} }
function exec(command) { function exec(command) {
const { stdout, error } = spawnSync(command, { shell: true }) // the try-catch is kinda stupid, but otherwise Node will not give any meaningful info
if (error) throw error try {
const { stdout, error, stderr } = spawnSync(command, {
shell: true,
stdio: ['inherit', 'inherit', 'pipe']
})
return stdout if (stderr.toString().trim()) throw stderr.toString().trim()
if (error) throw error
return stdout
} catch (error) {
throw new Error(error)
}
} }
// // parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
// // cd "$parent_path"
// find "../static/imgs/ricing_competitions/" -type f \
// \( -iname "*.jpg" -o -iname "*.png" -o -iname "*.gif" -o -iname "*.bmp" -o -iname "*.jpeg" -o -iname "*.webp" \) -not -name "generated_*" -print0 |
// while IFS= read -r -d '' filepath; do
// echo "$filepath" gets blurred
// directory=$(dirname "$filepath")
// filename=$(basename "$filepath")
// generated_filename="${directory}/generated_${filename}"
// brightness=$( convert $filepath -colorspace Gray -format "%[mean]" info: )
// max_brightness="65535" # The possible maximum brightness possible from the previous command
// brightness_threshold=$( python -c "print( $max_brightness * 0.5 )" )
// # Boost the brightness if the image is very dark
// brightness_boost=$( python -c "print( max( (1 - ($brightness / $brightness_threshold)) * 50 , 0) )" )
// # Modify colors with LUT
// magick convert -brightness-contrast ${brightness_boost}x40 -modulate 100,1000,100 "$filepath" "$generated_filename"
// magick "$generated_filename" "./hald-clut.color.io.png" -hald-clut "$generated_filename"
// # Also make them smaller to reduce file size
// magick convert -modulate 100,250,100 -scale 10% -gaussian-blur 0x20 -resize 500% -quality 50 "$generated_filename" "$generated_filename"
// # magick convert -scale 10% -brightness-contrast ${brightness_boost}x25 -modulate 100,500,100 -gaussian-blur 0x20 -resize 1000% "$filepath" "$generated_filename"
// done