Search code examples
imagemagickimagemagick-convert

ImageMagick script fails with MISLEADING filename or extension too long (-sparse-color)


Problem

In my quite short script I have the problem that it sometimes reports that the filename or extension is too long. Depending on the $image and $size values in my script this error may occur or not.

E.g. the script below produces this error with the image from here - saved and converted to "example3.png".

I do use Version: ImageMagick 7.0.10-62 Q16 x64 on windows and I don't know what to do with the error message... Any ideas what the problem is here?

Powershell script

#####################
# Setup
#####################

$image = "./example3.png"
$out = "./result.png"
$outPalette = "./palette.png"

$size = 50
$fuzz = 50
$colors = 6
$resizedSize = "$($size)x$($size)`!"
$histogramSize = "$($size)x$($size)"

#####################
# Program
#####################

Write-Host ""

# 1) Scale + change depth + remove unwanted colors (b/w)
Write-Host "- Step 1..." -ForegroundColor Green
magick convert $image -scale $resizedSize -depth 8 `
    -fuzz $fuzz -transparent black -transparent white `
    $out

#2) create histogram with the help of the sparse colors
Write-Host "- Step 2..." -ForegroundColor Green
$dataHistogram = magick convert -size $histogramSize xc: -sparse-color voronoi ( magick convert $out sparse-color: ) +dither -colors $colors -depth 8 -format %c histogram:info:

# ... more ...

Edit: Adjustments

  • replaced magick convert with magick
  • replaced $fuzz = 50 with $fuzz = "50%"
  • replaced $size = 50 with $size = 100

More images do work now but e.g. following still fails with the same error: enter image description here

Edit2:

The result of the inner magick command (magick convert $out sparse-color:) looks like following:

# ImageMagick pixel enumeration: 100,100,255,srgba
0,0: (87,72,86,0)  #57485600  srgba(87,72,86,0)
1,0: (105,81,91,0)  #69515B00  srgba(105,81,91,0)
...

Solution

  • I am not sure what's going on with powershell but if the issue is the length of the command-line, you can supply the sparse colour from a file like this:

    magick -size 800x600 xc: -sparse-color voronoi @colors.txt result.png
    

    Or on stdin like this:

    echo "10,10 red 200,200 yellow" | magick -size 800x600 xc: -sparse-color voronoi @- result.png