How to use batch to convert to PNG8? Mogrify?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
zimbla88

How to use batch to convert to PNG8? Mogrify?

Post by zimbla88 »

Hello, I am using Imagemagick 6.6.3 Q8.

I have folders with thousands of PNG's that I need to convert to indexed color, and have been trying to find the quickest way to make this happen. I found that simply using the command "convert image.png PNG8:new_image.png" works great for my needs. But that only applies to a single image, I need to make this happen for entire folders.

I'm either not finding the right keyword or not sure how to implement the mogrify command. I tried using the PNG8: command using mogrify but it doesn't appear to recognize it, or it's just not a supported command. GIven the example that works for me "convert image.png PNG8:new_image.png" - what would be the best way to execute this command in a batch?

Thanks in advance for any help!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to use batch to convert to PNG8? Mogrify?

Post by Bonzo »

Not sure about sub folders but this method should work if you modify it to what you need:

Code: Select all

::Turn of displaying the code on the screen
@echo off

:: Read all the jpg images from the directory, resize them, add some text and save as a png in a different directory
for %%f in (%1\*.jpg) do ( convert "%%f" -resize 200x200 -pointsize 18 -fill black ^
-gravity northwest -annotate +0+0 "Some text" "%2\%%~nf.png" )

This batch file was named resize.bat and called using "resize path\to\original\ path\to\save"

I have some other batch script examples on my site: http://www.rubblewebs.co.uk/imagemagick/batch.php

This method viewtopic.php?f=1&t=14587 also worked for windows "send to".

Note: I always have problems with spaces in file names.
zimbla88

Re: How to use batch to convert to PNG8? Mogrify?

Post by zimbla88 »

Thanks for the reply.

Are you able to test this method in reference to the specific type of conversion I need? I am taking a PNG image and converting it to a PNG8 using this command "convert image.png PNG8:new_image.png"

PNG8: is the only function I'm using really, but it looks like it needs the colon there between PNG and the new filename. Would I just do this?

Code: Select all

::Turn of displaying the code on the screen
@echo off

:: Read all the jpg images from the directory, resize them, add some text and save as a png in a different directory
for %%f in (%1\*.jpg) do ( convert "%%f" PNG8:"%2\%%~nf.png" )
Also, I don't quite understand what you mean here
This batch file was named resize.bat and called using "resize path\to\original\ path\to\save"
I just need to convert a PNG to a PNG8.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to use batch to convert to PNG8? Mogrify?

Post by anthony »

zimbla88 wrote:HI found that simply using the command "convert image.png PNG8:new_image.png" works great for my needs. But that only applies to a single image, I need to make this happen for entire folders.
So use

Code: Select all

   mogrify -format PNG8 *.png
However that will replace the images directly so also see the -path option of mogrify to specify a different destination directly for the new images.

http://www.imagemagick.org/Usage/basics/#mogrify
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
zimbla88

Re: How to use batch to convert to PNG8? Mogrify?

Post by zimbla88 »

Hello, I tried that command and it executes without error but instead of overwriting the image with a new one, it creates a new file named 'image.PNG8'

It looks like it is just creating a new file type with a PNG8 extension using that command. I've typed that in verbatim, there's no chance I'm typing it wrong. Is there something I'm missing?

Just to clarify, it creates a "filename.PNG8" file, as far as I know that isn't correct... Using the "convert filename.png PNG8:new_filename.png" command creates a PNG file, not a PNG8.

Wondering what to do? Thanks in advance for any help.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to use batch to convert to PNG8? Mogrify?

Post by anthony »

Is the filename.PNG8 files png8?

If they are you can use a file renaming program to correct the file naming.

However it probably should be reported as a bug in the bugs forum.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: How to use batch to convert to PNG8? Mogrify?

Post by Drarakel »

The code from Bonzo should work for you.

There are a lot of alternative methods however. If you want to try it with mogrify (and don't want .png8 as extension), then you could use something like that:

Code: Select all

mogrify -define png:color-type=3 *.png
As another example, a more extensive commandline with mogrify that a) might be a bit more reliable in creating indexed PNGs and b) should also make them a bit smaller:

Code: Select all

