Conditional change of resolution

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
Clive Webster

Conditional change of resolution

Post by Clive Webster »

Using command line options I want to decrease the resolution to 300dpi but ONLY if the source image has a higher resolution. I'm trying to decrease the file size of the image for big images but to leave small images the same.
If I use:

Code: Select all

convert -colorspace RGB -strip -resample 300x300 in.tif out.jpg
then this works fine if in.tif is, say, 800dpi, but if its only 100dpi then it changes the resolution to 300 and, in order to make the physical canvas size the same, create an out.jpg with 9 times the number of pixels.

So I'm after a sort of hybrid that does the same as some of the 'resize image if its bigger than ...' but does it to the resolution instead.

ie 600dpi resolution image that is 1in wide and 1 in high (has 600*600pixels) becomes a 300dpi image that is 1in wide and 1in high (has 300*300 pixels)
but a 100 dpi resolution image of any size will pass through unchanged

Note that this is running against a folder of incoming images which can be any resolution and and pixel dimension.

Any help appreciated
Clive Webster

Re: Conditional change of resolution

Post by Clive Webster »

But isn't that example using php ?

I'm trying to do it as a command line task - so am restricted to the command line options.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Conditional change of resolution

Post by Bonzo »

Sorry Clive I didn't fully digest your post, It is using php.

I have never tried it but from a post on another forum about running php on a windows PC:
You are able on window as well by using the command line. If you have PHP's path to the executable set in the PATH environment variable you can run a file by typing C:\>php myphpfile.php
Clive Webster

Re: Conditional change of resolution

Post by Clive Webster »

Bonzo, thanks for this and your earlier comments.

However my main reason for evaluating ImageMagick is to replace our existing command line Image Preview functionality in an existing Java Servlet/JSP application so I am looking for an 'out-of-the-box' kind of solution and dont want to get into 'You need to install PHP Version x-y-z and then learn PHP and then write custom scripts' kind of debate - as it just confuses the technical architecture etc of the existing application and creates more inter-dependencies of technologies, version numbers and testing. I've had a quick look at the Java API layer on top of ImageMagick but it seems to be somewhat unsupported and a subset of functionality - so dont want to be left with an immediate 'legacy'. It also doesn't help that it appears that you need a C++ compiler to compile the code - but then 'Hey-thats not your problem'.

Why am I replacing the existing image preview stuff - because although it does the basic job (including what the answer to this query) it does generate some slightly flakey image files.

Why ImageMagick - because its a very comprehensive package, it copes with 175Mb image files, all the formats I need etc etc - and all of this from command line prompts. EXCEPT for this one little(!) thing.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Conditional change of resolution

Post by Bonzo »

I am afraid I can not help then; the information can be obtained but as you says its then getting it into your application.
etiennesky

Re: Conditional change of resolution

Post by etiennesky »

you can do the exact same thing as the php script using bash, grep and awk.

After all the php script uses the output of the "identify" command and then looks for the "Resolution" line. Just a few lines in a bash script, using "grep Resolution" and then awk to get the relevant field in that line.
Clive Webster

Re: Conditional change of resolution

Post by Clive Webster »

Hmm - so I need to install 'bash', ' which then needs 'CygWin' etc etc - more inter-related Version nightmares!

However: it does sorta answer the question. I'm calling the command line from a Java program so I can still use 'identify' and parse the response myself before calling other command line options.

It IS a POTENTIAL solution but not a very elegant one as I'm then tied into the format of the 'identify' response - and can I predict all the different responses that it 'may' give me now and in the future for every image type?

Guess - it comes down to how much this is requested and whether we can 'vote' for it via the developers. Hey ho.

Thanks to all responders.

C
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Conditional change of resolution

Post by anthony »

ALL languages can do the string parsing from identify, some more easilly than others. shell is reasonable, perl is easier, etc etc.

however you the API for the language you have should let you get the meta-data from an image it has read in without needed to use an external command. It just may not be obvious, or (GASP!) unimplemented for that language.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply