Jump to content

Merging Images together


Recommended Posts

I was wondering how I could merge multiple image files together, side-by-side, or stack them over each other.

I searched and I found this post but I didn't see how it could help me.

I just want to know how I could at least stick two images together pixel-to-pixel and then save them as one image.

Thanks

Link to comment
Share on other sites

I was wondering how I could merge multiple image files together, side-by-side, or stack them over each other.

I searched and I found this post but I didn't see how it could help me.

I just want to know how I could at least stick two images together pixel-to-pixel and then save them as one image.

Thanks

Here is another link for combining images.

http://www.autoitscript.com/forum/index.ph...st&p=517195

This is an example of combining images using GDI+.

_GDIPlus_DrawImagePoints(),

_GDIPlus_GraphicsDrawImageRectRect(), and,

_GDIPlus_GraphicsDrawImageRect

are used as three ways of drawing the image to the graphics.

Hope it helps.

If this is too difficult, go with Zedna's recommendation.

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1) ;0=disabled, 1=OnEvent mode enabled

Global $GuiSizeX = 600, $GuiSizeY = 600
Global $iX1, $iY1, $iX2, $iY2, $iX3, $iY3, $iX4, $iY4, $hImage1, $hImage2, $hImage3, $hImage4

$hGui = GUICreate("GDIPlus Example", $GuiSizeX, $GuiSizeY)
GUISetOnEvent(-3, "_Quit")
GUISetState()

_GDIPlus_Startup()
$hImage1 = _GDIPlus_ImageLoadFromFile(@WindowsDir & "\Web\Wallpaper\Autumn.jpg")
$iX1 = _GDIPlus_ImageGetWidth($hImage1)
$iY1 = _GDIPlus_ImageGetHeight($hImage1)
$hImage2 = _GDIPlus_ImageLoadFromFile(@WindowsDir & "\Web\Wallpaper\Azul.jpg")
$iX2 = _GDIPlus_ImageGetWidth($hImage2)
$iY2 = _GDIPlus_ImageGetHeight($hImage2)
$hImage3 = _GDIPlus_ImageLoadFromFile(@WindowsDir & "\Web\Wallpaper\Home.jpg")
$iX3 = _GDIPlus_ImageGetWidth($hImage3)
$iY3 = _GDIPlus_ImageGetHeight($hImage3)
$hImage4 = _GDIPlus_ImageLoadFromFile(@WindowsDir & "\Web\Wallpaper\Tulips.jpg")
$iX4 = _GDIPlus_ImageGetWidth($hImage4)
$iY4 = _GDIPlus_ImageGetHeight($hImage4)

; Create Double Buffer, so the doesn't need to be repainted on PAINT-Event
$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui) ;Draw to this graphics, $hGraphicGUI, to display on GUI
$hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($GuiSizeX, $GuiSizeY, $hGraphicGUI); $hBMPBuff is a bitmap in memory
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff); Draw to this graphics, $hGraphic, being the graphics of $hBMPBuff
;End Double Buffer add-in 1 of 3

;Graphics here
_GDIPlus_GraphicsClear($hGraphic, 0xFFE8FFE8)
;$hBrush = _GDIPlus_BrushCreateSolid(0xFF0080FF)
;_GDIPlus_GraphicsFillEllipse($hGraphic, 3, 3, 50, 90, $hBrush)

$hPen = _GDIPlus_PenCreate(0xFFFF0000, 3)
;_GDIPlus_GraphicsDrawLine($hGraphic, 0, $GuiSizeX, $GuiSizeY, 0, $hPen)

;====== Draw images to the $hBMPBuff bitmap's graphics, $hGraphic. ==============
_GDIPlus_DrawImagePoints($hGraphic, $hImage1, 0, 0, 280, 20, 20, 280); Skewed
;_GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage1, 0, 0,  $iX1, $iY1, 0, 0,300, 300)

_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 305, 5, 290, 290); <== Easiest to use
;_GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage2, 0, 0, $iX2, $iY2, 305, 5, 290, 290)

_GDIPlus_DrawImagePoints($hGraphic, $hImage3, 0, 600, 300, 600, 0, 300) ; Upside down
;_GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage3, 0, 0, $iX3, $iY3, 0, 310, 300, 300)

_GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage4, 290, 290, 300, 300); <== Easiest to use
;_GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage4, $iX4/2, $iY4/2, $iX4/2, $iY4/2, 290, 290, 300, 300);Zoom in
;_GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage4, 0, 0, $iX4*2, $iY4*2, 290, 290, 300, 300);Zoom out

; Draw text
_GDIPlus_GraphicsDrawString($hGraphic, "Image #1", 10, 140)
_GDIPlus_GraphicsDrawString($hGraphic, "Image #2", 310, 140)
_GDIPlus_GraphicsDrawString($hGraphic, "Image #3", 10, 440)
_GDIPlus_GraphicsDrawString($hGraphic, "Image #4", 310, 440)

_GDIPlus_GraphicsDrawRect($hGraphic, 290 - 2, 290 - 2, 300 + 3, 300 + 3, $hPen); $hPen 3 pixels thick

;End of graphics

GUIRegisterMsg(0xF, "MY_PAINT"); Register PAINT-Event 0x000F = $WM_PAINT (WindowsConstants.au3)
GUIRegisterMsg(0x85, "MY_PAINT") ; $WM_NCPAINT = 0x0085 (WindowsConstants.au3)Restore after Minimize.

_GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0); Draw bitmap, $hBMPBuff, to the GUI's graphics, $hGraphicGUI.

;Save composite image
Local $sNewName = @DesktopDir & "\ImageCombine.png"
_GDIPlus_ImageSaveToFile($hBMPBuff, $sNewName) ; $hBMPBuff the bitmap
ShellExecute($sNewName)

While 1
    Sleep(20)
WEnd

;Func to auto-redraw on Windows internal PAINT messages.
Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    ;_WinAPI_RedrawWindow($hGui, "", "", BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME)) ; , $RDW_ALLCHILDREN
    Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_PAINT

Func _Quit()
    ;_GDIPlus_BrushDispose($hBrush)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_ImageDispose($hImage3)
    _GDIPlus_ImageDispose($hImage4)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hGraphicGUI)
    _WinAPI_DeleteObject($hBMPBuff)
    _GDIPlus_Shutdown()
    Exit
EndFunc   ;==>_Quit
Link to comment
Share on other sites

If you are not skilled in GDI+ then you can use external graphics tools (like IrfanView,XNView, ...) via commandline.

or better... learn GDI+ :)

Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Link to comment
Share on other sites

  • 2 years later...

hop... little image merge script inspired from previous ones.

I'm using it to merge 2 images side by side, horizontally, for my dual screen desktop.

Preconfig ini file : Merge2Images.ini

#~ -----------------------------------8<-----------------------------------
[Config]

MergedImageBackgroundColor  = 0xFF000000
    #~ 0x00...... : Specify 00 on first "digits" for transparent color (.png only !), all other values (......) will be ignored.
    #~ 0xFFRRGGBB : RGB : RR=red color level / GG=green color level / BB=blue color level
    #~ 0xFFFFFFFF : white
    #~ 0xFF000000 : black
    #~ 0xFF0000FF : blue

CenterImageVertically       = True
StrechImageVertically       = False

ShowFinaleMergedImage       = true



#~ -----------------------------------8<-----------------------------------
#~ For Debug only (you should not touch this !):
[Debug]
ShowMergeGUIWindow              = False
SleepTimeShowMergeGUIWindow     = 2000

Merge2Images.au3 script (taking the 2 images as command line parameter)

#include <GDIPlus.au3>
#include <GuiConstants.au3>
Opt("GUIOnEventMode", 1)                    ;0=disabled, 1=OnEvent mode enabled

; -----------------------------------8<-----------------------------------
$ScriptTitle                = @ScriptName ; "Merge2Images"
$ScriptRev                  = "A.02"
; -----------------------------------8<-----------------------------------
Global $Title               = $ScriptTitle & " - " & $ScriptRev
Global $IniFile             = StringTrimRight(@ScriptFullPath, 4) & ".ini"
; -----------------------------------8<-----------------------------------


; TBD (not specially in this order) :
;   - Define size of merged image X * Y
;   - Center Horizontally (X) images ? ... need "Define size of merged image X * Y" predefined !
;   - Strech Horizontally (X) images ? ... need "Define size of merged image X * Y" predefined !


; -----------------------------------8<-----------------------------------
$MergedImageBackgroundColor = GetFromIni("Config","MergedImageBackgroundColor", 0xFF000000)
$ShowFinaleMergedImage      = GetFromIni("Config","ShowFinaleMergedImage",      False)
$CenterImageVertically      = GetFromIni("Config","CenterImageVertically",      False)
$StrechImageVertically      = GetFromIni("Config","StrechImageVertically",      False)

$ShowMergeGUIWindow         = GetFromIni("Debug","ShowMergeGUIWindow",          False)
$SleepTimeShowMergeGUIWindow= GetFromIni("Debug","SleepTimeShowMergeGUIWindow", 2000)

If $CmdLine[0] < 2 Then
    MsgBox(0+48,$Title,"Usage is:" & @CR & _
                @TAB & @ScriptName & "  <ImageName1>  <ImageName2>  [<MergedImageName.png|MergedImageName.jpg>]" & @CR & @CR & _
                "Size of MergedImage destination file will be :" & @CR & _
                @TAB & "X Size = " & @TAB & "X ImageName1 size  +  X ImageName2" & @CR & _
                @TAB & "Y Size = " & @TAB & "Size Max [ Y ImageName1  ,  Y ImageName2]")
    Exit
EndIf

$ImageName1                     = $CmdLine[1]
$ImageName2                     = $CmdLine[2]
If $CmdLine[0] = 3 Then
    $ImageCompositeName         = $CmdLine[3]
Else
    $ImageCompositeName         = "Merged_" & $CmdLine[1] & "+" & $CmdLine[2] & ".png"              ; .jpg -or- .png
EndIf




; -----------------------------------8<-----------------------------------
$GuiSizeX = 0
$GuiSizeY = 0


; -----------------------------------8<-----------------------------------
_GDIPlus_Startup()

; Load image 1
$hImage1    = _GDIPlus_ImageLoadFromFile($ImageName1)
$iX1        = _GDIPlus_ImageGetWidth($hImage1)
$iY1        = _GDIPlus_ImageGetHeight($hImage1)


; Load image 2
$hImage2    = _GDIPlus_ImageLoadFromFile($ImageName2)
$iX2        = _GDIPlus_ImageGetWidth($hImage2)
$iY2        = _GDIPlus_ImageGetHeight($hImage2)


; -----------------------------------8<-----------------------------------
; Define the Merged (Composite) image size
$GuiSizeX = $iX1 + $iX2
If $iY1 > $iY2 then
    $GuiSizeY = $iY1
Else
    $GuiSizeY = $iY2
EndIf



; -----------------------------------8<-----------------------------------
; Initialise the Drawing windows/composite image...
$hGui = GUICreate("GDIPlus Example", $GuiSizeX, $GuiSizeY)
; Show drawing window ?
If $ShowMergeGUIWindow Then
    GUISetState()
    GUISetOnEvent(-3, "_Quit")
EndIf



; -----------------------------------8<-----------------------------------
; Create Double Buffer, so the doesn't need to be repainted on PAINT-Event
$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui)                                   ;Draw to this graphics, $hGraphicGUI, to display on GUI
$hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($GuiSizeX, $GuiSizeY, $hGraphicGUI)       ; $hBMPBuff is a bitmap in memory
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)                                 ; Draw to this graphics, $hGraphic, being the graphics of $hBMPBuff



; -----------------------------------8<-----------------------------------
; Fill Graphics zone in black color : _GDIPlus_GraphicsClear($hGraphic)
; Fill Graphics zone in white color : _GDIPlus_GraphicsClear($hGraphic, 0xFFFFFFFF)
; Fill Graphics zone in transparent : <nothing if Merged ImageExt is .png, default back color if Merged ImageExt is .jpg>
_GDIPlus_GraphicsClear($hGraphic, $MergedImageBackgroundColor)


; -----------------------------------8<-----------------------------------

;~ ; Merge 2 images (default)
;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage1, 0, 0, $iX1, $iY1,    0, 0, $iX1, $iY1)
;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage2, 0, 0, $iX2, $iY2, $iX1, 0, $iX2, $iY2)


; Image 2 is VCentered
;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage1, 0, 0, $iX1, $iY1,    0, 0, $iX1, $iY1)
;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage2, 0, 0, $iX2, $iY2, $iX1, ($iY1-$iY2)/2, $iX2, $iY2)


; VCenter both images
;~ $VPos = 0
;~ If $CenterImageVertically Then $VPos = ($GuiSizeY - $iY1)/2
;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage1, 0, 0, $iX1, $iY1,    0, $VPos, $iX1, $iY1)
;~ If $CenterImageVertically Then $VPos = ($GuiSizeY - $iY2)/2
;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage2, 0, 0, $iX2, $iY2, $iX1, $VPos, $iX2, $iY2)

;~ Vertical Strech
;~ $YStrech = $iY1
;~ If $StrechImageVertically Then $YStrech = $GuiSizeY
;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage1, 0, 0, $iX1, $iY1,    0, 0, $iX1, $YStrech)
;~ $YStrech = $iY2
;~ If $StrechImageVertically Then $YStrech = $GuiSizeY
;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage2, 0, 0, $iX2, $iY2, $iX1, 0, $iX2, $YStrech)


; VCenter & Vertical Strech both images
$VPos = 0
$YStrech = $iY1
If $StrechImageVertically Then $YStrech = $GuiSizeY
If $CenterImageVertically and Not $StrechImageVertically Then $VPos = ($GuiSizeY - $iY1)/2
_GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage1, 0, 0, $iX1, $iY1,    0, $VPos, $iX1, $YStrech)

$VPos = 0
$YStrech = $iY2
If $StrechImageVertically Then $YStrech = $GuiSizeY
If $CenterImageVertically and Not $StrechImageVertically Then $VPos = ($GuiSizeY - $iY2)/2
_GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage2, 0, 0, $iX2, $iY2, $iX1, $VPos, $iX2, $YStrech)
; -----------------------------------8<-----------------------------------



; -----------------------------------8<-----------------------------------
; Save composite image
Local $sNewName = @ScriptDir & "\" & $ImageCompositeName
_GDIPlus_ImageSaveToFile($hBMPBuff, $sNewName)                                          ; $hBMPBuff the bitmap



; -----------------------------------8<-----------------------------------
; Show drawing window ?
If $ShowMergeGUIWindow Then
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)   ; Draw bitmap, $hBMPBuff, to the GUI's graphics, $hGraphicGUI.
    Sleep($SleepTimeShowMergeGUIWindow)                         ; Wait $SleepTimeShowMergeGUIWindow ms, or press Esc.
EndIf



; -----------------------------------8<-----------------------------------
; Show result saved file ?
If $ShowFinaleMergedImage Then ShellExecute($sNewName)



_Quit()
; -----------------------------------8<-----------------------------------





; -----------------------------------8<-----------------------------------
Func GetFromIni($SectionToken,$Token,$DefaultToken)
    $DummyToken = "#$%svd$%^@#$RVFB%QW!@#&*)NVM>?"
    $Token = IniRead($IniFile,$SectionToken,$Token,$DummyToken)
    If StringUpper($Token)  = "TRUE"        Then Return True
    If StringUpper($Token)  = "FALSE"       Then Return False
    If $Token               = $DummyToken   Then Return $DefaultToken
    Return $Token
EndFunc

; -----------------------------------8<-----------------------------------
;Func to auto-redraw on Windows internal PAINT messages.
Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    ;_WinAPI_RedrawWindow($hGui, "", "", BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME)) ; , $RDW_ALLCHILDREN
    Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_PAINT

; -----------------------------------8<-----------------------------------
Func _Quit()
    ;_GDIPlus_BrushDispose($hBrush)
;~     _GDIPlus_PenDispose($hPen)
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
;~     _GDIPlus_ImageDispose($hImage3)
;~     _GDIPlus_ImageDispose($hImage4)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hGraphicGUI)
    _WinAPI_DeleteObject($hBMPBuff)
    _GDIPlus_Shutdown()
    Exit
EndFunc   ;==>_Quit

thx for "inspiration" :unsure:

Mjr.

Link to comment
Share on other sites

  • 1 year later...

A long time afterwards, thanks a lot for your script!

I needed the same functions, so I made some improvments to your tool:

- some CommandLine arguments added

- Horizontally merge available

- One file can be self-merged

- Fixed the case one file was missing, _GDIPlus_ImageGetWidth($h) reported 4294967295 (-1), now manually set to 0

- Deprecation of attached IniFile, replaced by CommandLine arguments

To be done :

- Better "help" CommandLine message (obtained by "?", "/?", "-?", "--?", "h", "/h", "-h", "--h", "help", "/help", "-help", or "--help" parameter)

- Transformations on the merged files (mirroring, inverting colours)

- (English translation of some line comments ^^)

[Removed Code block, too long] ~ http://pastebin.com/NbtbJvfK

Thanks a lot for all !

Edited by Denver
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...