Insert SVG into a non-trivial frame?

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
Fry-kun
Posts: 8
Joined: 2019-12-02T16:51:28-07:00
Authentication code: 1152

Insert SVG into a non-trivial frame?

Post by Fry-kun » 2019-12-02T17:42:51-07:00

I have some frames (as in "picture frame") with transparency inside & out and a generic SVG, I would like to insert the SVG as a photo into the frames.
The problem is that the frames are not consistent size & shape (inner transparency of the frame is mostly a rectangle, but can have other embellishments). The SVG is also not perfectly matching with ratio of frame.

What's a reasonable way of making such framing mostly automated?
From my limited experience with IM, I'm thinking I can create a transparency mask and (somehow?) get rid of the outer transparency & frame, then resize down and that'll give me the size (& position?) for my insert..

Any advice?
Thanks!

User avatar
fmw42
Posts: 25761
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Insert SVG into a non-trivial frame?

Post by fmw42 » 2019-12-02T18:53:08-07:00

Please post an example image to help us understand the issue. Also always post your Imagemagick version and platform/OS when asking questions as syntax may vary.

Fry-kun
Posts: 8
Joined: 2019-12-02T16:51:28-07:00
Authentication code: 1152

Re: Insert SVG into a non-trivial frame?

Post by Fry-kun » 2019-12-02T19:10:17-07:00

Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org
Platform: linux

Frame:
Image
(note the gradual transparency around the outer edges & not straight lines along the inner edges)

background image:
Image

Wanted result:
Image

It's ok to clip the filler background, but it should stretch all the way through to the frame. It shouldn't extend past the frame.

Fry-kun
Posts: 8
Joined: 2019-12-02T16:51:28-07:00
Authentication code: 1152

Re: Insert SVG into a non-trivial frame?

Post by Fry-kun » 2019-12-02T23:52:51-07:00

If it helps, the inner image can be stretched all the way to the edges, but shouldn't be visible past the frame's outer transparency

User avatar
fmw42
Posts: 25761
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Insert SVG into a non-trivial frame?

Post by fmw42 » 2019-12-03T10:54:41-07:00

Trim the frame to its outer boundary that removes all outer transparency. Get its with and height. Then rasterize the SVG and resize to those dimensions. See -define trim:percent-background=0% at https://imagemagick.org/script/command- ... s.php#trim

If you can post a link to your actual SVG file and actual frame, we can try to help further.

Fry-kun
Posts: 8
Joined: 2019-12-02T16:51:28-07:00
Authentication code: 1152

Re: Insert SVG into a non-trivial frame?

Post by Fry-kun » 2019-12-03T12:48:32-07:00

This was very helpful, thank you!

I tried to do this on the alpha mask of the frame (8-bit Gray 256c), but it doesn't trim all of the background for some reason.

Code: Select all

convert frame.png -alpha extract frame_mask.png
Image

Code: Select all

convert frame_mask.png -fuzz 99% -define trim:percent-background=0% -trim fm_trim_f99.png
Image
Unfortunately, this still leaves some of the background.

I tried changing the pallete to 2 colors:

Code: Select all

convert frame_mask.png -fuzz 5% -fill black +opaque '#FFFFFF' frame_mask_2c.png
Image

Code: Select all

convert frame_mask_2c.png -fuzz 99% -define trim:percent-background=0% -trim fm2c_trim_f99.png
Image

User avatar
fmw42
Posts: 25761
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Insert SVG into a non-trivial frame?

Post by fmw42 » 2019-12-03T15:48:41-07:00

Try this

Code: Select all

convert frame.png -alpha extract -bordercolor black -border 20 -fuzz 99% -define trim:background-color=black -define trim:percent-background=0% -trim +repage result.png


or

Code: Select all

convert frame.png -bordercolor none -border 20 -fuzz 99% -define trim:background-color=none -define trim:percent-background=0% -trim +repage result.png.png

Fry-kun
Posts: 8
Joined: 2019-12-02T16:51:28-07:00
Authentication code: 1152

Re: Insert SVG into a non-trivial frame?

Post by Fry-kun » 2019-12-04T19:00:45-07:00

I came up with an option that seems to work:

Code: Select all

convert frame.png
  -alpha extract					# extract alpha channel
  -bordercolor black -border 1				# add black 1px border (in case frame touches the edges)
  -fuzz 99% -fill white -draw "color 0,0 floodfill"	# flood fill the border transparency to make it opaque (fuzz to avoid errant transparencies)
  -fuzz 99% -define trim:percent-background=0% -trim	# trim the now-consistent border
  -print "%wx%h" null:					# finally, print the width/height (since I don't need the image itself)
From there, I resize my background and add back the transparent border with

Code: Select all

... -resize "WxH^"
  -gravity center -crop "WxH+0+0"
  -background none -extent <ORIG_SIZE>
(+color correction)

And finally

Code: Select all

composite frame.png background.png result.png
Actually, unlike the hand-drawn example here, my files are centered, so this works.. but would be great if I could get the offset of the intermediate step (1st step here) so I don't have to rely on that.

Fry-kun
Posts: 8
Joined: 2019-12-02T16:51:28-07:00
Authentication code: 1152

Re: Insert SVG into a non-trivial frame?

Post by Fry-kun » 2019-12-04T19:04:41-07:00

If I pass -identify, I see

Code: Select all

frame.png PNG 640x480=>451x291 642x482+100+94 8-bit sRGB 15718B 0.010u 0:00.000
is +100+94 what I want?

User avatar
fmw42
Posts: 25761
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Insert SVG into a non-trivial frame?

Post by fmw42 » 2019-12-04T19:42:55-07:00

After -trim add +repage to remove the virtual canvas

Fry-kun
Posts: 8
Joined: 2019-12-02T16:51:28-07:00
Authentication code: 1152

Re: Insert SVG into a non-trivial frame?

Post by Fry-kun » 2019-12-05T11:45:53-07:00

How do I extract +100+94 from the earlier result?

If I use that as

Code: Select all

composite -compose Dst_Over -geometry +100+94 bg.png frame.png  res.png
then I get exactly what I need

P.S. +repage gets rid of this info (at least in output of -identify), so I probably don't want to do that

Fry-kun
Posts: 8
Joined: 2019-12-02T16:51:28-07:00
Authentication code: 1152

Re: Insert SVG into a non-trivial frame?

Post by Fry-kun » 2019-12-05T11:54:37-07:00

Found it:

Code: Select all

-print '%wx%h %O'
(gets me the cropped size & offset)

User avatar
fmw42
Posts: 25761
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Insert SVG into a non-trivial frame?

Post by fmw42 » 2019-12-05T15:03:35-07:00

composite is a rather primitive tool. I suggest you use convert ... -compose Dst_Over -composite syntax. See https://imagemagick.org/Usage/compose/#compose. The latter is much more flexible.

Post Reply