Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   AutoIt (http://forum.oszone.net/forumdisplay.php?f=103)
-   -   [решено] Наложение водяного знака на изображение (http://forum.oszone.net/showthread.php?t=201671)

centaurvv 10-03-2011 22:43 1631918

Наложение водяного знака на изображение
 
Есть пачка изображений в формате *.jpg.

Задача состоит в том, чтобы наложить на каждое изображение дату его создания (т.е. в данном случае текст, не картинку).

Как можно это реализовать средствами Autoit без использования сторонних программ?

Если с jpg проблемно, пусть это будут другие форматы... png, bmp и тп.

Creat0R 11-03-2011 00:45 1631990

Код:

#include <File.au3>
#include <GDIPlus.au3>

$iMake_Backup = 0
$sImages_Path = @ScriptDir & "\Images"

$aFiles = _FileListToArray($sImages_Path, "*.jpg", 1)

For $i = 1 To UBound($aFiles)-1
    $sImage = $sImages_Path & "\" & $aFiles[$i]
    $sFileTime = StringRegExpReplace(FileGetTime($sImage, 1, 1), "(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})", "$3/$2/$1, $4:$5:$6")

    If $iMake_Backup Then
        FileCopy($sImage, $sImage & ".bak", 1)
    EndIf

    _ImageSetWaterMark($sImage, -1, $sFileTime, 0xFF0000, 16, -1, -1, 4)
Next

Func _ImageSetWaterMark($sSrcImage, $sDstImage, $sWaterMark, $iColor=0xFF0000, $iFontSize=16, $iLeft=0, $iTop=0, $iFormat=4)
    ; Initialize GDI+ library
    _GDIPlus_StartUp()

    Local $hImage    = _GDIPlus_ImageLoadFromFile($sSrcImage)

    Local $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
    Local $hFamily    = _GDIPlus_FontFamilyCreate("Arial")
    Local $hFont    = _GDIPlus_FontCreate($hFamily, $iFontSize, 1)
    Local $tLayout  = _GDIPlus_RectFCreate(0, 0)
    Local $hFormat  = _GDIPlus_StringFormatCreate($iFormat)
    Local $hBrush1  = _GDIPlus_BrushCreateSolid(0xA2FFFFFF)
    Local $hBrush2  = _GDIPlus_BrushCreateSolid("0xC4" & Hex(Number($iColor), 6))
    Local $hPen    = _GDIPlus_PenCreate(0xC4000000, 2)
    Local $aInfo    = _GDIPlus_GraphicsMeasureString($hGraphic, $sWaterMark, $hFont, $tLayout, $hFormat)

    Local $iWidth  = DllStructGetData($aInfo[0], "Width")
    Local $iHeight  = DllStructGetData($aInfo[0], "Height")

    If $iLeft = -1 Then $iLeft = _GDIPlus_ImageGetWidth($hImage) - $iWidth
    If $iTop = -1 Then $iTop = _GDIPlus_ImageGetHeight($hImage) - $iHeight

    If $iLeft = 0 Then $iLeft = 1
    If $iTop = 0 Then $iTop = 1

    DllStructSetData($aInfo[0], "X", $iLeft)
    DllStructSetData($aInfo[0], "Y", $iTop)

    _GDIPlus_GraphicsFillRect($hGraphic, $iLeft, $iTop, $iWidth, $iHeight, $hBrush1)
    _GDIPlus_GraphicsDrawRect($hGraphic, $iLeft, $iTop, $iWidth, $iHeight, $hPen)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $sWaterMark, $hFont, $aInfo[0], $hFormat, $hBrush2)

    ; Save image
    Local $sTmp_ImageFile = StringRegExpReplace($sSrcImage, "(.*)(\.[^\.]*$)", "\1_tmp\2")
    _GDIPlus_ImageSaveToFile($hImage, $sTmp_ImageFile)

    ; Free resources
    _GDIPlus_PenDispose        ($hPen    )
    _GDIPlus_BrushDispose      ($hBrush1 )
    _GDIPlus_BrushDispose      ($hBrush2 )
    _GDIPlus_StringFormatDispose($hFormat )
    _GDIPlus_FontDispose        ($hFont  )
    _GDIPlus_FontFamilyDispose  ($hFamily )
    _GDIPlus_GraphicsDispose    ($hGraphic)
    _GDIPlus_ImageDispose      ($hImage  )

    _GDIPlus_ShutDown()

    If $sDstImage <> $sSrcImage And FileExists($sDstImage) Then
        FileMove($sTmp_ImageFile, $sDstImage, 1)
    ElseIf $sDstImage = -1 Or $sDstImage = $sSrcImage Or Not FileExists($sDstImage) Then
        FileMove($sTmp_ImageFile, $sSrcImage, 1)
    EndIf

    FileDelete($sTmp_ImageFile)
EndFunc


centaurvv 11-03-2011 02:06 1632024

Цитата:

Цитата Creat0R
“Сделай так просто, как возможно, но не проще этого.”

Браво, Маэстро!
В очередной раз преклоняюсь пред Вашим мастерством!


Время: 13:38.

Время: 13:38.
© OSzone.net 2001-