mogrify -strip +set date:create +set date:modify -define png:color-type=3 -depth 8 +dither -colors 256 -type Palette -quality 95 *.png
(Note also Anthony's hint about the -path option.)

A small warning: The creation of indexed PNGs in a current ImageMagick can be problematic sometimes. See this report. Also note that the last example sort of enforces the creation of indexed PNGs. So, if you have images with more than 256 colors, these get color-reduced. And if you have PNGs with alpha channel, then you will loose this channel with a "-type Palette" option. But even without "-type Palette", you probably won't succeed in creating indexed PNGs with transparency in a current ImageMagick.
If you don't have such files, then you could have luck and use one of the shown ImageMagick methods in order to create indexed PNGs.

Another alternative is to use a specialized PNG optimizing tool. See e.g. here. That would be my recommendation (if all you want to do is convert a lot of PNGs to smaller indexed PNGs).
zimbla88

Re: How to use batch to convert to PNG8? Mogrify?

Post by zimbla88 »

Thanks for the reply.

The code

Code: Select all

mogrify -define png:color-type=3 *.png

worked just fine and appears to have done exactly what I needed it to do. Thanks!

Now here's something I am a bit confused about. The whole reason I am changing these images to indexed, is to be able to enlarge them to different 4:3 pixel aspect ratios in order to edit them without any loss of pixel detail. This is something I have been doing in Photoshop for several months without any problems, I have been converting them to indexed and resizing them larger without any loss in detail or any fuzzing of pixel edges (the images are 8-bit graphics, all blocky pixel style)

I just checked the image that I just converted with Imagemagick inside of Photoshop, and it does indeed come up indexed. I then resized it in Photoshop and voila, perfectly hard pixel edge no fuzzing out anywhere.

What I'm confused about is I just tried using the resize command in imagemagick to do the same thing, resizing a 320x240 indexed png to 960x720, and the image was super fuzzy and blurred edges. ? It's weird because I brought that same image in to Photoshop before resizing it with imagemagick, and it resized without a problem in photoshop.

Is there some different way I should be converting these using resize in imagemagick?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use batch to convert to PNG8? Mogrify?

Post by fmw42 »

try -scale (or -sample) in stead of -resize. -resize filters and interpolates between pixels to enlarge. I believe that -scale and -sample just replicated pixels. There is a difference when minifying. So read http://www.imagemagick.org/Usage/resize/#sample and below it for -sample.

Check your PS interpolation method to see what you are using. I doubt it is cubic convolution or bilinear or you would see the same blurring.
zimbla88

Re: How to use batch to convert to PNG8? Mogrify?

Post by zimbla88 »

Yes! The command:

Code: Select all

mogrify -scale 300% *.png
worked perfectly! Thank you so much fmw42 for that, it didn't occur to me to use scale instead of resize. It all works perfectly now. Thank you everyone for the help, I really appreciate it, was feeling stuck for a few weeks there!

Now to go convert my billion frames and reassemble them into avi files. :)
zimbla88

Re: How to use batch to convert to PNG8? Mogrify?

Post by zimbla88 »

Hi, just wanted to post an update on this.

Code: Select all

mogrify -define png:color-type=3 -scale 300% *.png 
That code works great for what I'm doing, all except for the amount of time it takes. I used a stopwatch and it took 35 minutes from the command line to execute a folder of roughly 4,000 png files. I was using a batch in Photoshop to do the same thing and it takes about that much time, I haven't timed it but I plan on doing that as well to see how close it is. I was thinking a batch command in DOS would execute faster for some reason, but it doesn't look like I'm saving much if any time with imagemagick. Definitely saving money if it came down to buying Photoshop or using imagemagick, but I already bought Photoshop. :)

Anyways just wanted to mention this to see if anyone knows of any commands or methods to shorten the amount of time? Also is there a way to see the progress of the batch? (When I hit enter using the code above it just pauses for 35 minutes, would be nice to see a list of filenames progressing)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to use batch to convert to PNG8? Mogrify?

Post by fmw42 »

try adding -monitor to your command line. But I don't know if mogrify works with that. Possibly only convert.

Note that IM is a general purpose image processing system and so is not fine tuned for speed. Also mogrify is rather old. You could try writing a script to loop through your images and use convert instead, but I doubt it would make a whole lot of difference.

I really don't see much way of speeding it up. You are already using -scale to enlarge which is faster than -resize.

I don't know if stripping any profiles and other header information will speed things up, but you could try adding -strip.

If you are on a Q16 IM compile, you could try compiling in Q8. It would be faster. You could also add -depth 8, if on Q16 to keep the file size down so that writing the file would take less time.
Post Reply