Jump to content

Image Editor (ImageMagick)


Jex
 Share

Recommended Posts

Posted Image

Need ImageMagick dll for use that script : http://www.imagemagick.org/download/binari...windows-dll.exe

#include <GUIConstants.au3>
#include <GDIPlus.au3>
#include <Misc.au3>
#Include <GuiMenu.au3>

Global $Picture, $Recentcount = 1, $Recent[2], $Edit = 0, $Place, $ImageWidth, $ImageHeight, $Resize = 0, $Sharpen = 0
Global $Output = @WindowsDir & "\Autoit.jpg"

FileInstall("ImageMagickObject.dll", @SystemDir & "\ImageMagickObject.dll", 0)
RunWait(@ComSpec & " /c regsvr32 /s ImageMagickObject.dll", @SystemDir, @SW_HIDE)

$GUI = GUICreate("Image Editor", 300, 300, 300, 200, $WS_OVERLAPPEDWINDOW)
$FileMenu = GUICtrlCreateMenu("File")
$FileMenuOpen = GUICtrlCreateMenuItem("Open...", $FileMenu)
$FileMenuSave = GUICtrlCreateMenuItem("Save", $FileMenu)
GUICtrlSetState(-1, $GUI_DISABLE)
$FileMenuSaveAs = GUICtrlCreateMenuItem("Save As...", $FileMenu)
GUICtrlSetState(-1, $GUI_DISABLE)
$FileMenuRecentFiles = GUICtrlCreateMenu("Recent Files", $FileMenu)
GUICtrlSetState(-1, $GUI_DISABLE)
$FileMenuSeparator1 = GUICtrlCreateMenuItem("", $FileMenu)
$FileMenuRenameFile = GUICtrlCreateMenuItem("Rename File...", $FileMenu)
GUICtrlSetState(-1, $GUI_DISABLE)
$FileMenuDeleteFile = GUICtrlCreateMenuItem("Delete File...", $FileMenu)
GUICtrlSetState(-1, $GUI_DISABLE)
$FileMenuSeparator2 = GUICtrlCreateMenuItem("", $FileMenu)
$FileMenuExit = GUICtrlCreateMenuItem("Exit", $FileMenu)
$EditMenu = GUICtrlCreateMenu("Edit")
GUICtrlSetState(-1, $GUI_DISABLE)
$EditMenuBlur = GUICtrlCreateMenuItem("Blur...", $EditMenu)
$EditMenuCrop = GUICtrlCreateMenuItem("Crop Selection...", $EditMenu)
$EditMenuResizeImage = GUICtrlCreateMenuItem("Resize Image...", $EditMenu)
$EditMenuSharpen = GUICtrlCreateMenuItem("Sharpen...", $EditMenu)

