Войти

Показать полную графическую версию : [решено] Наложение водяного знака на изображение


centaurvv
10-03-2011, 22:43
Есть пачка изображений в формате *.jpg.

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

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

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

Creat0R
11-03-2011, 00:45
#include (http://www.autoitscript.com/autoit3/docs/keywords.htm##include) <File.au3>
#include (http://www.autoitscript.com/autoit3/docs/keywords.htm##include) <GDIPlus.au3>

$iMake_Backup = 0
$sImages_Path = @ScriptDir (http://www.autoitscript.com/autoit3/docs/macros.htm#@scriptdir) & "\Images"

$aFiles = _FileListToArray (http://dundats.mvps.org/help/html/libfunctions/_filelisttoarray.htm)($sImages_Path, "*.jpg", 1)

For (http://www.autoitscript.com/autoit3/docs/keywords.htm#For) $i = 1 To (http://www.autoitscript.com/autoit3/docs/keywords.htm#To) UBound (http://www.autoitscript.com/autoit3/docs/functions/UBound.htm)($aFiles)-1
$sImage = $sImages_Path & "\" & $aFiles[$i]
$sFileTime = StringRegExpReplace (http://www.autoitscript.com/autoit3/docs/functions/StringRegExpReplace.htm)(FileGetTime (http://www.autoitscript.com/autoit3/docs/functions/FileGetTime.htm)($sImage, 1, 1), "(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})", "$3/$2/$1, $4:$5:$6")

If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $iMake_Backup Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
FileCopy (http://www.autoitscript.com/autoit3/docs/functions/FileCopy.htm)($sImage, $sImage & ".bak", 1)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)

_ImageSetWaterMark($sImage, -1, $sFileTime, 0xFF0000, 16, -1, -1, 4)
Next (http://www.autoitscript.com/autoit3/docs/keywords.htm#Next)

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _ImageSetWaterMark($sSrcImage, $sDstImage, $sWaterMark, $iColor=0xFF0000, $iFontSize=16, $iLeft=0, $iTop=0, $iFormat=4)
; Initialize GDI+ library
_GDIPlus_StartUp (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_startup.htm)()

Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $hImage = _GDIPlus_ImageLoadFromFile (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_imageloadfromfile.htm)($sSrcImage)

Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $hGraphic = _GDIPlus_ImageGetGraphicsContext (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_imagegetgraphicscontext.htm)($hImage)
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $hFamily = _GDIPlus_FontFamilyCreate (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_fontfamilycreate.htm)("Arial")
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $hFont = _GDIPlus_FontCreate (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_fontcreate.htm)($hFamily, $iFontSize, 1)
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $tLayout = _GDIPlus_RectFCreate (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_rectfcreate.htm)(0, 0)
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $hFormat = _GDIPlus_StringFormatCreate (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_stringformatcreate.htm)($iFormat)
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $hBrush1 = _GDIPlus_BrushCreateSolid (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_brushcreatesolid.htm)(0xA2FFFFFF)
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $hBrush2 = _GDIPlus_BrushCreateSolid (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_brushcreatesolid.htm)("0xC4" & Hex (http://www.autoitscript.com/autoit3/docs/functions/Hex.htm)(Number (http://www.autoitscript.com/autoit3/docs/functions/Number.htm)($iColor), 6))
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $hPen = _GDIPlus_PenCreate (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_pencreate.htm)(0xC4000000, 2)
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $aInfo = _GDIPlus_GraphicsMeasureString (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_graphicsmeasurestring.htm)($hGraphic, $sWaterMark, $hFont, $tLayout, $hFormat)

Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $iWidth = DllStructGetData (http://www.autoitscript.com/autoit3/docs/functions/DllStructGetData.htm)($aInfo[0], "Width")
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $iHeight = DllStructGetData (http://www.autoitscript.com/autoit3/docs/functions/DllStructGetData.htm)($aInfo[0], "Height")

If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $iLeft = -1 Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then) $iLeft = _GDIPlus_ImageGetWidth (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_imagegetwidth.htm)($hImage) - $iWidth
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $iTop = -1 Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then) $iTop = _GDIPlus_ImageGetHeight (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_imagegetheight.htm)($hImage) - $iHeight

If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $iLeft = 0 Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then) $iLeft = 1
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $iTop = 0 Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then) $iTop = 1

DllStructSetData (http://www.autoitscript.com/autoit3/docs/functions/DllStructSetData.htm)($aInfo[0], "X", $iLeft)
DllStructSetData (http://www.autoitscript.com/autoit3/docs/functions/DllStructSetData.htm)($aInfo[0], "Y", $iTop)

_GDIPlus_GraphicsFillRect (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_graphicsfillrect.htm)($hGraphic, $iLeft, $iTop, $iWidth, $iHeight, $hBrush1)
_GDIPlus_GraphicsDrawRect (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_graphicsdrawrect.htm)($hGraphic, $iLeft, $iTop, $iWidth, $iHeight, $hPen)
_GDIPlus_GraphicsDrawStringEx (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_graphicsdrawstringex.htm)($hGraphic, $sWaterMark, $hFont, $aInfo[0], $hFormat, $hBrush2)

; Save image
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $sTmp_ImageFile = StringRegExpReplace (http://www.autoitscript.com/autoit3/docs/functions/StringRegExpReplace.htm)($sSrcImage, "(.*)(\.[^\.]*$)", "\1_tmp\2")
_GDIPlus_ImageSaveToFile (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_imagesavetofile.htm)($hImage, $sTmp_ImageFile)

; Free resources
_GDIPlus_PenDispose (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_pendispose.htm) ($hPen )
_GDIPlus_BrushDispose (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_brushdispose.htm) ($hBrush1 )
_GDIPlus_BrushDispose (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_brushdispose.htm) ($hBrush2 )
_GDIPlus_StringFormatDispose (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_stringformatdispose.htm)($hFormat )
_GDIPlus_FontDispose (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_fontdispose.htm) ($hFont )
_GDIPlus_FontFamilyDispose (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_fontfamilydispose.htm) ($hFamily )
_GDIPlus_GraphicsDispose (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_graphicsdispose.htm) ($hGraphic)
_GDIPlus_ImageDispose (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_imagedispose.htm) ($hImage )

_GDIPlus_ShutDown (http://dundats.mvps.org/help/html/libfunctions/_gdiplus_shutdown.htm)()

If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $sDstImage <> $sSrcImage And (http://www.autoitscript.com/autoit3/docs/keywords.htm#And) FileExists (http://www.autoitscript.com/autoit3/docs/functions/FileExists.htm)($sDstImage) Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
FileMove (http://www.autoitscript.com/autoit3/docs/functions/FileMove.htm)($sTmp_ImageFile, $sDstImage, 1)
ElseIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#ElseIf) $sDstImage = -1 Or (http://www.autoitscript.com/autoit3/docs/keywords.htm#Or) $sDstImage = $sSrcImage Or (http://www.autoitscript.com/autoit3/docs/keywords.htm#Or) Not (http://www.autoitscript.com/autoit3/docs/keywords.htm#Not) FileExists (http://www.autoitscript.com/autoit3/docs/functions/FileExists.htm)($sDstImage) Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
FileMove (http://www.autoitscript.com/autoit3/docs/functions/FileMove.htm)($sTmp_ImageFile, $sSrcImage, 1)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)

FileDelete (http://www.autoitscript.com/autoit3/docs/functions/FileDelete.htm)($sTmp_ImageFile)
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc)

centaurvv
11-03-2011, 02:06
“Сделай так просто, как возможно, но не проще этого.”

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




© OSzone.net 2001-2012