Search code examples
javascriptnode.jsimage-processingimagemagickgraphicsmagick

How to Resize image and reduce DPI using Imagemagick in node


I am trying to resize image and reduce its DPI both at once using package.

I am using im.convert() instead of im.resize() because I have other options as well that needs to be specified. I tried to pass an array with all the options to the im.convert(), but I am getting an error that says "Error: Command failed: Invalid Parameter - -units"

const inputPath = "E:\PC\lister\uploads\dbmid_AAAeU6UkAOHoUmq6t25RPjK6g1sfK6gn7fg\Clients\Matt\2019-06-10T03-32-02.385Z45.jpg";'

const outputPath= "E:\PC\lister\uploads\dbmid_AAAeU6UkAOHoUmq6t25RPjK6g1sfK6gn7fg\Clients\Matt\Web\2019-06-10T03-32-02.385Z45.jpg";
const args = [
    inputPath,
    "-units",
    "pixelsperinch",
    "-density",
    "75x75",
    "-resize",
    "1920",
    outputPath
]
im.convert(args, function(err, stdout, stderr) {
    if(err) console.log(err)

});

I am expecting the resized image with 75dpi in the folder called 'web' that I specified in the outputPath above. but I am getting an error that says "Error: Command failed: Invalid Parameter - -units"


Solution

  • You are mistakenly running a Microsoft-supplied program called CONVERT.EXE that converts FAT filesystems to NTFS (or something similar) rather than the ImageMagick command you want. Depending on the version of ImageMagick you want to use, the solution is potentially different.


    If you want to use ImageMagick v7 (which is the best and a sensible idea), the commands have changed as follows:

    Old v6 command |  New v7 command
    ===============|================
    identify       | magick identify
    animate        | magick animate
    montage        | magick montage
    convert        | magick
    mogrify        | magick mogrify    
    

    So, you need to change your Javascript to use magick rather than convert.


    If you want to use v6 ImageMagick, you need to continue to use convert but ensure that Windows finds ImageMagick convert rather than Windows C:\WINDOWS\SYSTEM32\CONVERT.EXE. You can either do that by explicitly running the full ImageMagick command, so instead of plain convert you use the full path which will look roughly like this:

    C:\ImageMagick\ImageMagick-6.7.6\convert ...
    

    or you set your PATH in some GUI window ( I avoid Windows, but it's something like Settings->Control Panel->System->Environment Variables->PATH) and make it so the directory containing ImageMagick is at the front so Windows finds ImageMagick before its own CONVERT.EXE:

    PATH=C:\ImageMagick\ImageMagick-6.7.6:<REST OF PATH>