How to set background color while Applying an SRT rotation to a PNG file ?

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
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

How to set background color while Applying an SRT rotation to a PNG file ?

Post by Alexvb6 »

Hi,

Under windows with IM 6.5 or 6.9, I am applying an SRT rotation over a PNG image, and would like to set the background color.
I can successfully rotate the image and set the virtual pixels color, but my rotated image in the center remains in a "transparent square".
To be clearer, the initial transparency of the image is retained.
I would like the background color to be applied 1). to the virtual pixels, but also 2). in the whole canvas background.

I have tried the two following approaches, without success :

Approach 1 : Setting the background color the simplest way.
It would seem to be the way to go...

Code: Select all

convert InputTransparentFile.png ^
-alpha remove ^
-alpha off ^
-background #F39393 ^
-virtual-pixel background ^
-background #F39393 ^
+distort SRT '45' ^
+repage ^
OutputPicture-withLightRedBackground.png
Approach 2 : Trying to rotate the image over a transparent background, THEN extract its alpha channel, reapply it, then compose the transparent image over a plain background.
It seems very over-complicated to me...

Code: Select all

convert ^
InputTransparentFile.png ^
-alpha set ^
-virtual-pixel none ^
+distort SRT '45' ^
-background #F39393 ^
+write TempResult-RotatedImage.png ^
-write mpr:RotatedImage +delete ^
 ^
mpr:RotatedImage -set colorspace RGB ^
-alpha extract ^
+write TempResult-ExtractedImageAlpha.png ^
-write mpr:ExtractedImageAlpha +delete ^
 ^
mpr:RotatedImage mpr:ExtractedImageAlpha -alpha Off -compose CopyOpacity -composite ^
+write TempResult-RotatedImgWithAlpha.png ^
-write mpr:RotatedImgWithAlpha +delete ^
 ^
mpr:RotatedImgWithAlpha -alpha Off -background #F39393 -flatten ^
OutputPicture-withLightRedBackground.png
Could a good soul lead me to the light :-) ?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by fmw42 »

Keep everything transparent until the end. Then add -background somecolor -flatten before the output.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by snibgo »

As Fred says, so the command is simply:

Code: Select all

convert ^
  in.png ^
  -virtual-pixel None -distort SRT 45 +repage ^
  -background #F39393 -layers Flatten ^
  out.png
snibgo's IM pages: im.snibgo.com
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by Alexvb6 »

Hi Fred, Hi Snigbo,

Keeping everything transparent THEN apply the background by flattenning is what I was trying to achieve... But your knowledge of IM surpassed mine !
I'ma little bit contrite as the command-line is just "simple" and "self-explanatory"... So my final words will simlpy be :

You helped me alot, and I thank you kindly :D
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by Alexvb6 »

Oh ! Just another thing I am thinking about... I have seen that I could use -virtual-pixel random -filter point ^ to get the newly created pixels of the background as "random from the colors contained in the original image".
But without considering SRT or any other operator, I am wondering if there is a simple way to create an image with random pixels that match the one found in the original image ?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by snibgo »

You can rotate the image, with transparent virtual pixels, then composite over any image you want. That other image could be made of random pixels from the source image.

There are two obvious ways to get the random pixels.

The simplest uses "-spread" to move pixels:

Code: Select all

convert toes.png -spread 20 randmove1.jpg
Image

To get more control, you can use a relative displacement map that is made from some random variation of a gray image, for example:

Code: Select all

convert ^
  toes.png ^
  ( +clone ^
    -fill gray(50%%) -colorize 100 ^
    -attenuate 2 ^
    +noise Gaussian ^
  ) ^
  -compose Displace -set option:compose:args 100%%x100%% -composite ^
  randmove2.jpg
Image

(I make JPG images for web convenience. You should use PNG or other format instead.)
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by fmw42 »

When you create your random pattern either by using -virtual-pixel or +noise, before that add -seed X. Use the same value for X.
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by GeeMack »

