How to change DPI of a PNG file? (in command line, without ImageMagick)
I am looking for a way to change the DPI on PNG image files. Using ImageMagick this is easy:
$ convert -density 150 -units pixelsperinch file.png file.png
The problem is that I will run this command from my application, running on multiple VMs, which don't have ImageMagick installed. I can push the upgrade packages to these VMs, but in case of ImageMagick that would add about 30MB to an upgrade bundle, which is a bit too much for my use case.
Long story short: is there a way I can change the DPI on a PNG file using a small (as in package size) command-line tool?
Optionally, changing bytes in a raw image file would be okay too, if someone knows where to look for them...
Is 2.10 MB small enough? If yes, use NConvert and a batch command like:
nconvert -out png -dpi 150 -keepdocsize -keepfiledate mysource.png
What is NConvert for?
XnView is a GUI-based application, you can change and manipulate images using a graphical user interface. NConvert has about the same capabilities as XnView, but it is a command line tool and has no graphical user interface. You can use NConvert in Batch scripts or another application can call it.
Helpful links
- Beginner guide and Wiki with some examples to start
- Help file to see what command line parameters are available (Uploaded on pastebin)
- A graphical front-end using Nconvert is XnConvert. Use this to create & export Nconvert batch files
- Forum for Xnconvert and Nconvert for
You mentioned you would be running this command "from your application," and this leads me to believe that you have written this application, or are maintaining it. That said, there are ImageMagick API's for several languages, and you could use one of them to add the functionality right into your existing program. The increase in package size would depend on what language your application is written in and whether you link the library statically or dynamically, but I would imagine it would be significantly smaller than a 30 MB increase.
Another option is not to include the entire ImageMagick package. You can download the ImageMagick Zip file (which is only 13 MB BTW), and extract just the convert
tool and it's dependencies and distribute that to your VM's. There is a tool called Dependency Walker that you can use to determine what the dependencies of the convert
tool are.
Use the -size
flag in the netpbm pnmtopng program. The units used are pixels per meter. The following example will set newfile.png to 300dpi.
pngtopnm <oldfile.png | pnmtopng -size='11811 11811 1' >newfile.png