_GDIPlus_Startup()
$oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$oImg = ObjCreate("ImageMagickObject.MagickImage.1")
GUISetState()
Global $Client = _WinGetClientPos(1)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $FileMenuOpen
            $Image = FileOpenDialog("Choose file...", @DesktopCommonDir, "JPG (*.jpg)")
            If Not @error Then
                For $i = 1 To $Recentcount
                    If _GUICtrlMenu_GetItemText(GUICtrlGetHandle($FileMenuRecentFiles), $i - 1) = $Image Then
                        ExitLoop
                    EndIf
                    If $i = $Recentcount Then
                        $Recent[$Recentcount] = GUICtrlCreateMenuItem($Image, $FileMenuRecentFiles)
                        $Recentcount += 1
                        ReDim $Recent[$Recentcount + 1]
                    EndIf
                Next
                MenuEnable()
                Preview($Image)
            EndIf
        Case $FileMenuSave
            If FileExists(@WindowsDir & "\Autoit.jpg") Then FileCopy(@WindowsDir & "\Autoit.jpg", $Place, 9)
        Case $FileMenuSaveAs
            If FileExists(@WindowsDir & "\Autoit.jpg") Then
                $Save = FileSaveDialog("Choose place for save...", @DesktopCommonDir, "JPG - JPEG Files (*.jpg)")
                If Not StringInStr($Save, ".jpg") Then $Save &= ".jpg"
                FileCopy(@WindowsDir & "\Autoit.jpg", $Save, 9)
                $Place = $Save
            EndIf
        Case $FileMenuRenameFile
            $SplitPlace = StringSplit($Place, "\", 1)
            $Input = InputBox("Rename File", "New name :", $SplitPlace[UBound($SplitPlace) - 1], "", 200, 130)
            If Not @error And $Input <> "" Then
                $TrimPlace = StringTrimRight($Place, StringLen($SplitPlace[UBound($SplitPlace) - 1]))
                MsgBox("", "", $TrimPlace & $Input)
                FileMove($Place, $TrimPlace & $Input, 1)
            EndIf
        Case $FileMenuDeleteFile
            $Delete = MsgBox(36, "Delete File", "Are you really want delete ?")
            If $Delete = 6 Then FileDelete($Place)
        Case $FileMenuExit
            Exit
        Case $EditMenuBlur
            SharpenGUI("Blur")
        Case $EditMenuCrop
            $Reg = Region()
            $Position = GetClientPos($GUI)
            Crop($Image, $Output, $Reg[0] - $Position[0], $Reg[1] - $Position[1], $Reg[4], $Reg[5])
            Preview($Output, 1)
        Case $EditMenuResizeImage
            ResizeGUI()
        Case $EditMenuSharpen
            SharpenGUI("Sharpen")
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            For $i = 1 To $Recentcount - 1
                If $Recent[$i] = $msg Then
                    $Image = _GUICtrlMenu_GetItemText(GUICtrlGetHandle($FileMenuRecentFiles), $i - 1)
                    Preview($Image)
                EndIf
            Next
    EndSwitch
WEnd

Func Preview($ImageName, $Edit = 0)
    Local $hImage = _GDIPlus_ImageLoadFromFile($ImageName)
    Global $ImageWidth = _GDIPlus_ImageGetWidth($hImage)
    Global $ImageHeight = _GDIPlus_ImageGetHeight($hImage)
    _GDIPlus_ImageDispose($hImage)
    If $Picture <> "" Then GUICtrlDelete($Picture)
    $Picture = GUICtrlCreatePic($ImageName, 0, 0, $ImageWidth, $ImageHeight)
    WinMove($GUI, "", Default, Default, $ImageWidth, $ImageHeight)
    Local $ClientSize = WinGetClientSize($GUI)
    WinMove($GUI, "", (@DesktopWidth / 2) - ($ImageWidth / 2), (@DesktopHeight / 2) - ($ImageHeight / 2), $ImageWidth + ($ImageWidth - $ClientSize[0]), $ImageHeight + ($ImageHeight - $ClientSize[1]))
    Global $Image = $ImageName
    If Not $Edit Then
        Global $Place = $Image
        FileCopy($Place, @WindowsDir & "\Autoit.jpg", 9)
    EndIf
EndFunc   ;==>Preview

Func ResizeGUI()
    $ResizeForm = GUICreate("Resize Image", 210, 110, -1, -1)
    $ResizeLabel1 = GUICtrlCreateLabel("Current size :   " & $ImageWidth & " x " & $ImageHeight, 16, 16, 137, 17)
    $ResizeInput1 = GUICtrlCreateInput("", 56, 40, 41, 21)
    $ResizeLabel2 = GUICtrlCreateLabel("Width :", 16, 48, 38, 17)
    $ResizeLabel3 = GUICtrlCreateLabel("Height :", 104, 48, 41, 17)
    $ResizeInput2 = GUICtrlCreateInput("", 152, 40, 41, 21)
    $ResizeButton1 = GUICtrlCreateButton("OK", 16, 72, 83, 25, 0)
    $ResizeButton2 = GUICtrlCreateButton("Cancel", 112, 72, 83, 25, 0)
    GUISetState(@SW_SHOW, $ResizeForm)
    While 1
        $msg = GUIGetMsg(1)
        If $msg[0] = $ResizeButton1 Then
            $NewWidth = GUICtrlRead($ResizeInput1)
            $NewHeight = GUICtrlRead($ResizeInput2)
            $Resize = 1
            ExitLoop
        ElseIf $msg[0] = $ResizeButton2 Or $msg[0] = $GUI_EVENT_CLOSE And $msg[1] = $ResizeForm Then
            ExitLoop
        ElseIf $msg[0] = $GUI_EVENT_CLOSE And $msg[1] = $GUI Then
            Exit
        EndIf
    WEnd
    GUIDelete($ResizeForm)
    If $Resize = 1 Then
        Resize($Image, $Output, $NewWidth, $NewHeight)
        Preview($Output, 1)
        $Resize = 0
    EndIf
EndFunc   ;==>ResizeGUI

Func SharpenGUI($Name)
    $SharpenForm = GUICreate($Name, 210, 110, -1, -1)
    $SharpenButton1 = GUICtrlCreateButton("OK", 16, 72, 83, 25, 0)
    $SharpenButton2 = GUICtrlCreateButton("Cancel", 112, 72, 83, 25, 0)
    $SharpenSlider1 = GUICtrlCreateSlider(8, 8, 190, 37)
    GUICtrlSetLimit(-1, 50, 2)
    $SharpenLabel1 = GUICtrlCreateLabel($Name & " value :   " & GUICtrlRead($SharpenSlider1) / 10, 56, 48, 103, 17)
    GUISetState(@SW_SHOW, $SharpenForm)
    While 1
        $msg = GUIGetMsg(1)
        If $msg[0] = $SharpenButton1 Then
            $SharpenValue = GUICtrlRead($SharpenSlider1) / 10
            $Sharpen = 1
            ExitLoop
        ElseIf $msg[0] = $SharpenSlider1 Then
            GUICtrlSetData($SharpenLabel1, $Name & " value :   " & GUICtrlRead($SharpenSlider1) / 10)
        ElseIf $msg[0] = $SharpenButton2 Or $msg[0] = $GUI_EVENT_CLOSE And $msg[1] = $SharpenForm Then
            ExitLoop
        ElseIf $msg[0] = $GUI_EVENT_CLOSE And $msg[1] = $GUI Then
            Exit
        EndIf
    WEnd
    GUIDelete($SharpenForm)
    If $Sharpen = 1 Then
        If $Name = "Sharpen"  Then
            Sharpen($Image, $Output, $SharpenValue)
        ElseIf $Name = "Blur"  Then
            Blur($Image, $Output, $SharpenValue)
        EndIf
        Preview($Output, 1)
        $Sharpen = 0
    EndIf
EndFunc   ;==>SharpenGUI

Func Blur($sInFile, $sOutFile, $Sigma)
    $oImg.Convert($sInFile, "-blur", "0x" & $Sigma, $sOutFile)
EndFunc   ;==>Blur

Func Crop($sInFile, $sOutFile, $sX, $sY, $sWidth, $sHeight)
    $oImg.Convert($sInFile, "-crop", $sWidth & "x" & $sHeight & "+" & $sX & "+" & $sY, "+repage", $sOutFile)
EndFunc   ;==>Crop

Func Resize($sInFile, $sOutFile, $sWidth, $sHeight)
    $oImg.Convert($sInFile, "-resize", $sWidth & "x" & $sHeight & "!", $sOutFile)
EndFunc   ;==>Resize

Func Sharpen($sInFile, $sOutFile, $Sigma)
    $oImg.Convert($sInFile, "-sharpen", "0x" & $Sigma, $sOutFile)
EndFunc   ;==>Sharpen

Func MenuEnable()
    GUICtrlSetState($FileMenuSave, $GUI_ENABLE)
    GUICtrlSetState($FileMenuSaveAs, $GUI_ENABLE)
    GUICtrlSetState($FileMenuRecentFiles, $GUI_ENABLE)
    GUICtrlSetState($FileMenuRenameFile, $GUI_ENABLE)
    GUICtrlSetState($FileMenuDeleteFile, $GUI_ENABLE)
    GUICtrlSetState($EditMenu, $GUI_ENABLE)
EndFunc   ;==>MenuEnable

Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    MsgBox(0, "COM Error Test", "We intercepted a COM Error !" & @CRLF & @CRLF & _
            "err.description is: " & @TAB & $oMyError.description & @CRLF & _
            "err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _
            "err.number is: " & @TAB & $HexNumber & @CRLF & _
            "err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _
            "err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _
            "err.source is: " & @TAB & $oMyError.source & @CRLF & _
            "err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _
            "err.helpcontext is: " & @TAB & $oMyError.helpcontext _
            )
    SetError(1)
EndFunc   ;==>MyErrFunc

Func GetClientPos($wTitle = "")
    Local $cPos[6]
    $wPos = WinGetPos($wTitle)
    $cPos[0] = $wPos[0] + $Client[0]
    $cPos[1] = $wPos[1] + $Client[1]
    $cPos[2] = ($wPos[0] + $wPos[2]) - $Client[2]
    $cPos[3] = ($wPos[1] + $wPos[3]) - $Client[3]
    $cPos[4] = $cPos[2] - $cPos[0]
    $cPos[5] = $cPos[3] - $cPos[1]
    Return $cPos
EndFunc   ;==>GetClientPos

Func _WinGetClientPos($fAbsolute = 0)
    Local $cPos = DllStructCreate("long;long;long;long"), $hWin = WinGetHandle("")
    DllCall("user32.dll", "int", "GetClientRect", "hwnd", $hWin, "ptr", DllStructGetPtr($cPos))
    Local $cPos2[4] = [DllStructGetData($cPos, 1), DllStructGetData($cPos, 2), DllStructGetData($cPos, 3), DllStructGetData($cPos, 4)]
    If $fAbsolute Then
        Local $MousePosSav = MouseGetPos()
        Local $MouseModeSav = Opt("MouseCoordMode", 2)
        MouseMove(0, 0, 0)
        Opt("MouseCoordMode", 1)
        Local $avRET = MouseGetPos()
        Opt("MouseCoordMode", $MouseModeSav)
        MouseMove($MousePosSav[0], $MousePosSav[1], 0)
        $wPos = WinGetPos("")
        $cPos2[0] = $avRET[0] - $wPos[0]
        $cPos2[1] = $avRET[1] - $wPos[1]
        $cPos2[2] = ($wPos[0] + $wPos[2]) - ($avRET[0] + $cPos2[2])
        $cPos2[3] = ($wPos[1] + $wPos[3]) - ($avRET[1] + $cPos2[3])
    EndIf
    Return $cPos2
EndFunc   ;==>_WinGetClientPos

Func Region($Tray = 0)
    While 1
        Opt("MouseCoordMode", 2)
        If _IsPressed("01") Then
            $Pos1 = MouseGetPos()
            $sClient = WinGetClientSize($GUI)
            If WinActive($GUI) And $Pos1[0] > 0 And $Pos1[1] > 0 And $Pos1[0] < $sClient[0] And $Pos1[1] < $sClient[1] Then
                Opt("MouseCoordMode", 1)
                $Position = GetClientPos($GUI)
                Local $Pos1 = MouseGetPos(), $x1, $x2, $y1, $y2
                $RegionGUI = GUICreate("", 1, 1, $Pos1[0], $Pos1[1], BitOR($WS_POPUP, $WS_BORDER), $WS_EX_TOPMOST)
                WinSetTrans($RegionGUI, "", 50)
                GUISetState(@SW_SHOW, $RegionGUI)
                Do
                    Local $Pos2 = MouseGetPos()
                    If $Pos2[0] < $Pos1[0] And $Pos2[1] < $Pos1[1] Then
                        $x2 = $Pos1[0]
                        $y2 = $Pos1[1]
                        $x1 = $Pos2[0]
                        $y1 = $Pos2[1]
                    ElseIf $Pos2[0] > $Pos1[0] And $Pos2[1] < $Pos1[1] Then
                        $x1 = $Pos1[0]
                        $y2 = $Pos1[1]
                        $x2 = $Pos2[0]
                        $y1 = $Pos2[1]
                    ElseIf $Pos2[0] > $Pos1[0] And $Pos2[1] > $Pos1[1] Then
                        $x1 = $Pos1[0]
                        $y1 = $Pos1[1]
                        $x2 = $Pos2[0]
                        $y2 = $Pos2[1]
                    ElseIf $Pos2[0] < $Pos1[0] And $Pos2[1] > $Pos1[1] Then
                        $x2 = $Pos1[0]
                        $y1 = $Pos1[1]
                        $x1 = $Pos2[0]
                        $y2 = $Pos2[1]
                    EndIf
                    If $x1 < $Position[0] Then $x1 = $Position[0]
                    If $x2 > $Position[2] Then $x2 = $Position[2]
                    If $y1 < $Position[1] Then $y1 = $Position[1]
                    If $y2 > $Position[3] Then $y2 = $Position[3]
                    WinMove($RegionGUI, "", $x1, $y1, $x2 - $x1, $y2 - $y1)
                    If $Tray = 1 Then TrayTip("", "X (Left) : " & $x1 & "  | Y (Up) : " & $y1 & @CRLF & "X (Right) : " & $x2 & "  | Y (Down) : " & $y2 & @CRLF & "Region size : " & $x2 - $x1 & " x " & $y2 - $y1, 1, 16)
                Until Not _IsPressed("01")
                GUIDelete($RegionGUI)
                ExitLoop
            EndIf
        EndIf
    WEnd
    Local $Pos3[6] = [$x1, $y1, $x2, $y2, $x2 - $x1, $y2 - $y1]
    Return $Pos3
EndFunc   ;==>Region
Edited by Jex
Link to comment
Share on other sites

Hi,

Thank you for sharing you tidy script

Nice work.

But since your using GDIplus to get the image dimension, can't you use gdi to crop and or resize and output the image to any format without the need of third party ImageMagic?

Cheers

After read your post im looked help file for see gdiplus functions list and im found that function "_GDIPlus_BitmapCloneArea" and that function can crop image :P

Thanks for your help ;)

Now im creating Image Editor and i can put "_GDIPlus_BitmapCloneArea" that function in script.

But still good example for use ImageMagick i think.

Edited by Jex
Link to comment
Share on other sites

.....

But still good example for use ImageMagick i think.

yep agreed :P

I look forward to your editor release, maybe I can borrow some of your code when it's done for my own recent script or vice versa?

Here is my last script that I'm still working on when I get the time.

It's not so much an image editor , more a image conversion tool.

Cheers

Link to comment
Share on other sites

I'm added GUI. Script not completed yet.

I have problem about find client position. I'm using _WinGetClientPos function for find client position relative to screen but that function take 1 or 2 second to receive positions, for that reason crop image not instantly :) I can't find fast solution for find client position. ( http://www.autoitscript.com/forum/index.php?showtopic=58367 )

Edit : Now find client position instantly. ( That mean image crop instantly )

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