Alexvb6 wrote: 2018-03-07T05:20:38-07:00But without considering SRT or any other operator, I am wondering if there is a simple way to create an image with random pixels that match the one found in the original image ?
You can create a clone of the input image and give it a random distribution of pixels, then use your input image as a color lookup table to recolor the random image. Something like this should work...

Code: Select all

convert input.png \( +clone +noise random \) +swap -clut output.png
For IM7 use "magick" instead of "convert". For Windows you don't need to escape the parentheses with backslashes, so use this "(...)" instead of this "\(...\)".
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by snibgo »

An interesting method as always from GeeMack. However it will pick values only from pixels on the diagonal from input.png, from top-left to bottom-right, because "-clut" uses only the diagonal pixels.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by GeeMack »

snibgo wrote: 2018-03-07T11:42:57-07:00 However it will pick values only from pixels on the diagonal from input.png, from top-left to bottom-right, because "-clut" uses only the diagonal pixels.
Thanks. I wasn't aware of that.
Alexvb6
Posts: 49
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by Alexvb6 »

Hi,

Another time : thank you Snigbo, fmw42, and GeeMack !
The best results are obtained by Snigbo's two proposals, -spread 20 and -compose Displace !
I will start from them, then blur them a little. This way, my background will be in coherence with the rotated image (that I will compose over the spread out and blurred background).

Surprisingly, the GeeMack suggested -clut method is interesting too. But as it actually return random colors (not from the original image), and it would involve a recoloring using a color lookup table that I do not know how to use, I tend to prefer the two methods proposed by Snigbo.

I thank you very much for your helpful help (I like this kind of repetitions !)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by snibgo »

The "-clut" method returns values picked from random pixels that are on the diagonal. So a particular output pixel will get its red channel from a random pixel, the green value from a random pixel, and the blue value from a random pixel, and these will probably be different random pixels.

To get values for each output pixel from the same random input pixel, make the random image grayscale just before the ")".
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by GeeMack »

snibgo wrote: 2018-03-08T10:08:31-07:00The "-clut" method returns values picked from random pixels that are on the diagonal. So a particular output pixel will get its red channel from a random pixel, the green value from a random pixel, and the blue value from a random pixel, and these will probably be different random pixels.

To get values for each output pixel from the same random input pixel, make the random image grayscale just before the ")".
To follow up on the example I offered, the lookup table could be made from the results of "-unique-colors" which will be a single row of pixels representing every color in the image. Include your suggestion about making the "+noise random" image grayscale, and arrive at a command something like this (Windows style)...

Code: Select all

magick input.png ^
   ( -clone 0 +noise random -colorspace gray ) ( -clone 0 -unique-colors ) -delete 0 -clut output.png
This may not be any less complicated than using "-virtual-pixel random" and "-distort", but it is another way of looking at the task.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by snibgo »

Yet another variation: random displacement, but blurring the displacement map for a mottled "shower-cubicle-glass" effect:

Code: Select all

%IM%convert ^
  toes.png ^
  ( +clone ^
    -fill gray(50%%) -colorize 100 ^
    -attenuate 20 ^
    +noise Gaussian ^
    -blur 0x2 ^
  ) ^
  -virtual-pixel Tile ^
  -compose Displace -set option:compose:args 100%%x100%% -composite ^
  randmove2a.jpg
Image
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to set background color while Applying an SRT rotation to a PNG file ?

Post by snibgo »

We can process the displacement map to get a graduated effect:

Code: Select all

%IM%convert ^
  lenna_hires.jpg ^
  -resize 600x600 ^
  ( +clone ^
    -fill gray(50%%) -colorize 100 ^
    -attenuate 4 ^
    +noise Gaussian ^
    -blur 0x3 ^
    ( +clone ^
      -sparse-color bilinear "0,100,Black 0,200,White" ^
    ) ^
    -compose Mathematics -define compose:args=1,-0.5,0,0.5 -composite ^
  ) ^
  -virtual-pixel Tile ^
  -compose Displace -set option:compose:args 100%%x100%% -composite ^
  randmove5.jpg
Image
snibgo's IM pages: im.snibgo.com
Post Reply