AWS Thinkbox Discussion Forums

Compositing Two Images: Blending Options?

Can someone detail the compositing options with Draft? Is there a way to blend images or apply operations to one channel? Specifically, I’m looking to overlay image A over B but with a transparency value. I know it’s possible with ImageMagik but not sure if your wrapper allows for this. If it does not, could that be a request? I think this would be of tremendous value (for me anyway!).

Does image A already have the transparency you want?

Blending Images

You’ll need to use one of the Composite methods. These are:

  • Image.Composite(sourceImage, leftOffset, bottomOffset, compositeOperator)
  • Image.CompositeWithGravity(sourceImage, positionalGravity, compositeOperator)
  • Image.CompositeWithPositionAndGravity(sourceImage, leftOffset, bottomOffset, compositeOperator, gravity)

And you’ll probably want to use the Draft.CompositeOperator.OverCompositeOp compositeOperator.

For example, the following script puts A centered over B:

import Draft A = Draft.Image.ReadFromFile("c:/users/paul/A.png") B = Draft.Image.ReadFromFile("c:/users/paul/B.png") B.CompositeWithGravity(A, Draft.PositionalGravity.CenterGravity, Draft.CompositeOperator.OverCompositeOp) B.WriteToFile("c:/users/paul/out.png")

For more information on these methods, and for a list of all available compositing operations, please see:

viewtopic.php?f=125&t=6892#p27829

Please don’t hesitate to ask if you have any questions. And please let us know if there is some other ImageMagick command that you’d like to access in Draft.

Apply Operations to a Single Channel

Currently the only operations that work on a single channel are the copy operations:

  • CompositeOperator.CopyRedCompositeOp
  • CompositeOperator.CopyGreenCompositeOp
  • CompositeOperator.CopyBlueCompositeOp
  • CompositeOperator.CopyOpacityCompositeOp

Is there some other single-channel operation you have in mind?

Now let’s say your images don’t have an alpha channel, and you want to composite B over A with 50% transparency.

What we need to do is create an extra image with the desired transparency, and copy that transparency into image B. This is a bit awkward, and suggests that we may need a method to set individual channels directly.

[code]import Draft
a = Draft.Image.ReadFromFile(“c:/users/paul/A.png”)
b = Draft.Image.ReadFromFile(“c:/users/paul/B.png”)

Create an image with 50% transparency (alpha = 0.5)

b_alpha_image = Draft.Image.CreateImage(b.width,b.height)
b_alpha_image.SetToColor(Draft.ColorRGBA(0, 0, 0, 0.5))

Copy the 50% transparency into image b

b.Composite(b_alpha_image, 0, 0, Draft.CompositeOperator.CopyOpacityCompositeOp)

Composite b onto a

a.CompositeWithGravity(b, Draft.PositionalGravity.CenterGravity, Draft.CompositeOperator.OverCompositeOp)
a.WriteToFile(“c:/users/paul/out.png”)[/code]

Shouldn’t a number < 1 in the alpha of an annotationInfo.Color call make that text transparent (same for shadow)?

I’m using the Resizable_Demo_Template2.py as my template. Why doesn’t the below give me transparent letters?

annotationInfo.Color = ColorRGBA(0.85, 0.85, 0.85, 0.3)
annotationInfo.DrawShadow = True
annotationInfo.ShadowColor = ColorRGBA(0.1, 0.1, 0.1, 0.3)

annotationInfo.PointSize = int(height*0.025)
annotation = Image.CreateAnnotation(params['username'], annotationInfo)
bgFrame.CompositeWithPositionAndGravity(annotation, 0.025, 0.015, PositionalGravity.CenterGravity, CompositeOperator.OverCompositeOp)

Also, my movie file name comes out as “SYS_ProxyTest_comp_XXX_v01_bg…mov” … is there an easy way to remove one of the two .'s before “mov”?

Yes, it should.

I notice the transparency is reset in several places in Resizable_Demo_Template2.py. I suspect it may be getting reset to opaque. Could you please try the attached script?

Resizable_Demo_Template2_transparent_text.zip (1.69 KB)

You can do this in your Draft script:

(outBase, outExt)= os.path.splitext( params['outFile'] ) outBase = outBase.rstrip('.') videoEncoder = VideoEncoder( outBase + outExt, fps, origBgFrame.width, origBgFrame.height, kBitRate, codec )
I understand Jon is considering changing the Draft submission scripts to do this automatically.

I’ve been trying to get a Draft template to work that scales everything correctly to 1024x778, adding black on top and bottom if the source image is not 1.33. I’ve been trying to create a new image that is 1024x778 and then copy the existing image, scaled to 1024 width, in the center of it. I’ve attached the job directory as well as the script in question.

Does the resolution (ie. height/width) of the image being introduced through the composite command need to be smaller than the “base” image?

Why does this error below seem to say the command is asking for TWO image inputs when the documentation and the example scripts all only ever have one image called by the Composite command?


0: STDOUT: Processing frame: 195
0: STDOUT: Traceback (most recent call last):
0: STDOUT: File “/Local/Farm18/jobsData/1K_1_movie.py”, line 144, in
0: STDOUT: bgFrame.CompositeWithPositionAndGravity(bgFrameAdd, 0, 0, CompositeOperator.AddCompositeOp, PositionalGravity.CenterGravity )
0: STDOUT: Boost.Python.ArgumentError: Python argument types in
0: STDOUT: Image.CompositeWithPositionAndGravity(Image, Image, int, int, CompositeOperator, PositionalGravity)
0: STDOUT: did not match C++ signature:
0: STDOUT: CompositeWithPositionAndGravity(frantic_image::image* self, frantic_image::image* image, float x, float y, MagickCore::GravityType operation, MagickCore::CompositeOperator gravity)
0: INFO: Process exit code: 1
1K_1_movie01.py.zip (1.92 KB)
00j_100_005_1f094712.zip (5.93 KB)

It looks like you’ll need to flip around the CompositeOperator and PositionalGravity arguments on line 144, like this:

bgFrame.CompositeWithPositionAndGravity(bgFrameAdd, 0, 0, PositionalGravity.CenterGravity, CompositeOperator.AddCompositeOp )

That error message is a little weird. In Python, behind the scenes, the first parameter is the image you’re invoking the command on. For example, the line above is transformed into CompositeWithPositionAndGravity( bgFrame, bgFrameAdd, 0, 0, …etc… ). This can lead to confusing error messages, but otherwise you don’t need to worry about it.

Edit: I just noticed these parameters are flipped around in our documentation. This will be fixed in the next build.

Regarding the alpha problem you described earlier:

Are you using OS X or Linux?

The other day I tested this on Windows, where it works fine. Today I did some testing on other platforms, and I noticed that it’s broken on OS X and Linux. We’ll look into fixing this.

This bug should be fixed in Draft Beta 9. Please let us know if it still doesn’t work correctly.

We added a SetChannel( channel, value ) method in Draft Beta 9, which we hope will make this a bit more straightforward. For example, if you want to composite B over A with 50% transparency:

[code]import Draft
a = Draft.Image.ReadFromFile(“c:/users/paul/A.png”)
b = Draft.Image.ReadFromFile(“c:/users/paul/B.png”)

Set b’s transparency to 50% (‘A’ = 0.5)

b.SetChannel( ‘A’, 0.5 )

Composite b onto a

a.CompositeWithGravity(b, Draft.PositionalGravity.CenterGravity, Draft.CompositeOperator.OverCompositeOp)
a.WriteToFile(“c:/users/paul/out.png”)[/code]

Privacy | Site terms | Cookie preferences