Jump to content

Contact Sheet with AutoIt - Like Photoshop?


Go to solution Solved by Werty,

Recommended Posts

Is there a way to create a contact sheet of  pictures from a folder?  For example, i have a folder of 12 pictures.   I would like to place 4 pictures on one page until i reach 12 pictures total.  In this case there would be three separate jpgs.  Is this possible?

Thanks!

 

Link to comment
Share on other sites

  • Solution

Very easy using ImageMagick, takes as little as....

$img = ObjCreate("ImageMagickObject.MagickImage.1")
$img.Montage(@ScriptDir & "\Temp\*.bmp","montage.bmp")

...that takes all bmp images in scriptdirtemp and makes a montage of it.

You can change size, crop, how many images you want in each row/column and much more.

Read up on it here...

http://www.imagemagick.org/Usage/montage/

Some guy's script + some other guy's script = my script!

Link to comment
Share on other sites

So, not to re-open my own post, but I had some questions about mult-page montages.   I can call montage command with no problem, however I have a folder of about 1000 images.  Is there a AutoIt script that can go through and take 6 files, make a montage image called montage1.jpg and then take the next 6 and name it montage 2.jpg and so on?

Thanks!
-Tony

Link to comment
Share on other sites

How are your files named ? are they named 1.jpg. 2.jpg. 3.jpg. etc. or do they have actual names or weird names like hh34ggdshg33.jpg ?

Maybe look into FileFindFirstFile and FileFindNextFile in the helpfile, stuff them in an array, and make some loop that does it.

Some guy's script + some other guy's script = my script!

Link to comment
Share on other sites

Try this (GDI+ version):

;coded by UEZ build 2014-06-13

#cs
    A4 paper size
    72 dpi (web)    =  595 X  842 pixels
    200 dpi (print) = 1654 X 2339 pixels
    300 dpi (print) = 2480 X 3508 pixels
    600 dpi (print) = 4960 X 7016 pixels

    Letter
    72 dpi (web)    =  612 X  792 pixels
    200 dpi (print) = 1700 X 2200 pixels
    300 dpi (print) = 2550 X 3300 pixels
    600 dpi (print) = 5100 X 6600 pixels

#ce
AutoItSetOption("MustDeclareVars", 1)
#include <GDIPlus.au3>
#include <File.au3>

Global $bRec = 1
Global $sFolder = FileSelectFolder("Select a folder with GDI+ supported images", @ScriptDir, 0, "")
If @error Then Exit MsgBox(16, "Error", "No folder has been selected - exiting script!", 30)
Global $t = TimerInit()
Global $aFiles = _FileListToArrayRec($sFolder, "*.jpg;*.png;*.gif;*.bmp", $FLTAR_FILES, $bRec, $FLTAR_SORT, $FLTAR_FULLPATH)
Global $te = TimerDiff($t)
If UBound($aFiles) = 1 Then Exit MsgBox(16, "Error", "No images found in dir " & $sFolder & " - exiting script!", 30)
Global $sDestFolder = FileSelectFolder("Select destination folder where the contact sheets will be saved to", @ScriptDir, 0, "")
If @error Then Exit MsgBox(16, "Error", "No destination folder has been selected - exiting script!", 30)
If Not FileExists($sDestFolder) Then DirCreate($sDestFolder)
If StringRight($sFolder, 1) = "\" Then $sDestFolder = StringTrimRight($sDestFolder, 1)


ConsoleWrite($aFiles[0] & " image files detected in " & Round($te, 2) & " milli seconds. Processing..." & @CRLF)
_GDIPlus_Startup()
Global $iPieces = 8, $iExtended = 0, $iRet
$t = TimerInit()
$iRet = _GDIPlus_BitmapCreateContactSheets($aFiles, $sDestFolder, $iPieces)
$iExtended = @extended
If $iExtended Then ConsoleWrite($iRet & @CRLF)
$te = TimerDiff($t)
ConsoleWrite("Processed " & $aFiles[0] & " image files in " & Round($te / 1000, 2) & " seconds, added " & $aFiles[0] - $iExtended & " images and created " & Ceiling(($aFiles[0] - $iExtended) / $iPieces ^ 2) & " contact sheets!" & @CRLF)
_GDIPlus_Shutdown()

Func _GDIPlus_BitmapCreateContactSheets($aFiles, $sDestFolder, $iPieces = 4, $iWidth = 842, $iHeight = 595)
    If Not IsArray($aFiles) Then Return SetError(1, 0, 0)
    If UBound($aFiles) = 1 Then Return SetError(2, 0, 0)
    Local $iW = Floor($iWidth / $iPieces), $iH = Floor($iHeight / $iPieces), $iPages = Ceiling($aFiles[0] / $iPieces ^ 2), $iX = 0, $iY = 0, $i, $j = 1, $hBmp_tmp, $iErrors = 0, $sErrorFiles
    Local Const $hBitmap_CS = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local Const $hGfx_CS = _GDIPlus_ImageGetGraphicsContext($hBitmap_CS)
    For $i = 1 To $iPages
        _GDIPlus_GraphicsClear($hGfx_CS)
        While $iY < $iPieces
            While $iX < $iPieces
                If $j > $aFiles[0] Then ExitLoop 2
                $hBmp_tmp = _GDIPlus_BitmapFitCenteredToSize($aFiles[$j], $iW, $iH)
                If @error Then
                    $iErrors += 1
                    $sErrorFiles &= $aFiles[$j] & ";"
                    $j += 1
                    ContinueLoop 2
                Else
                    _GDIPlus_GraphicsDrawImageRect($hGfx_CS, $hBmp_tmp, $iW * $iX, $iH * $iY, $iW, $iH)
                    _GDIPlus_BitmapDispose($hBmp_tmp)
                    $iX += 1
                EndIf
                $j += 1
            WEnd
            $iX = 0
            $iY += 1
        WEnd
        $iY = 0
        _GDIPlus_ImageSaveToFile($hBitmap_CS, $sDestFolder & "\ContactSheets" & StringFormat("%0" & StringLen($iPages) & "i", $i) & ".png")
        If $j > $aFiles[0] Then ExitLoop
    Next
    _GDIPlus_GraphicsDispose($hGfx_CS)
    _GDIPlus_BitmapDispose($hBitmap_CS)
    If $iErrors Then Return SetError(1, $iErrors, "Skipped files (" & $iErrors & "): " & $sErrorFiles)
    Return 1
EndFunc   ;==>_GDIPlus_BitmapCreateContactSheets

Func _GDIPlus_BitmapFitCenteredToSize($sFile, $iW, $iH, $bDisplayFilename = True, $sFontname = "Verdana", $bFullpath= False, $bUpscale = False)
    Local Const $hBitmap = _GDIPlus_BitmapCreateFromFile($sFile)
    If @error Then Return SetError(10, 0, 0)
    Local Const $iWidth = _GDIPlus_ImageGetWidth($hBitmap), $iHeight = _GDIPlus_ImageGetHeight($hBitmap)
    Local $f, $iW_new, $iH_new
    If $iW >= $iH Then
        If $iWidth > $iHeight Then
            $f = $iW / $iWidth
            If Int($iHeight * $f) > $iH Then $f = $iH / $iHeight
        Else
            $f = $iH / $iHeight
            If Int($iWidth * $f) > $iW Then $f = $iW / $iWidth
        EndIf
    Else
        If $iWidth >= $iHeight Then
            $f = $iW / $iWidth
            If Int($iHeight * $f) > $iH Then $f = $iH / $iHeight
        Else
            $f = $iH / $iHeight
            If Int($iWidth * $f) > $iW Then $f = $iW / $iWidth
        EndIf
    EndIf
    $f = (Not $bUpscale And ($iWidth < $iW) And ($iHeight < $iH)) ? 1 : $f
    $iW_new = Int($iWidth * $f)
    $iH_new = Int($iHeight * $f)
    Local Const $hBitmap_new = _GDIPlus_BitmapCreateFromScan0($iW, $iH), $hGfx = _GDIPlus_ImageGetGraphicsContext($hBitmap_new)
    _GDIPlus_GraphicsSetInterpolationMode($hGfx, 7)
    _GDIPlus_GraphicsDrawImageRect($hGfx, $hBitmap, ($iW - $iW_new) / 2, ($iH - $iH_new) / 2, $iW_new, $iH_new)
    If $bDisplayFilename Then
        Local $iSize = Int(($iW + $iH) / 40)
        $iSize = ($iSize < 8) ? 8 : ($iSize > 12) ? 12 : $iSize
        _GDIPlus_GraphicsSetTextRenderingHint($hGfx, 4)
        _GDIPlus_GraphicsSetSmoothingMode($hGfx, 2)
        Local Const $hFamily = _GDIPlus_FontFamilyCreate($sFontname)
        Local Const $hFormat = _GDIPlus_StringFormatCreate()
        Local Const $tLayout = _GDIPlus_RectFCreate(0, 0, $iW, $iH)
        _GDIPlus_StringFormatSetLineAlign($hFormat, 2)
        Local Const $hPath = _GDIPlus_PathCreate()
        Local $sPath = (Not $bFullpath) ? StringRegExpReplace($sFile, ".+\\(.+)", "$1") : $sFile
        _GDIPlus_PathAddString($hPath, $sPath, $tLayout, $hFamily, 0, $iSize, $hFormat)
        Local Const $hPen = _GDIPlus_PenCreate(0xFF000000, 1 + Int($iSize / 11))
        Local Const $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
        _GDIPlus_GraphicsDrawPath($hGfx, $hPath, $hPen)
        _GDIPlus_GraphicsFillPath($hGfx, $hPath, $hBrush)
        _GDIPlus_PathDispose($hPath)
        _GDIPlus_PenDispose($hPen)
        _GDIPlus_BrushDispose($hBrush)
        _GDIPlus_FontFamilyDispose($hFamily)
        _GDIPlus_StringFormatDispose($hFormat)
    EndIf
    _GDIPlus_GraphicsDispose($hGfx)
    Return $hBitmap_new
EndFunc   ;==>_GDIPlus_BitmapFitCenteredToSize

Benchmark: Processed 997 image files in 69.28 seconds, added 993 images and created 16 contact sheets!

Please report any bugs!

Br,

UEZ

Edit1: fixed memory leak

Edit2: fixed a bug in _GDIPlus_BitmapFitCenteredToSize function (calculation) and added text (filename)

Edited by UEZ

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

ok, I upgraded my AutoIt and I no longer get the error.  However after I select the source folder and then the destination folder, nothing seems to happen.  The interface dismisses with no message saying that files are being processed.  

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

×
×
  • Create New...