+Distort SRT then scale image

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
rickysullivan

+Distort SRT then scale image

Post by rickysullivan »

In my script, I'm taking a PNG with transparency and overlaying it with a JPG using the Darken command, I then rotate the comp by -3 and save out as a composite.

The script works fine, except when I got to resize the image before saving out as a PNG, my transparency around the image turns white.

If I take out the rescale command, it looks fine.

Code: Select all

"convert" "C:\Ricks Data\Front Page Getter\Paper-Back-BCM_BSM_MX.png" "C:\Ricks Data\Front Page Getter\Processed\BCM_001.jpg" -background transparent -virtual-pixel background -compose darken -gravity Center +distort ScaleRotateTranslate -3 -scale 500x -composite "C:\Ricks Data\Front Page Getter\Processed\4BCM_001_Paper.png"
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: +Distort SRT then scale image

Post by anthony »

Your command seems a little mixed up to me.

You set various options for BOTH +distort and the -composition but your do the composite AFTER the +distort and even after the -scale, which is not what you said you did. note that -compose is NOT an operator, but a setting that can be used by quite a number of different operators. -composite is the operator you use in this case.

Also if the script is running (I mean where you run it, not where the script is located) in the same directory as the files, you do no need to specify the full path of the image files and results.

As you do the composition AFTER the distort (and scale) their are still two images in memory when the +distort operator is used, one without out an alpha channel (the JPG). Also SRT is itself a scaling operator (the 'S' in SRT) as such unless you specifically want 'pixelated' output you can use it to scale the result.

I would suggest you re-organise your command to do the steps in the actual order wanted. Providing the settings for each of the operators just before the operator. Better still break up your command into separate lines so each operator is on its own set of lines. That makes it a LOT easier to read. In DOS add a '^' at the end of the line to continue the command after the end of line (the UNIX examples use '\' for this.

Here is my re-coding...

Code: Select all

"convert" "C:\Ricks Data\Front Page Getter\Paper-Back-BCM_BSM_MX.png" ^
              "C:\Ricks Data\Front Page Getter\Processed\BCM_001.jpg" ^
              -compose darken -gravity Center -composite ^
              -virtual-pixel transparent +distort SRT -3 ^
             -scale 500x ^
              "C:\Ricks Data\Front Page Getter\Processed\4BCM_001_Paper.png"
each OPERATOR is on a line by itself with any settings used by that operator, making the steps being performed very clear.

This should work, much as you describe and as you seem to be trying to specify.

ASIDE: as the jpeg was composed into a PNG the transparency should be handled correctly, and alpha channel should be enabled when you use +distort. Otherwise you should have also have an -alpha set after reading your images.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
rickysullivan

Re: +Distort SRT then scale image

Post by rickysullivan »

Hi Anthony,

Appreciate your help.

With this script, it's executed in VBS, so the way I fire it off is using the ImageMagick object, and I do usually break it up per line so I can read it better. I was unaware that DOS could accept multiple lines, thanks for the tip.

I'm also only using the full paths as this is just a quick test before implementing into my full code.

I get confused with the order of commands in ImageMagick. I thought -composite had to be the final command before output.

When it comes to maintaining the transparency through out the command, I thought setting the -background transparent -virtual-pixel background before any commands applied to everything that was performed in the convert command

Perhaps I should be using PNG for both files.

I'm using scale so I can target a specific size and not by percentage, I thought SRT was only by percentage. I've also changed from -scale to -adaptive-resize as this seems to look a bit better.

Thanks heaps for you help. I'm still new to IM.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: +Distort SRT then scale image

Post by anthony »

rickysullivan wrote:I get confused with the order of commands in ImageMagick. I thought -composite had to be the final command before output.
It is just an operator that takes 2 (or 3 with a (what should change) mask) and composes them according to the current -compose, -gravity, and -geometry settings.

Another simular operator is -flatten (or -mosaic) which takes a list of images with virtual canvas offsets (-page), a initial starting canvas (-background) and the composition method (-compose) to merge the list into one image (multiple image layer flatten).
When it comes to maintaining the transparency through out the command, I thought setting the -background transparent -virtual-pixel background before any commands applied to everything that was performed in the convert command
Both are just settings, and as with any setting, only effects operators that follow the setting (not before).
Background just sets a specific color used by some operators (like -flatten) -virtual-pixel, just defines how to handle references to pixels that fall outside or surrounding the actual image given. Neither actually gives an image a alpha (transparency) channel, if it does not have one (JPEG doesn't).

Some operators do automatically give a resulting image alpha, but only if that operator is directly associated with an alpha channel. +distort does NOT, and neither does -composite.

"-alpha set" is an operator that modifies the image to ensure it has an alpha channel, but without actually changing the actual 'look' of an image. That is is an image does not have an alpha channel (created or enabled) it will create, and clear it to be fully-opaque. If the image already has alpha and some transparency, ENABLED, it leaves it alone.

Do not confuse this with -alpha on, which will not clear a existing but disabled alpha, to opacity. IT just re-enables it without any clearing, which could modify the 'look' of an image, that may have been previously using an alpha channel.
Perhaps I should be using PNG for both files.
Not necessary, as long as the 'destination' has alpha, the result of the composition will have alpha. Destination controls most aspects of the results of a composite image.
I'm using scale so I can target a specific size and not by percentage, I thought SRT was only by percentage. I've also changed from -scale to -adaptive-resize as this seems to look a bit better.
That is true. SRT can only do it by size. It is just a scaling factor. However SRT is really an AFFINE transform, and you can specify things in pixels with that transform, though you can not easily specify rotations with it.

WARNING: scale generates 'block' artefacts (giant pixels). It is essentially a 'nearest neighbour' interpolation. Adaptive resize generates a 'mesh' interpolative gradient.
http://www.imagemagick.org/Usage/misc/#mesh

Resize will generate a smoother image, that is generally preferred.

However if you can incorperate the scale into the SRT (calculate the scaling factor if you need to) then the resulting image will be directly generated using the distorts own scaling filters, as part of the distortion.

Remember scale, adaptive-resize and resize, are all themselves a type of distortion operation (specially designed limited form of distort), and distorts of distort should be avoided if possible, to generate the best quality result.

I do not mean to say you are doing wrong. What you are doing is perfectly acceptable, and if it is easier and you are okay with the results, fine. Just letting you know there are alternatives.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply