Jump to content

combining transparent images


Recommended Posts

hello ... i'm relatively new to AI scripts and have been trying to create a script that will do the following.

Open a file, change it's transparency level to a user defined level, then merge this with another image and save the result as a transparent png file ...

Basically, I have two images. One is a black filled rectangle and one is a border or frame for around this rectangle. I would like to be able to change the transparency of the black rectangle from a var passed to the script and then merge background with the border to create a new png file.

At the moment I can merge two files (jpg or png) but I cannot figure out how to manipulate the transparency of the first image before saving the output file.

Any help would be greatly appreciated.

Val.

Link to comment
Share on other sites

Hi,

Without me getting to in depth, then maybe have a look at AutoIt help file under "User Defined Functions ->GDIPlus Management"

Some functions that may help assist you with your task at hand:

_GDIPlus_ImageLoadFromFile($sFileName)

_GDIPlus_BrushCreateSolid([$iARGB = 0xFF000000])

_GDIPlus_ImageSaveToFile($hImage, $sFileName)

Also maybe have a look at "User Defined Functions ->WinAPI Management"

Some more functions that may help assist you with your task at hand:

_WinAPI_GetDC($hWnd)

_WinAPI_CreateCompatibleDC($hDC)

_WinAPI_CreateCompatibleBitmap($hDC, $iWidth, $iHeight)

If you feel you can understand some of the above functions, but are still having a problem linking it all together then post the code your trying and maybe someone may be be able to assist a little more.

Cheers

Edit: Sorry forgot to say "Welcome to AutoIt and the forums" :)

Edited by smashly
Link to comment
Share on other sites

Hi,

Without me getting to in depth, then maybe have a look at AutoIt help file under "User Defined Functions ->GDIPlus Management"

Some functions that may help assist you with your task at hand:

_GDIPlus_ImageLoadFromFile($sFileName)

_GDIPlus_BrushCreateSolid([$iARGB = 0xFF000000])

_GDIPlus_ImageSaveToFile($hImage, $sFileName)

Also maybe have a look at "User Defined Functions ->WinAPI Management"

Some more functions that may help assist you with your task at hand:

_WinAPI_GetDC($hWnd)

_WinAPI_CreateCompatibleDC($hDC)

_WinAPI_CreateCompatibleBitmap($hDC, $iWidth, $iHeight)

If you feel you can understand some of the above functions, but are still having a problem linking it all together then post the code your trying and maybe someone may be be able to assist a little more.

Cheers

Edit: Sorry forgot to say "Welcome to AutoIt and the forums" :)

thanks smashly, i'll have a look at those and see how I get on ... but I'd say I might need more help.

Link to comment
Share on other sites

One way to do it is with GDI+ ColorMatrix, which is meant for tweaking RGBA values easily (another example):

#Include <GDIPlus.au3>

_GDIPlus_Startup()

$hImage1 = _GDIPlus_ImageLoadFromFile(@DesktopDir & "\test.jpg")
$hImage2 = _GDIPlus_ImageLoadFromFile(@DesktopDir & "\overlay.jpg")
$hGraphics1 = _GDIPlus_ImageGetGraphicsContext($hImage1)

_GDIPlus_GraphicsDrawImageTrans($hGraphics1, $hImage2, 0, 0, 0.66)

_GDIPlus_ImageSaveToFile($hImage1, @DesktopDir & "\test_merged.png")

_GDIPlus_GraphicsDispose($hGraphics1)
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)

_GDIPlus_ShutDown()

Exit


;; _GDIPlus_GraphicsDrawImageTrans()
;;      Draw Image object with transparency
Func _GDIPlus_GraphicsDrawImageTrans($hGraphics, $hImage, $iX, $iY, $nTrans)
    Local $tColorMatrix, $x, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage)
;;create color matrix data
    $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]")
    ;blending values:
    $x = DllStructSetData($tColorMatrix, 1, 1, 1) * DllStructSetData($tColorMatrix, 2, 1, 2) * DllStructSetData($tColorMatrix, 3, 1, 3) * _
        DllStructSetData($tColorMatrix, 4, $nTrans, 4) * DllStructSetData($tColorMatrix, 5, 1, 5)
;;create an image attributes object and update its color matrix
    $hImgAttrib = _GDIPlus_ImageAttributesCreate()
    _GDIPlus_ImageAttributesSetColorMatrix($hImgAttrib, 1, DllStructGetPtr($tColorMatrix))
;;draw image into graphic object with alpha blend
    _GDIPlus_GraphicsDrawImageRectRectEx($hGraphics, $hImage, 0, 0, $iW, $iH, $iX, $iY, $iW, $iH, 2, $hImgAttrib)
;;clean up
    _GDIPlus_ImageAttributesDispose($hImgAttrib)
    Return
EndFunc


;;;;; other GDIPlus wrappers ;;;;;

#cs
_GDIPlus_ImageAttributesSetColorMatrix()
    Sets ColorMatrix of ImageAttributes object
    Parameters:
        $hImgAttrib = ImageAttributes object
        $iColorAdjustType = can be:
            ColorAdjustTypeDefault                =  0
            ColorAdjustTypeBitmap                 =  1
            ColorAdjustTypeBrush                  =  2
            ColorAdjustTypePen                    =  3
            ColorAdjustTypeText                   =  4
            ColorAdjustTypeCount                  =  5
            ColorAdjustTypeAny (Reserved)         =  6
        $pColorMatrix = pointer to ColorMatrix structure
        $pGrayMatrix = pointer to GreyMatrix structure
        $iColorMatrixFlags = can be:
            ColorMatrixFlagsDefault               =  0
            ColorMatrixFlagsSkipGrays             =  1
            ColorMatrixFlagsAltGray               =  2
    Return value: True/False
#ce
Func _GDIPlus_ImageAttributesSetColorMatrix($hImgAttrib, $iColorAdjustType, $pColorMatrix = 0, $pGrayMatrix = 0, $iColorMatrixFlags = 0)
    Local $fEnable = 1, $aResult = DllCall($ghGDIPDll, "int", "GdipSetImageAttributesColorMatrix", "ptr",$hImgAttrib, "int",$iColorAdjustType, _
                                            "int",$fEnable, "ptr",$pColorMatrix, "ptr",$pGrayMatrix, "int",$iColorMatrixFlags)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc
;Create ImageAttributes object
Func _GDIPlus_ImageAttributesCreate()
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipCreateImageAttributes", "ptr*", 0)
    Return SetError($aResult[0], 0, $aResult[1])
EndFunc
;Delete ImageAttributes object
Func _GDIPlus_ImageAttributesDispose($hImgAttrib)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDisposeImageAttributes", "ptr", $hImgAttrib)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc
;Same as _GDIPlus_GraphicsDrawImageRectRect(), but adds 1 optional parameter - $hImgAttrib (handle to ImageAttributes object)
Func _GDIPlus_GraphicsDrawImageRectRectEx($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth, $iSrcHeight, $iDstX, $iDstY, $iDstWidth, $iDstHeight, $iUnit = 2, $hImgAttrib = 0)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDrawImageRectRectI", "hwnd", $hGraphics, "hwnd", $hImage, "int", $iDstX, "int", _
            $iDstY, "int", $iDstWidth, "int", $iDstHeight, "int", $iSrcX, "int", $iSrcY, "int", $iSrcWidth, "int", _
            $iSrcHeight, "int", $iUnit, "ptr", $hImgAttrib, "int", 0, "int", 0)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc

;;=====================================================

"be smart, drink your wine"

Link to comment
Share on other sites

hey Siao,

that worked perfectly, it is exactly what I was trying to do, thanks so much. I would NEVER have been able to come up with script like that ...

thanks again ...

valheru.

sorry about this Saio, but one last question.

Your script fades image2 and then overlays it on image1. How do I change it to fade image1 and then overlay image2 onto the faded image1 ?

thanks in advance ...

valheru.

Link to comment
Share on other sites

sorry about this Saio, but one last question.

Your script fades image2 and then overlays it on image1. How do I change it to fade image1 and then overlay image2 onto the faded image1 ?

thanks in advance ...

valheru.

Something like that (not optimal, and depending on what you are trying to do, could be simplified):

_GDIPlus_Startup()

$hImage0 = _GDIPlus_ImageLoadFromFile(@DesktopDir & "\test.jpg")
$hImage1 = _GDIPlus_ImageLoadFromFile(@DesktopDir & "\test.jpg")
$hImage2 = _GDIPlus_ImageLoadFromFile(@DesktopDir & "\overlay.jpg")
$hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage0)

_GDIPlus_GraphicsFillRect($hGraphics, 0, 0, _GDIPlus_ImageGetWidth($hImage1), _GDIPlus_ImageGetHeight($hImage1), 0)
_GDIPlus_GraphicsDrawImageTrans($hGraphics, $hImage1, 0, 0, 0.66)
_GDIPlus_GraphicsDrawImage($hGraphics, $hImage2, 0, 0)

_GDIPlus_ImageSaveToFile($hImage0, @DesktopDir & "\test_merged.png")

_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_ImageDispose($hImage0)
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)

_GDIPlus_ShutDown()

I mean, what's the problem? If you want to draw image into graphic object wtih transparency, use _GDIPlus_GraphicsDrawImageTrans(); if you want to draw image without transparency, use _GDIPlus_GraphicsDrawImage()

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

Something like that (not optimal, and depending on what you are trying to do, could be simplified):

_GDIPlus_Startup()

$hImage0 = _GDIPlus_ImageLoadFromFile(@DesktopDir & "\test.jpg")
$hImage1 = _GDIPlus_ImageLoadFromFile(@DesktopDir & "\test.jpg")
$hImage2 = _GDIPlus_ImageLoadFromFile(@DesktopDir & "\overlay.jpg")
$hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage0)

_GDIPlus_GraphicsFillRect($hGraphics, 0, 0, _GDIPlus_ImageGetWidth($hImage1), _GDIPlus_ImageGetHeight($hImage1), 0)
_GDIPlus_GraphicsDrawImageTrans($hGraphics, $hImage1, 0, 0, 0.66)
_GDIPlus_GraphicsDrawImage($hGraphics, $hImage2, 0, 0)

_GDIPlus_ImageSaveToFile($hImage0, @DesktopDir & "\test_merged.png")

_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_ImageDispose($hImage0)
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)

_GDIPlus_ShutDown()

I mean, what's the problem? If you want to draw image into graphic object wtih transparency, use _GDIPlus_GraphicsDrawImageTrans(); if you want to draw image without transparency, use _GDIPlus_GraphicsDrawImage()

thanks siao .. i managed to use your original script and modified it a little to get what i needed ... thanks again for your help ... valheru

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...