Creating an image corner relative to the size of the picture

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
louim
Posts: 2
Joined: 2012-07-09T10:05:36-07:00
Authentication code: 13

Creating an image corner relative to the size of the picture

Post by louim »

Greetings everyone.

I've been trying since the past few day to generate an image corner looking like this:

Image

where the red corner is generated over the image and the white space is actually transparent, so I can put the image on any kind of background and it'll show trough.

The problem I have is that I need to generate multiple picture like this one, except they have different sizes. I am wondering how is it possible to generate this corner relative to the image size?

I'm able to generate the square gradient (tested at 100x100) but I have no idea how to create it relative to the picture size, and create & apply the cropping mask so they fit.
Image

Any help will be gladly appreciated.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Creating an image corner relative to the size of the pic

Post by fmw42 »

Are you asking how to apply the same size fold to any image or do you need to have the size of the fold be bigger for larger images and smaller for smaller images. If the latter, then please clarify how big it should be in percent of the width or the height (as they could be different).

If you are on unix or windows w/cygwin, see my scripts, pagepeel and pagecurl, at the link below.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Creating an image corner relative to the size of the pic

Post by fmw42 »

This should work for unix to make a 20% sized corner.

infile="logo:"
pct=20
lw=`convert $infile -format "%[fx:w-1]" info:`
lh=`convert $infile -format "%[fx:h-1]" info:`
size=`convert $infile -format "%[fx:$pct*min(w,h)/100]" info:`
convert $infile \
\( -size ${size}x${size} xc: -sparse-color barycentric '0,0 red -%w,%h red %w,%h black' +repage \) \
\( -size ${size}x${size} xc: -set colorspace RGB -sparse-color barycentric '0,0 white -%w,%h white %w,%h black' -threshold 50% +repage \) \
\( -clone 1 -clone 2 -compose multiply -composite \) \
-delete 1,2 -gravity southeast -compose over -composite \
-fill none -draw "matte $lw,$lh floodfill" logo_redcorner.png


see
http://www.imagemagick.org/Usage/canvas ... _gradients
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/draw/#matte
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Creating an image corner relative to the size of the pic

Post by anthony »

The barycentric method use to generate the gradient ensures that the gradient falling bteeen thw top-left to bottom-right diagonal, is at a 50% level between the top-right and bottom-left.

But if the image is square, then a baricentric with just two points will do the job (IM will calculate the third point)

-sparse-color barycentric '0,0 white %w,%h black'

Just a matter of coloring and setting transparency in that image.

As the overlay is square with transparency, just overlay it using 'Copy' Compostion.
That replaces just the overlay area with both color and transparency.
http://www.imagemagick.org/Usage/compose/#copy

This compose method is not often used, but in this case it is ideal.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Creating an image corner relative to the size of the pic

Post by fmw42 »

anthony wrote:The barycentric method use to generate the gradient ensures that the gradient falling bteeen thw top-left to bottom-right diagonal, is at a 50% level between the top-right and bottom-left.

But if the image is square, then a baricentric with just two points will do the job (IM will calculate the third point)

-sparse-color barycentric '0,0 white %w,%h black'

Just a matter of coloring and setting transparency in that image.

As the overlay is square with transparency, just overlay it using 'Copy' Compostion.
That replaces just the overlay area with both color and transparency.
http://www.imagemagick.org/Usage/compose/#copy

This compose method is not often used, but in this case it is ideal.
Thanks Anthony. I knew there was compose method to do that, but could not think of it.

Here is the revised script according to Anthony's suggestions, which avoids the matte floodfill -draw:


infile="logo:"
pct=20
size=`convert $infile -format "%[fx:$pct*min(w,h)/100]" info:`
convert $infile \
\( -size ${size}x${size} xc: -sparse-color barycentric '0,0 red %w,%h black' +repage \) \
\( -size ${size}x${size} xc: -set colorspace RGB -sparse-color barycentric '0,0 white %w,%h black' \
-threshold 50% +repage \) \
\( -clone 1 -clone 2 -alpha off -compose copy_opacity -composite \) \
-delete 1,2 -gravity southeast -alpha set -compose copy -composite \
logo_redcorner.png
louim
Posts: 2
Joined: 2012-07-09T10:05:36-07:00
Authentication code: 13

Re: Creating an image corner relative to the size of the pic

Post by louim »

Many thanks!!!

It is exactly what I needed. I'm really impressed by the image magick community. Quick and efficient answers!

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

Re: Creating an image corner relative to the size of the pic

Post by anthony »

fmw42 wrote:Thanks Anthony. I knew there was compose method to do that, but could not think of it.
try this... Uses a large fuzz to set transparency :-)
You can now overlay that directly using just one 'Copy' compose.

Code: Select all

convert -size 256x256 xc: -set colorspace RGB \
            -sparse-color barycentric '0,0 white %w,%h black' \
            -fuzz 50% -transparent black \
            +level-colors black,red show:
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply