Sign in to follow this  
Followers 0
qwert

[Solved] Looking for method to append images

5 posts in this topic

#1 ·  Posted (edited)

I have a set of 5 images (PNGs) that I need to combine, vertically, into one PNG.  The result needs to be a "stacked" format, with the images left-aligned.  Although there are always 5, the dimensions of each image can differ.  (see example)

I recall a post with some method to combine two images, but it was quite a while ago and I've been unable to find any reference.

I'll certainly appreciate any advice on how to go about this.

Thanks in advance.

Five PNGs.png

 

Edited by qwert

Share this post


Link to post
Share on other sites

That should be an easy task.

 

Pseudo code:

For each Image

iWidth = Max(ImageDimension)

iHeight += ImageHeight(i)

Next

NewBitmap (iWidth, iHeight)

iHeight = 0

For each Image

copyImage(NewBitmap, 0, iHeight)

iHeight += ImageHeight(i)

Next

SaveImage(NewBitmap)

Give it a try. ;)


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thanks for your response.

The "flowchart" of steps that you describe is clear.  But what is the right "foundation" of the AU3 operations? ... hBitMaps?  For example, are WinAPI_CopyImage and WinAPI_SaveHBITMAPToFile the kind of statements to be using?  Or is GDI+ more appropriate (_GDIPlus_ImageSaveToFile)?

Maybe a better way to ask is: can you suggest an example script that basically works with an image in the way you're suggesting?  (i.e., "load image ... work on image ... save image")

Share this post


Link to post
Share on other sites
#include <Array.au3>
#include <File.au3>
#include <GDIPlus.au3>

Global Const $sFolder = FileSelectFolder("Select a folder with images", "")
If @error Then Exit MsgBox($MB_ICONERROR, "Error", "No folder was selected!", 10)
Global $aFilter = _FileListToArrayRec($sFolder, "*.jpg;*.png;*.bmp", $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH)
If @error Then Exit MsgBox($MB_ICONERROR, "Error", "No images were found in selected folder!", 10)

_GDIPlus_Startup()

Global $hBitmap_Stacked = _GDIPlus_BitmapCreateStackedBitmaps($aFilter)
_GDIPlus_ImageSaveToFile($hBitmap_Stacked, @ScriptDir & "\StackedBitmaps.png")
_GDIPlus_ImageDispose($hBitmap_Stacked)
_GDIPlus_Shutdown()


Func _GDIPlus_BitmapCreateStackedBitmaps($aFiles, $iLimit = 5)
    If $aFiles[0] = 1 Then Return SetError(1, 0, 0) ;only one image
    $iLimit = $iLimit > $aFiles[0] ? $aFiles[0] : $iLimit
    Local $i, $aDim, $iW_max = 0, $iH_max = 0, $aImages[$iLimit + 1], $iY = 0
    $aImages[0] = $iLimit
    For $i = 1 To $iLimit
        $aImages[$i] = _GDIPlus_ImageLoadFromFile($aFiles[$i])
        $aDim = _GDIPlus_ImageGetDimension($aImages[$i])
        $iW_max = $aDim[0] > $iW_max ? $aDim[0] : $iW_max
        $iH_max += $aDim[1]
    Next
    Local Const $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW_max, $iH_max), $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    For $i = 1 To $iLimit
        $aDim = _GDIPlus_ImageGetDimension($aImages[$i])
        _GDIPlus_GraphicsDrawImageRect($hGfx, $aImages[$i], 0, $iY, $aDim[0], $aDim[1])
        _GDIPlus_ImageDispose($aImages[$i])
        $iY += $aDim[1]
    Next
    _GDIPlus_GraphicsDispose($hGfx)
    Return $hBitmap
EndFunc

 


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Wow.  33 statements!  And fast.  (It produced a 3Mb result instantly.)

This belongs in the Examples topic.  I can foresee a variety of uses ... and versions that could do horizontal ... and mosaics, with automatic image sizing.

Plus, it's an excellent example of proper GDI+ implementation with ImageDispose, GraphicsDispose, etc.  We see these things in scripts, of course, but rarely in such a concise context.

Thank you for this.

Share this post


Link to post
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
Sign in to follow this  
Followers 0