Resize indexed PNG image with ImageMagick while preserving color map

Viewed 1284

I am using custom batch script to make resized copies (33% and 66%) of all PNG images in folder. Here is my code:

for f in $(find /myFolder -name '*.png'); 
do
sudo cp -a $f "${f/%.png/-3x.png}"; 
sudo convert $f -resize 66.67% "${f/%.png/-2x.png}"; 
sudo convert $f -resize 33.33% $f; 
done

It works fine, except when the original image is indexed. In this case the smaller version of the image is RGB (so even larger file size then original image).

I have try several versions but not worked. One that I guess supposed to sort this out was fallowing:

for f in $(find /myFolder -name '*.png'); 
do
sudo cp -a $f "${f/%.png/-3x.png}"; 
sudo convert $f -define png:preserve-colormap -resize 66.67% "${f/%.png/-2x.png}"; 
sudo convert $f -define png:preserve-colormap -resize 33.33% $f; 
done

But it doesn't work.

EDIT:

This is updated co, but it still doesn't work as it supposed to (see the attached image-left is original, right is resized):

for f in $(find /myFolder -name '*.png');
do
  sudo cp -a $f "${f/%.png/-3x.png}";
  numberOfColors=`identify -format "%k" $f`

  convert "$f"                                                        \
    \( +clone -resize 66.67% -colors $numberOfColors -write "${f/%.png/-2x.png}" +delete \)  \
    -resize 33.33% -colors $numberOfColors "$f"
done

enter image description here

Original image: Original image

Scaled version: Scaled image

2 Answers
Related