Malkey Posted March 24, 2009 Share Posted March 24, 2009 (edited) The Hex colour format used in this script is 0xBBGGRRAA. 0x - indicates the number is a hexidecimal number;BB - Blue channel, range 0x00 to 0xFF (0 - 255);GG - Green channel, range 0x00 to 0xFF (0 - 255);RR - Red channel, range 0x00 to 0xFF (0 - 255);AA - Alpha channel, range 0x00 to 0xFF (0 - 255), 0x00 - makes the pixel transparent (invisible) and 0xFF - makes the pixel fully opaque (completely visible)..0xFFFFFFFF is white. All colour channels are at their maximum.0x000000FF is black. All colour channels are empty, and is completely visible with the alpha channel being at maximum.It is important to know this to get the best use of this script.Normally, the colour format 0xAARRGGBB is most common in GDIPlus stuff. But because _GDIPlus_BitmapLockBits() is used, the raw data being manipulated is in the 0xBBGGRRAA format.This script is a utility for manipulating colours or pixels of an image. Here is some of the things that can be done:-- Search of a particular colour and replace all occurrences of the colour with another specified colour. - Same as previous, but with a range added to the search colour. the range is plus or minus range/2.see example 5- Search within a rectangular area within the image and change the pixels. The changed area can then be superimposed on the original image. See example 1.- Change just the transparency of a colour or range of colours. See examples 2 and 3.- Change the transparent colour/s to any visible colour. See example 4.- Use a regular expression to do anything you want. See example RegExp Eg.- Choice of images to use - Screen capture, image from file, or previous modified image.- Can save modified image.- Enable/disable colour tool tip zoom window with colour selection to clipboard. - A "RUNNING", Process time display.- All parameter input boxes can be changed, and the image can be processed again.- Added 30Mar2009 - Can now set the captured clipboard colour pixel to image with Alt + Left click. RegExp note:-With a search pattern of "(12)(34)(56)" and a replacement pattern of "${3}${1}", all occurrences of "123456" will be replaced with "5612". The return value, ${#}, refers to the search pattern "(number1)(number2)(number3)" , that search pattern which is within the () brackets.The longest process time of all the examples on my machine is about 7 secs.Bugs:-- Sometimes an image display is blank after the script has stopped running. To get the display, drag the window off and back on to the desktop. This auto-repaints the window.- Sometimes on start-up, the Color Modified window does not display the darken rectangle area in the center of the image. Press GO button to re-process.- There are not too many error trapping routines present, but appears It works ok on my xp.Bug fix 24Mar2009: When Example 1 produced a composite window, and that image was used as the "Original" image for the next search and replace, it would not work.Now, when "Use Modified Image" radio button is selected, the next "Original" image is created. To be re-created correctly, it uses the parameters in the "Second _CraphicsDrawImageRect() Function" input boxes, if they exist.So, select "Use Modified Image" before changing the parameter for the next image.Also now, the image selected when "Use Modified Image" was pressed remains as the "Original" image until one of the three "Image Source" radio buttons is pressed again.Hope you enjoy it. I had a learning experience doing it.CODE#include <GuiConstantsEx.au3>#include <GDIPlus.au3>#include <WinAPI.au3>#include <ScreenCapture.au3>#include <ButtonConstants.au3>#include <StaticConstants.au3>#include <WindowsConstants.au3>#include <EditConstants.au3>#include <Color.au3>#include <Array.au3>#include <Misc.au3>; http://www.autoitscript.com/forum/index.ph...st&p=660196Opt("MouseCoordMode", 0) ;1=absolute, 0=relative, 2=clientGlobal $sGUITitle = "Ctrl+Left Click to Clpbrd: Alt+Left to Image", $z1 = 28, $SIZEX = 256, $SIZEY = 300, $GUIZoom, $hGraphicGlobal $zoomX = Int($SIZEX / $z1), $zoomY = Int(($SIZEY - 44) / $z1), $DeskDC, $MyDC, $hPen, $hColLab, $source, $hBitmap1Global $hGUI1, $hGUI2, $OWin, $CMWin, $hGraphic1, $hGraphic2, $hBitmap, $GuiSizeX, $GuiSizeY, $PathFile, $checkCN, $sColorGlobal $aInputData[6][19] = [["$OWin", 40, 60, "$GuiSizeX-80", "$GuiSizeY-120", "0xE0E0E0FF", "0x000000FF", 30, "$OWin", 0, 0, "$GuiSizeX", _ "$GuiSizeY", "$CMWin", 40, 60, "$GuiSizeX-80", "$GuiSizeY-120", _ "Changes all near white colours in a rectanular area to fully opaque black," & @CRLF & "using range parameter"], _ ["$OWin", 0, 0, "$GuiSizeX", "$GuiSizeY", "0xE0E0E0FF", "Default", 30, "$CMWin", 0, 0, "$GuiSizeX", "$GuiSizeY", "", "", "", "", "", _ "Changes all near white colours in image to fully transparent," & @CRLF & "using range parameter"], _ ["$OWin", "", "", "", "", "", "", "", "$CMWin", 0, 0, "$GuiSizeX", "$GuiSizeY", "", "", "", "", "", _ "Changes the background colour at point x=1, y=1 of image to fully transparent," & @CRLF & "using Default parameters."], _ ["$OWin", 0, 0, "$GuiSizeX", "$GuiSizeY", "(.{6}00) ", "0x00FFFFA0", "", "$CMWin", 0, 0, "$GuiSizeX", "$GuiSizeY", "", "", "", "", "", _ "Changes all existing transparent colours to (100 * 0xA0/0xFF) percent opaque yellow." & @CRLF & _ " Range auto-set to 0. Format of $iColSrch is 0xBBGGRRAA AA is Alpha channel." & @CRLF & _ " 00 - completely transparent, FF- fully opaque."], _ ["$OWin", 0, 0, "$GuiSizeX", "$GuiSizeY", "0xE0FFFFFF", "0x000000FF", 30, "$CMWin", 0, 0, "$GuiSizeX", "$GuiSizeY", "", "", "", "", "", _ "Changes existing colours to another colour." & @CRLF & "Colour format is always hex 0xBBGGRRAA"], _ ["$OWin", 0, 0, "$GuiSizeX", "$GuiSizeY", "([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})(FF)", "${3}${2}${1}${4}", "", _ "$CMWin", 0, 0, "$GuiSizeX", "$GuiSizeY", "", "", "", "", "", _ "Swaps all existing blue (BB) and red (RR) channnels within each pixel." & @CRLF & _ " Colour format is 0xBBGGRRAA AA is Alpha channel (transparency." & @CRLF & _ " An example of using a regular expression."]]Global $Tips[8] = ["Image Handle" & @CRLF & "$OWin - Original Window" & @CRLF & "$CMWin - Colour Modified Window", _ "Window Top Left X Pos", "Window Top Left Y Pos", "Variable $GuiSizeX" & @CRLF & "is Original window Width.", _ "Variable $GuiSizeY" & @CRLF & "is Original window Height", "A colour or RegExp of a colour to search for." & @CRLF & _ "Colour format is hex 0xBBGGRRAA", "Replace with", "Range"]Local $Radio[6], $Input[19], $Label[19], $Err = 0, $aPosClientLocal $CtrlWidths[18] = [83, 41, 45, 113, 113, 113, 113, 33, 77, 47, 47, 107, 107, 77, 47, 47, 107, 107]Local $LabelText[8] = ["Image", "Left Pos", "Top Pos", "GUI Width", "GUI Height", "Search For", "Replace with", "Range"]$Form1_1 = GUICreate("Modify Pixels", 779, 321, @DesktopWidth - 779, 30, -1, $WS_EX_TOPMOST)Local $Grap = GUICtrlCreateGraphic(0, 0, 779, 321)For $x = 1 To 321 $hue = Color_SetHSL(Int($x / 2)) GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 1, $x) GUICtrlSetGraphic(-1, $GUI_GR_COLOR, "0x" & Hex($hue, 6)) GUICtrlSetGraphic(-1, $GUI_GR_LINE, 779, $x)NextGUICtrlSetState($Grap, $GUI_DISABLE)$Group1 = GUICtrlCreateGroup("ImageColorToTransparent() Function Parameters", 8, 8, 761, 73)GUICtrlSetBkColor(-1, 0xEFC1BA)$Input[0] = GUICtrlCreateInput("$OWin", 16, 30, $CtrlWidths[0], 24)GUICtrlSetTip(-1, $Tips[0])GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")$Label[0] = GUICtrlCreateLabel($LabelText[0], 16, 56, $CtrlWidths[0], 20)GUICtrlSetBkColor(-1, 0xEFC1BA)GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")For $num = 1 To 7 $CtrlPos = ControlGetPos("Modify Pixels", "", $Input[$num - 1]) $Input[$num] = GUICtrlCreateInput("", $CtrlPos[0] + $CtrlWidths[$num - 1] + 12, $CtrlPos[1], $CtrlWidths[$num], 24) GUICtrlSetTip(-1, $Tips[$num]) GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif") $Label[$num] = GUICtrlCreateLabel($LabelText[$num], $CtrlPos[0] + $CtrlWidths[$num - 1] + 12, $CtrlPos[1] + 26, $CtrlWidths[$num] + 9, 20) GUICtrlSetBkColor(-1, 0xEFC1BA) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")NextGUICtrlCreateGroup("", -99, -99, 1, 1)$Group2 = GUICtrlCreateGroup("_GDIPlus_GraphicsDrawImageRect() Function Parameters", 8, 84, 440, 73)GUICtrlSetBkColor(-1, 0xEBFFBA)$Input[8] = GUICtrlCreateInput("$OWin", 16, 106, $CtrlWidths[8], 24)GUICtrlSetTip(-1, $Tips[0])GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")$Label[8] = GUICtrlCreateLabel($LabelText[0], 16, 132, $CtrlWidths[8], 20)GUICtrlSetBkColor(-1, 0xEBFFBA)GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")For $num = 9 To 12 $CtrlPos = ControlGetPos("Modify Pixels", "", $Input[$num - 1]) $Input[$num] = GUICtrlCreateInput("", $CtrlPos[0] + $CtrlWidths[$num - 1] + 8, $CtrlPos[1], $CtrlWidths[$num], 24) GUICtrlSetTip(-1, $Tips[$num - 8]) GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif") $Label[$num] = GUICtrlCreateLabel($LabelText[$num - 8], $CtrlPos[0] + $CtrlWidths[$num - 1] + 8, $CtrlPos[1] + 26, $CtrlWidths[$num] + 5, 20) GUICtrlSetBkColor(-1, 0xEBFFBA) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")NextGUICtrlCreateGroup("", -99, -99, 1, 1)$Group3 = GUICtrlCreateGroup("Second _GraphicsDrawImageRect() Function Parameters", 8, 160, 440, 73)GUICtrlSetBkColor(-1, 0xBAEFBA)$Input[13] = GUICtrlCreateInput("$OWin", 16, 182, $CtrlWidths[13], 24)GUICtrlSetTip(-1, $Tips[0])GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")$Label[13] = GUICtrlCreateLabel($LabelText[0], 16, 208, $CtrlWidths[13], 20)GUICtrlSetBkColor(-1, 0xBAEFBA)GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")For $num = 14 To 17 $CtrlPos = ControlGetPos("Modify Pixels", "", $Input[$num - 1]) $Input[$num] = GUICtrlCreateInput("", $CtrlPos[0] + $CtrlWidths[$num - 1] + 8, $CtrlPos[1], $CtrlWidths[$num], 24) GUICtrlSetTip(-1, $Tips[$num - 13]) GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif") $Label[$num] = GUICtrlCreateLabel($LabelText[$num - 13], $CtrlPos[0] + $CtrlWidths[$num - 1] + 8, $CtrlPos[1] + 26, $CtrlWidths[$num] + 5, 20) GUICtrlSetBkColor(-1, 0xBAEFBA) GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")NextGUICtrlCreateGroup("", -99, -99, 1, 1)$Group4 = GUICtrlCreateGroup("Pick Image Source", 571, 84, 129, 90)GUICtrlSetBkColor(-1, 0xA6CAF0)$Radio6 = GUICtrlCreateRadio("Screen Capture", 579, 108, 113, 17)GUICtrlSetBkColor(-1, 0xA6CAF0)GUICtrlSetState(-1, $GUI_CHECKED)$Radio7 = GUICtrlCreateRadio("Load from File", 579, 129, 113, 17)GUICtrlSetBkColor(-1, 0xA6CAF0)$Radio1 = GUICtrlCreateRadio("Use Modified Image", 579, 150, 113, 17)GUICtrlSetBkColor(-1, 0xA6CAF0)GUICtrlCreateGroup("", -99, -99, 1, 1)$checkCN = GUICtrlCreateCheckbox("Select Colour", 579, 177, 113, 17, -1, $BS_NOTIFY)GUICtrlSetBkColor(-1, 0xBAEFCA)GUICtrlSetState($checkCN, $GUI_UNCHECKED)$Group5 = GUICtrlCreateGroup("Select Example", 457, 84, 105, 148)GUICtrlSetBkColor(-1, 0xA0FFA0)For $x = 0 To UBound($Radio) - 1 $Radio[$x] = GUICtrlCreateRadio("Example " & $x + 1, 464, 105 + $x * 21, 89, 17) GUICtrlSetBkColor(-1, 0xA0FFA0)NextGUICtrlSetState($Radio[0], $GUI_CHECKED)GUICtrlSetData($Radio[uBound($Radio) - 1], "RegExp eg.")GUICtrlSetBkColor(-1, 0xA0FFA0)GUICtrlCreateGroup("", -99, -99, 1, 1)$Button1 = GUICtrlCreateButton("Go", 706, 90, 65, 41)GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")$Button2 = GUICtrlCreateButton("Exit", 706, 185, 65, 41)GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")GUICtrlSetBkColor(-1, 0xFF0000)$Input[18] = GUICtrlCreateEdit("", 8, 240, 753, 72, $ES_MULTILINE);, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$WS_HSCROLL,$WS_VSCROLL))GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")$Button3 = GUICtrlCreateButton("Save Image", 706, 137, 65, 41)$Label1 = GUICtrlCreateLabel("Process Time", 576, 197, 102, 17)GUICtrlSetBkColor(-1, 0xF8F8F8)$Label2 = GUICtrlCreateLabel("", 576, 213, 102, 20)GUICtrlSetBkColor(-1, 0xF8F8F8)GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")GUISetState(@SW_SHOW);WinWaitActive("Modify Pixels")_GDIPlus_Startup()GUICtrlSetState($Group1, $GUI_HIDE)GUIRegisterMsg(0xF, "MY_PAINT"); Register PAINT-Event 0x000F = $WM_PAINT (WindowsConstants.au3)GUIRegisterMsg(0x85, "MY_PAINT") ; $WM_NCPAINT = 0x0085 (WindowsConstants.au3)Restore after Minimize.$source = "Screen"ControlClick("Modify Pixels", "", $Radio[0])Sleep(10)_Main("Screen");, "init");ControlClick("Modify Pixels", "", $Radio[0])ControlClick("Modify Pixels", "", $Button1)While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE, $Button2 ExitLoop Case $checkCN If BitAND(GUICtrlRead($checkCN), $GUI_CHECKED) = 1 Then $GUIZoom = GUICreate($sGUITitle, $SIZEX, $SIZEY, @DesktopWidth - $SIZEX - 5, @DesktopHeight - $SIZEY - 50, _ $WS_CLIPSIBLINGS, $WS_EX_TOPMOST) GUICtrlSetState($checkCN, $GUI_CHECKED) WinSetTrans($sGUITitle, "", 254) $hColLab = GUICtrlCreateLabel("This is a label for colours.", 0, 0, 254, 39) GUISetBkColor(0xFFFFA0) GUISetState() $DeskDC = _WinAPI_GetDC(0) $MyDC = _WinAPI_GetDC($GUIZoom) $hGraphic = _GDIPlus_GraphicsCreateFromHWND($GUIZoom) $hPen = _GDIPlus_PenCreate() Else GUICtrlSetState($checkCN, $GUI_UNCHECKED) GUIDelete($GUIZoom) _WinAPI_ReleaseDC(0, $DeskDC) _WinAPI_ReleaseDC($GUIZoom, $MyDC) EndIf Case $Radio1 If BitAND(GUICtrlRead($Radio1), $GUI_CHECKED) = 1 Then If Execute(GUICtrlRead($Input[13])) <> "" Then Local $hImageS = _GDIPlus_BitmapCloneArea($OWin, 0, 0, $GuiSizeX, $GuiSizeY, $GDIP_PXF32ARGB) Local $hGraphicS = _GDIPlus_ImageGetGraphicsContext($hImageS) _GDIPlus_GraphicsDrawImage($hGraphicS, $CMWin, GUICtrlRead($Input[14]), GUICtrlRead($Input[15])) _GDIPlus_ImageDispose($OWin) $OWin = _GDIPlus_BitmapCloneArea($hImageS, 0, 0, $GuiSizeX, $GuiSizeY, $GDIP_PXF32ARGB) _GDIPlus_GraphicsDispose($hGraphicS) _GDIPlus_ImageDispose($hImageS) Else _GDIPlus_ImageDispose($OWin) $OWin = _GDIPlus_BitmapCloneArea($CMWin, 0, 0, $GuiSizeX, $GuiSizeY, $GDIP_PXF32ARGB) EndIf EndIf Case $Radio[0] To $Radio[5] For $num = 0 To 17 GUICtrlSetData($Input[$num], $aInputData[$nMsg - $Radio[0]][$num]) Next GUICtrlSetData($Input[18], "$GuiSizeX = " & $GuiSizeX & "; $GuiSizeY = " & $GuiSizeY & @CRLF & _ $aInputData[$nMsg - $Radio[0]][18]) Case $Button1 $Err = 0 ;ConsoleWrite("Cleanup $Radio1 " & BitAND(GUICtrlRead($Radio1), $GUI_CHECKED) & @CRLF) If BitAND(GUICtrlRead($Radio6), $GUI_CHECKED) = 1 Then $source = "Screen" If BitAND(GUICtrlRead($Radio1), $GUI_CHECKED) = 1 Then $source = "ModFile" If BitAND(GUICtrlRead($Radio7), $GUI_CHECKED) = 1 Then $source = "File" Local $PathFile = FileOpenDialog("Choose a File", @ScriptDir & "\", "Images (*.jpg;*.bmp;*.png;*.gif)|All Files (*.*)", 3) If @error Then $Err = 1 EndIf If $Err <> 1 Then CleanUpAll() If $Err <> 1 Then _Main($source) Case $Button3 $var = FileSaveDialog("Choose a name.", @ScriptDir, "Images (*.bmp;*.jpg;*.png;*.gif)|All files (*.*)", 16, "ColourModify.png") If Not @error Then If Execute(GUICtrlRead($Input[13])) <> "" Then Local $hImageS = _GDIPlus_BitmapCloneArea($OWin, 0, 0, $GuiSizeX, $GuiSizeY, $GDIP_PXF32ARGB) Local $hGraphicS = _GDIPlus_ImageGetGraphicsContext($hImageS) _GDIPlus_GraphicsDrawImage($hGraphicS, $CMWin, GUICtrlRead($Input[14]), GUICtrlRead($Input[15])) _GDIPlus_ImageSaveToFile($hImageS, $var) _GDIPlus_GraphicsDispose($hGraphicS) _GDIPlus_ImageDispose($hImageS) Else _GDIPlus_ImageSaveToFile($CMWin, $var) EndIf ShellExecute($var) EndIf EndSwitch If BitAND(GUICtrlRead($checkCN), $GUI_CHECKED) = 1 Then Opt("MouseCoordMode", 1) ;1=absolute, 0=relative, 2=client $aPos = MouseGetPos() Sleep(50) _WinAPI_StretchBlt($MyDC, 0, 44, $SIZEX, $SIZEY - 44, $DeskDC, $aPos[0] + 1 - ($zoomX / 2), $aPos[1] + 1 - ($zoomY / 2), $zoomX, $zoomY, $SRCCOPY) _GDIPlus_GraphicsDrawLine($hGraphic, 128, 45, 128, $SIZEY - 1, $hPen) _GDIPlus_GraphicsDrawLine($hGraphic, 1, 172, 255, 172, $hPen) $sColor = GetPixel(0, $aPos[0], $aPos[1]) Opt("MouseCoordMode", 2) ;1=absolute, 0=relative, 2=client $aPosClient = MouseGetPos() Opt("MouseCoordMode", 1) ;1=absolute, 0=relative, 2=client $sAWinTte = WinGetTitle("") GUICtrlSetData($hColLab, "0xBBGGRR colour is " & $sColor & " ; X, Y : " & $aPosClient[0] & ", " & $aPosClient[1] & @CRLF & _ "Active Win Title : " & _Iif(StringLen($sAWinTte) > 35, StringLeft($sAWinTte, 13) & "..." & StringRight($sAWinTte, 18), _ $sAWinTte) & @CRLF & _ "Screen X, Y : " & $aPos[0] & ", " & $aPos[1]) Opt("MouseCoordMode", 0) ;1=absolute, 0=relative, 2=client EndIf If IsPressed("01") And IsPressed("12") Then ;Left click + Alt key Do Until Not IsPressed("01") Local $sPutCol Local $sOnCl = ClipGet() If StringLeft($sOnCl, 2) = "0x" Then $sPutCol = "0x" & StringRight($sOnCl, 2) & StringMid($sOnCl, 7, 2) & StringMid($sOnCl, 5, 2) & StringMid($sOnCl, 3, 2) Else $sPutCol = "0xFFFF0000" EndIf Opt("MouseCoordMode", 2) ;1=absolute, 0=relative, 2=client $aPos = MouseGetPos() if WinGetTitle("") = "Original" or WinGetTitle("") = "Colour Modified" then DllCall($ghGDIPDll, "int", "GdipBitmapSetPixel", "hwnd", $OWin, "int", $aPos[0], "int", $aPos[1], "dword", $sPutCol) ;if WinGetTitle("") = "Colour Modified" then _ DllCall($ghGDIPDll, "int", "GdipBitmapSetPixel", "hwnd", $CMWin, "int", $aPos[0], "int", $aPos[1], "dword", $sPutCol) EndIf Opt("MouseCoordMode", 1) ;1=absolute, 0=relative, 2=client If WinGetHandle("") = WinGetHandle("Colour Modified") or WinGetHandle("") = WinGetHandle("Original") Then _GDIPlus_GraphicsDrawImageRect($hGraphic1, $OWin, 0, 0, $GuiSizeX, $GuiSizeY) If GUICtrlRead($Input[13]) <> "" Or GUICtrlRead($Input[14]) <> ""Then _GDIPlus_GraphicsDrawImageRect($hGraphic2, Execute(GUICtrlRead($Input[8])), GUICtrlRead($Input[9]), GUICtrlRead($Input[10]), _ Execute(GUICtrlRead($Input[11])), Execute(GUICtrlRead($Input[12]))) _GDIPlus_GraphicsDrawImageRect($hGraphic2, Execute(GUICtrlRead($Input[13])), GUICtrlRead($Input[14]), GUICtrlRead($Input[15]), _ Execute(GUICtrlRead($Input[16])), Execute(GUICtrlRead($Input[17]))) Else _GDIPlus_GraphicsDrawImageRect($hGraphic2, Execute(GUICtrlRead($Input[8])), GUICtrlRead($Input[9]), GUICtrlRead($Input[10]), _ Execute(GUICtrlRead($Input[11])), Execute(GUICtrlRead($Input[12]))) EndIf EndIf EndIf If IsPressed("01") And IsPressed("11") Then ;Left click + Ctrl key Do Until Not IsPressed("01") If BitAND(GUICtrlRead($checkCN), $GUI_CHECKED) = 1 Then ClipPut($sColor & "FF") ;ConsoleWrite('$sColor & "FF = "' & $sColor & "FF" & @CRLF) EndIfWEnd_GDIPlus_Shutdown()Func _Main($source) If $source = "File" Then $OWin = _GDIPlus_ImageLoadFromFile($PathFile) ; @DesktopDir & "\heart.png") $GuiSizeX = _GDIPlus_ImageGetWidth($OWin) $GuiSizeY = _GDIPlus_ImageGetHeight($OWin) ;ConsoleWrite("$GuiSizeX = " & $GuiSizeX & @CRLF & "$GuiSizeY = " & $GuiSizeY & @CRLF) ElseIf $source = "Screen" Then ; Capture top left corner of the screen $GuiSizeX = 300 $GuiSizeY = 400 $hBitmap = _ScreenCapture_Capture("", 0, 0, $GuiSizeX, $GuiSizeY) $OWin = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap) EndIf ; Create a GUI for the original image $hGUI1 = GUICreate("Original", $GuiSizeX + 10, $GuiSizeY + 30, 0, 0, $WS_CLIPSIBLINGS) GUISetState() ; Create a GUI for the colour modified image $hGUI2 = GUICreate("Colour Modified", $GuiSizeX + 10, $GuiSizeY + 30, 0, $GuiSizeY + 30, $WS_CLIPSIBLINGS) GUISetState() ; Draw original image $hGraphic1 = _GDIPlus_GraphicsCreateFromHWND($hGUI1) ;_GDIPlus_GraphicsDrawImage($hGraphic1, $OWin 0, 0) _GDIPlus_GraphicsDrawImageRect($hGraphic1, $OWin, 0, 0, $GuiSizeX, $GuiSizeY) $hGraphic2 = _GDIPlus_GraphicsCreateFromHWND($hGUI2) ;Read all parameters in input boxes $ calls functions $CMWin = ImageColorToTransparent(Execute(GUICtrlRead($Input[0])), GUICtrlRead($Input[1]), GUICtrlRead($Input[2]), _ Execute(GUICtrlRead($Input[3])), Execute(GUICtrlRead($Input[4])), GUICtrlRead($Input[5]), _ GUICtrlRead($Input[6]), GUICtrlRead($Input[7])); _GDIPlus_GraphicsDrawImageRect($hGraphic2, Execute(GUICtrlRead($Input[8])), GUICtrlRead($Input[9]), GUICtrlRead($Input[10]), _ Execute(GUICtrlRead($Input[11])), Execute(GUICtrlRead($Input[12]))) If Execute(GUICtrlRead($Input[13])) > 0 And GUICtrlRead($Input[14]) <> "" And GUICtrlRead($Input[15]) <> "" And _ Execute(GUICtrlRead($Input[16])) > 0 And Execute(GUICtrlRead($Input[17])) > 0 Then _ _GDIPlus_GraphicsDrawImageRect($hGraphic2, Execute(GUICtrlRead($Input[13])), _ GUICtrlRead($Input[14]), GUICtrlRead($Input[15]), _ Execute(GUICtrlRead($Input[16])), Execute(GUICtrlRead($Input[17]))) ; ========> End of Examples ======================================================== _WinAPI_RedrawWindow($hGUI1) _WinAPI_InvalidateRect($hGUI2) _GDIPlus_GraphicsDrawImageRect($hGraphic1, $OWin, 0, 0, $GuiSizeX, $GuiSizeY) If GUICtrlRead($Input[14]) > 0 Or GUICtrlRead($Input[15]) > 0 Then _GDIPlus_GraphicsDrawImageRect($hGraphic2, Execute(GUICtrlRead($Input[8])), GUICtrlRead($Input[9]), GUICtrlRead($Input[10]), _ Execute(GUICtrlRead($Input[11])), Execute(GUICtrlRead($Input[12]))) _GDIPlus_GraphicsDrawImageRect($hGraphic2, Execute(GUICtrlRead($Input[13])), GUICtrlRead($Input[14]), GUICtrlRead($Input[15]), _ Execute(GUICtrlRead($Input[16])), Execute(GUICtrlRead($Input[17]))) Else _GDIPlus_GraphicsDrawImageRect($hGraphic2, Execute(GUICtrlRead($Input[8])), GUICtrlRead($Input[9]), GUICtrlRead($Input[10]), _ Execute(GUICtrlRead($Input[11])), Execute(GUICtrlRead($Input[12]))) EndIf ReturnEndFunc ;==>_MainFunc CleanUpAll() ; Release resources If $hBitmap <> "" Then _WinAPI_DeleteObject($hBitmap) _GDIPlus_GraphicsDispose($hGraphic1) _GDIPlus_GraphicsDispose($hGraphic2) If BitAND(GUICtrlRead($Radio1), $GUI_CHECKED) = 1 Then _GDIPlus_ImageDispose($CMWin) Else _GDIPlus_ImageDispose($OWin) _GDIPlus_ImageDispose($CMWin) EndIf GUIDelete($hGUI1) GUIDelete($hGUI2) ReturnEndFunc ;==>CleanUpAllFunc ImageColorToTransparent($hImage2, $iStartPosX = 0, $iStartPosY = 0, $iGuiSizeX = Default, $iGuiSizeY = Default, _ $iColSrch = Default, $iColNew = Default, $Range = 0) Local $Reslt, $width, $height, $stride, $format, $Scan0, $v_Buffer, $v_Value, $iIW, $iIH Local $iColorSrch, $Timer, $v_BufferA, $AllPixels, $sREResult1, $iRnge, $blue, $sBlueString, $green, $sGreenString Local $red, $sRedString, $sSearch, $iColorNew $iIW = _GDIPlus_ImageGetWidth($hImage2) $iIH = _GDIPlus_ImageGetHeight($hImage2) If $iGuiSizeX = Default Or $iGuiSizeX = "" Then $iGuiSizeX = $GuiSizeX ;ConsoleWrite("$iGuiSizeX = " & $iGuiSizeX & @CRLF) If $iGuiSizeY = Default Or $iGuiSizeY = "" Then $iGuiSizeY = $GuiSizeY ;ConsoleWrite("$iGuiSizeY = " & $iGuiSizeY & @CRLF) $hBitmap1 = _GDIPlus_BitmapCloneArea($hImage2, $iStartPosX, $iStartPosY, $iGuiSizeX, $iGuiSizeY, $GDIP_PXF32ARGB); $GDIP_PXF16RGB555); ; Transparent color If $iColSrch = Default Or $iColSrch = "" Then $iColSrch = GDIPlus_BitmapGetPixel($hBitmap1, 1, 1) ; Convert 0xAARRGGBB to 0xBBGGRRAA $iColSrch = "0x" & Hex(BitAND(0x000000FF, $iColSrch), 2) & Hex(BitShift(BitAND(0x0000FF00, $iColSrch), 8), 2) & _ Hex(BitShift(BitAND(0x00FF0000, $iColSrch), 16), 2) & Hex(BitShift(BitAND(0xFF000000, $iColSrch), 24), 2) $Range = 0 EndIf If StringRegExp($iColSrch, "([][)(?#^{}\\:|,.])", 0) = 1 Then $iColorSrch = $iColSrch $Range = 0 Else ;Pixel format from bitmap in Hexidecimal BBGGRRAA format. So, convert to that format $iColorSrch = $iColSrch ;BBGGRRAA format EndIf ;ConsoleWrite("$iColSrch = " & $iColSrch & @CRLF) ;ConsoleWrite("$iColorSrch = " & $iColorSrch & @CRLF) If $iColNew = "Default" Or $iColNew = "" Then $iColNew = "${1}00 " If StringRegExp($iColSrch, "([][)(?#^{}\\:|,.])", 0) = 1 Then $iColorNew = $iColNew Else If StringLeft($iColNew, 2) = "0x" Then $iColNew = StringTrimLeft($iColNew, 2) $iColorNew = $iColNew ;BBGGRRAA format EndIf ;ConsoleWrite("Setting $iColorNew = " & $iColorNew & @CRLF) GUICtrlSetData($Label2, "Running", "") GUICtrlSetColor($Label2, 0xFF0000) $Timer = TimerInit() $Reslt = _GDIPlus_BitmapLockBits($hBitmap1, 0, 0, $iGuiSizeX, $iGuiSizeY, BitOR($GDIP_ILMREAD, $GDIP_ILMWRITE), $GDIP_PXF32ARGB) ;Get the returned values of _GDIPlus_BitmapLockBits () $width = DllStructGetData($Reslt, "width") $height = DllStructGetData($Reslt, "height") $stride = DllStructGetData($Reslt, "stride") $format = DllStructGetData($Reslt, "format") $Scan0 = DllStructGetData($Reslt, "Scan0") $v_BufferA = DllStructCreate("byte[" & $height * $width * 4 & "]", $Scan0) $AllPixels = DllStructGetData($v_BufferA, 1) ;ConsoleWrite("$AllPixels, raw data, first 9 colours = " & StringRegExpReplace($AllPixels, "(.{98})(.*)", "\1") & @CRLF) $sREResult1 = StringRegExpReplace(StringTrimLeft($AllPixels, 2), "(.{8})", "\1 ") ;ConsoleWrite("$sREResult1 first 9 colours = " & StringRegExpReplace($sREResult1, "(.{81})(.*)", "\1") & @CRLF) ;===================================================================================== #cs Local $search = "000000FF"; First StringReplace($sREResult1, $search, $search) Local $iNumOccor = @extended Local $aResult = "0x" & $search & " No. of matches = " & $iNumOccor & @LF & @LF Local $sResult For $x = 1 To $iNumOccor ; With space added in $sREResult1, No. of characters per pixel is 9. $sResult = (StringInStr($sREResult1, $search, 0, $x) - 1) / 9 ; Pixel position in string. Local $iPosY = Int($sResult / $width) + 1 ; Y pos of pixel from string position. Local $iPosX = ($sResult - (($iPosY - 1) * $width)) + 1; X Pos of pixel from same string position. $aResult &= "posX = " & $iPosX & " posY = " & $iPosY & @LF Next MsgBox(0, "Found Pixels", $aResult) #ce ;================================================================================ If $Range = 0 Then If StringRegExp($iColorSrch, "([][)(?#^{}\\:|,.])", 0) = 0 Then ;and StringLen($iColorSrch) = 8 If StringLeft($iColorNew, 2) = "0x" Then $iColorNew = StringTrimLeft($iColorNew, 2) Local $sResult = StringStripWS(StringRegExpReplace($sREResult1, "(" & StringTrimRight(StringTrimLeft($iColorSrch, 2), 2) & ")(.{2}) ", $iColorNew), 8) ;ConsoleWrite("Here " & "(" & StringTrimRight(StringTrimLeft($iColorSrch,2),2) & ")(.{2}) " & @CRLF & "$iColorNew " & $iColorNew & @CRLF) Else If StringLeft($iColorNew, 2) = "0x" Then $iColorNew = StringTrimLeft($iColorNew, 2) If StringLeft($iColorSrch, 2) = "0x" Then $iColorSrch = StringTrimLeft($iColorSrch, 2) Local $sResult = StringStripWS(StringRegExpReplace($sREResult1, $iColorSrch, $iColorNew), 8) ;ConsoleWrite("Here False regexp $Range = 0 " & $iColorSrch & " hex($iColorNew) " & $iColorNew & @CRLF) EndIf Else $iRnge = Int(255 * $Range / 200) $blue = "0x" & Hex(BitShift(BitAND(0xFF000000, $iColorSrch), 24), 2) ;ConsoleWrite("$blue = " & $blue & @CRLF ) $sBlueString = StringRERange($blue, $iRnge) ;ConsoleWrite("$sBlueString = " & $sBlueString & @CRLF & @CRLF) $green = "0x" & Hex(BitShift(BitAND(0x00FF0000, $iColorSrch), 16), 2) ;ConsoleWrite("$green = " & $green & @CRLF ) $sGreenString = StringRERange($green, $iRnge) ;ConsoleWrite("$sGreenString = " & $sGreenString & @CRLF & @CRLF) $red = "0x" & Hex(BitShift(BitAND(0x0000FF00, $iColorSrch), 8), 2) ;ConsoleWrite("$red = " & $red & @CRLF ) $sRedString = StringRERange($red, $iRnge) ;ConsoleWrite("$sRedString = " & $sRedString & @CRLF & @CRLF) $sSearch = "(" & $sBlueString & $sGreenString & $sRedString & ")" Local $sResult = StringStripWS(StringRegExpReplace($sREResult1, $sSearch & "FF ", $iColorNew), 8) ; "${1}00 "), 8) EndIf ;ConsoleWrite("$iColorNew = " & $iColorNew & @CRLF) ;ConsoleWrite("$sREResult1 first 9 colours = " & StringRegExpReplace($sResult, "(.{81})(.*)", "\1") & @CRLF) DllStructSetData($v_BufferA, 1, "0x" & $sResult) _GDIPlus_BitmapUnlockBits($hBitmap1, $Reslt) ;ConsoleWrite("Process time = " & Round(TimerDiff($Timer) / 1000, 6) & " Secs" & @CRLF) GUICtrlSetData($Label2, String(Round(TimerDiff($Timer) / 1000, 3)) & " Secs") GUICtrlSetColor($Label2, 0x000000) Return $hBitmap1EndFunc ;==>ImageColorToTransparentFunc StringRERange($iColSrch, $iRnge) Local $str = "(", $iColSrchMin, $iColSrchMax $iColSrchMin = "0x" & Hex((Dec(Hex($iColSrch)) - $iRnge) * ((Dec(Hex($iColSrch)) - $iRnge) > 0), 2) $iColSrchMax = "0x" & Hex((Dec(Hex($iColSrch)) + $iRnge) * ((Dec(Hex($iColSrch)) + $iRnge) < 255) + 255, 2) ;ConsoleWrite("$iColSrch = " & Hex($iColSrch) & " $iColSrchMin = " & Hex($iColSrchMin) & " $iColSrchMax = " & Hex($iColSrchMax) & @CRLF) ;ConsoleWrite("$iColSrch = " & Dec(Hex($iColSrch)) & " $iColSrchMin = " & Dec(Hex($iColSrchMin)) & " $iColSrchMax = " & Dec(Hex($iColSrchMax)) & @CRLF) For $n = $iColSrchMin To $iColSrchMax $str &= Hex($n, 2) & "|" Next $str = StringTrimRight($str, 1) & ")" Return $strEndFunc ;==>StringRERange;The GetPixel method gets the color of a specified pixel in this bitmap.Func GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY) Local $tArgb, $aRet $tArgb = DllStructCreate("dword Argb") $aRet = DllCall($ghGDIPDll, "int", "GdipBitmapGetPixel", "hwnd", $hBitmap, "int", $iX, "int", $iY, "ptr", DllStructGetPtr($tArgb)) Return "0x" & Hex(DllStructGetData($tArgb, "Argb"))EndFunc ;==>GDIPlus_BitmapGetPixelFunc MY_PAINT($hWnd, $msg, $wParam, $lParam) If $hWnd = $hGUI1 Then _GDIPlus_GraphicsDrawImageRect($hGraphic1, $OWin, 0, 0, $GuiSizeX, $GuiSizeY) If $hWnd = $hGUI2 Then If GUICtrlRead($Input[14]) > 0 Or GUICtrlRead($Input[15]) > 0 Then _GDIPlus_GraphicsDrawImageRect($hGraphic2, Execute(GUICtrlRead($Input[8])), GUICtrlRead($Input[9]), GUICtrlRead($Input[10]), _ Execute(GUICtrlRead($Input[11])), Execute(GUICtrlRead($Input[12]))) _GDIPlus_GraphicsDrawImageRect($hGraphic2, Execute(GUICtrlRead($Input[13])), GUICtrlRead($Input[14]), GUICtrlRead($Input[15]), _ Execute(GUICtrlRead($Input[16])), Execute(GUICtrlRead($Input[17]))) Else _GDIPlus_GraphicsDrawImageRect($hGraphic2, Execute(GUICtrlRead($Input[8])), GUICtrlRead($Input[9]), GUICtrlRead($Input[10]), _ Execute(GUICtrlRead($Input[11])), Execute(GUICtrlRead($Input[12]))) EndIf EndIf Return $GUI_RUNDEFMSGEndFunc ;==>MY_PAINTFunc Color_SetHSL($iHue, $Saturation = 150, $Brightness = 200);160) If IsArray($iHue) Then $aInput = $iHue Else Local $aInput[3] = [$iHue, $Saturation, $Brightness] EndIf Local $aiRGB = _ColorConvertHSLtoRGB($aInput) Return "0x" & Hex(Round($aiRGB[0]), 2) & Hex(Round($aiRGB[1]), 2) & Hex(Round($aiRGB[2]), 2)EndFunc ;==>Color_SetHSL; Copied from ...\Include\Misc.au3 FileFunc IsPressed($sHexKey) Local $a_R = DllCall('user32.dll', "int", "GetAsyncKeyState", "int", '0x' & $sHexKey) If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1 Return 0EndFunc ;==>IsPressedFunc GetPixel($handle, $x, $y) Local $dc, $icolor, $iRet $dc = DllCall("user32.dll", "int", "GetDC", "hwnd", $handle) $icolor = DllCall("gdi32.dll", "long", "GetPixel", "hwnd", $dc[0], "int", $x, "int", $y) DllCall("user32.dll", "int", "ReleaseDC", "hwnd", 0, "int", $dc[0]) ; Converts RGB to BGR colour and visa-versa $iRet = $icolor[0] ; BitAND(BitShift(String(Binary($iColor[0])), 8), 0xFFFFFF) ; Converts RGB to BGR colour and visa-versa Return "0x" & Hex($iRet, 6)EndFunc ;==>GetPixelFunc _WinAPI_StretchBlt($hDestDC, $iXDest, $iYDest, $iWidth, $iHeight, $hSrcDC, $iXSrc, $iYSrc, $iWidthSrc, $iHeightSrc, $iROP) Local $aResult = DllCall("GDI32.dll", "int", "StretchBlt", "hwnd", $hDestDC, "int", $iXDest, "int", $iYDest, "int", $iWidth, "int", $iHeight, _ "hwnd", $hSrcDC, "int", $iXSrc, "int", $iYSrc, "int", $iWidthSrc, "int", $iHeightSrc, "int", $iROP) If @error Then Return SetError(@error, 0, False) Return $aResult[0] <> 0EndFunc ;==>_WinAPI_StretchBltThis next script is for working out the regular expression.Local $v_ValueA = "0xA56E3AFFA56E3AFFA56E3AFFA56E3AFFA56E3A00A56E3A00C8D0D4FFC8D0D4FFC8D0D4FFC8D0D400C8D0D4FF" Local $sREResult1 = StringRegExpReplace(StringTrimLeft($v_ValueA, 2), "(.{8})", "\1 ") ;$sREResult = StringRegExpReplace($sREResult1, "([0-9B-D]{6})(FF)", "${1}00"); Get a range of colours to make transparent ;$sREResult = StringRegExpReplace($sREResult1, "(A56E3AFF|C8D0D4FF)", "000000FF"); Get specficate colour to change Local $sResult = StringRegExpReplace($sREResult1, "([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})(FF)", "${3}${2}${1}${4}"); Swap blue & red chanells. MsgBox(0,"Result", $sResult)Edit 24Mar2009: Update main script again. Fiddled with refreshing of $hGUI2 window. And thanks to WideBoyDixon and his Screen Crosshairs post for the idea, I added a zoom window for colour selection instead of a tool tip.Edit 30Mar2009: Another update. Edited March 31, 2009 by Malkey Link to comment Share on other sites More sharing options...
Josbe Posted March 24, 2009 Share Posted March 24, 2009 personally, very interesting...always are lessons your scripts. thxs • AUTOIT > AutoIt docs / Beta folder - AutoIt latest beta Link to comment Share on other sites More sharing options...
Xenobiologist Posted March 24, 2009 Share Posted March 24, 2009 Hi, looks interesting. May I use it instead of PixelSearch to search for a colour in a picture? Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
Malkey Posted March 25, 2009 Author Share Posted March 25, 2009 (edited) personally, very interesting...always are lessons your scripts. thxs I am pleased you found the script interesting. Thanks. Hi, looks interesting. May I use it instead of PixelSearch to search for a colour in a picture? MegaThe script as is will not return the x, y position of a colour like PixelSearch. Here is an example of one way how this could be done. The shortened raw data is of the type returned in the ImageColorToTransparent() function after the _GDIPlus_BitmapLockBits() function, first post script. The variables $width, $height, $AllPixels, $sREResult1 are compatible with the variables used in the first post script, if not the same. I did insert this example script into the main script, search on black (0x000000FF), and it returned over 1,700 matches, using example 1 as the "Original" image. This took a while, but if only the first match was needed, it would be fast. Although, PixelSearch is a built in function, which would have to be faster. ;Image Example 4x3 pixels Local $width = 4 Local $height = 3 Local $AllPixels = "0xBAC1EFFFBAC2EFFFBAC3EFFFBAC4EFFFBAC5EFFFBAC6EFFFBAC1EFFFBAC7EFFFB1DFEFFFB2DFEFFFB3DFEFFFBAC1EFFF" Local $sREResult1 = StringRegExpReplace(StringTrimLeft($AllPixels, 2), "(.{8})", "\1 ") ConsoleWrite("$sAllPixLess = " & $sREResult1 & @CRLF) Local $search = "BAC1EFFF"; First StringReplace($sREResult1, $search, $search) Local $iNumOccor = @extended Local $aResult = "0x" & $search & " No. of matches = " & $iNumOccor & @LF & @LF Local $sResult For $x = 1 To $iNumOccor ; With space added in $sREResult1, No. of characters per pixel is 9. $sResult = (StringInStr($sREResult1, $search, 0, $x) - 1) / 9; Pixel position in string. Local $iPosY = Int($sResult / $width) + 1 ; Y pos of pixel from string position. Local $iPosX = ($sResult - (($iPosY - 1) * $width)) + 1; X Pos of pixel from same string position. $aResult &= "posX = " & $iPosX & " posY = " & $iPosY & @LF Next MsgBox(0, "Found Pixels", $aResult) Local $AllPixels = "0xBAC1EFFFBAC2EFFFBAC3EFFFBAC4EFFFBAC5EFFFBAC6EFFFBAC1EFFFBAC7EFFFB1DFEFFFB2DFEFFFB3DFEFFFBAC1EF FF"This is strange. In the submit post edit box, the above line appears on one line. I have tried "code-/code, codebox-/codebox, and autoit-/autoit". All produce the split line error. Edited March 25, 2009 by Malkey Link to comment Share on other sites More sharing options...
Malkey Posted March 31, 2009 Author Share Posted March 31, 2009 (edited) Updated first post Here is a simpler, basic version of the method used in the first post. It puts all the pixels of an image as a string in the variable $AllPixels. Then, StringRegExpReplace() is used. Un - comment the _ImageColorRegExpReplace() example line you wish to try within the script, or, write your own regular expression. Enjoy. expandcollapse popup#include <Array.au3> #include <GuiConstantsEx.au3> #include <GDIPlus.au3> #include <Misc.au3> #include <ScreenCapture.au3> ; http://www.autoitscript.com/forum/index.php?s=&showtopic=91755&view=findpost&p=663576 Opt('MustDeclareVars', 1) Global $width, $height, $hGUI1, $hGUI2, $hImage, $hImage2, $hGraphic1, $hGraphic2, $iX, $iY, $sImageIn _Main() ;Run('C:\Program Files\GIMP 2\bin\gimp-2.8.exe "F:\Program Files\Autoio\Examples\Uploads\ColMod.png"') Func _Main() ;$sImageIn = "C:\Windows\Globalization\MCT\MCT-AU\Wallpaper\AU-wp2.jpg" ;$sImageIn = "C:\Users\Mal\Documents\Singer29K58BootPatcher6.png" ;or ; Capture top left corner of the screen ;$sImageIn = "GDIPlus_Image.png" ;_ScreenCapture_Capture($sImageIn, 0, 0, 400, 300) ;Or $sImageIn = FileOpenDialog("First image", "", "All images (*.jpg;*.png;*.gif;*.bmp;)") ;If $sImageIn = "" Then Exit ;ConsoleWrite($sImageIn & @LF) ;Or ;InetGet("http://img11.nnm.ru/a/1/5/b/0/1dba2209d5a133d9e84431a5587.jpg","FireWorksSydney.jpg") ;$sImageIn = "FireWorksSydney.jpg" ; Create a GUI for the original image $hGUI1 = GUICreate("Original", 400, 300, 0, 0) GUISetState() ; Create a GUI for the zoomed image $hGUI2 = GUICreate("Ctrl + Left click to save image", 400, 300, 0, 400) GUISetState() GUIRegisterMsg(0xF, "MY_PAINT"); Register PAINT-Event 0x000F = $WM_PAINT (WindowsConstants.au3) ; Initialize GDI+ library and load image _GDIPlus_Startup() ;$hImage = _GDIPlus_ImageLoadFromFile(@MyDocumentsDir & "\GDIPlus_Image.png") $hImage = _GDIPlus_ImageLoadFromFile($sImageIn) $iX = _GDIPlus_ImageGetWidth($hImage) $iY = _GDIPlus_ImageGetHeight($hImage) _GDIPlus_BitmapSetPixel($hImage, 110, 80, 0xFF000000) ; Draw original image $hGraphic1 = _GDIPlus_GraphicsCreateFromHWND($hGUI1) ;_GDIPlus_GraphicsDrawImage($hGraphic1, $hImage, 0, 0) _GDIPlus_GraphicsDrawImageRectRect($hGraphic1, $hImage, 0, 0, $iX, $iY, 0, 0, 400, 300) ; ====== Examples calling _ImageColorRegExpReplace() ========================= ; Note colour format hex 0xBBGGRRAA - Blue, Green, Red, Alpha (transparency) ;$hImage2 = _ImageColorRegExpReplace($hImage, "FFFFFFFF", "000000FF"); Change white to black. $hImage2 = _ImageColorRegExpReplace($hImage, "(?i)([E-F][0-9A-F]){3}FF" , "000000FF" ); Change almost white to black. ;$hImage2 = _ImageColorRegExpReplace($hImage, "(000000FF)" , "FFFFFFFF" ); Change black to white ;$hImage2 = _ImageColorRegExpReplace($hImage, "(FFFFFFFF)" , "FFFFFF00" ); Change white to transparent ;$hImage2 = _ImageColorRegExpReplace($hImage, "(([A-F][0-9A-F]){3}FF)" , "000000FF" ); Change near to white to transparent ;$hImage2 = _ImageColorRegExpReplace($hImage, "(FF0000FF|(0000[A-F]{2}FF)|FFFFFFFF)", "000000FF"); Change blue, off red, white to black ;Swap red and blue channels on half the image. Image is 400x300 = 120,000 pixels. Number of pixels to replace is 60,000. ;$hImage2 = _ImageColorRegExpReplace($hImage, "(?i)([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})(FF)", "${2}${3}${1}${4}") ; Image to black and white ;$hImage2 = _ImageColorRegExpReplace($hImage, "(?i)([9A-F][0-9A-F]{5}FF)" , "FFFFFFFF" ); Change near to white to white ;$hImage2 = _ImageColorRegExpReplace($hImage2, "(?i)([0-8][0-9A-F]{5}FF)" , "000000FF" ); Change near black to black ;$hImage2 = _ImageColorRegExpReplace($hImage, "(?i)(([0-9A-D][0-9A-F]){3}FF)", "FFFFFF00"); Change near to white to white ;$hImage2 = _ImageColorRegExpReplace($hImage, _NotHexColorREPattern("FFFFFFFF", 0xC9), "FFFFFF00") ;$hImage2 = _ImageColorRegExpReplace($hImage, _ShadeVariationREPattern("606060FF", 0x9E), "FFFFFF00") ;or ;$hImage2 = _ImageColorRegExpReplace($hImage, "(?i)([0-9A-F]{2}[9A-F][0-9A-F]{3}FF)" , "FFFFFFFF" ); Change near to white to white ;$hImage2 = _ImageColorRegExpReplace($hImage2, "(?i)([0-9A-F]{2}[0-8][0-9A-F]{3}FF)" , "000000FF" ); Change near black to black ;$hImage2 = _ImageColorRegExpReplace($hImage, "(FFFFFFFF)", "000000FF"); Change near to white to black ;$hImage2 = _ImageColorRegExpReplace($hImage, _NotHexColorREPattern("808080FF", 0x77), "FFFFFF00"); Change near to white to white ;$hImage2 = _ImageColorRegExpReplace($hImage, "(.{8}) (.{8}) (.{8}) ", "\1 00000000 00000000 "); Change near to white to white ;$hImage2 = _ImageColorRegExpReplace($hImage, _ShadeVariationREPattern("0000FFFF", 0x10, False), "000000FF"); Change near to white to white ;$hImage2 = _ImageColorRegExpReplace($hImage, _ShadeVariationREPattern("606060FF", 0x90, False), "FFFFFF00"); Change near to white to white ;$hImage2 = _ImageColorRegExpReplace($hImage, "808080FF", "FFFFFF00"); ;_ImagePixelPos($hImage, "0000FFFF") ;============================================================================================== ; Draw RegExpReplace image $hGraphic2 = _GDIPlus_GraphicsCreateFromHWND($hGUI2) ;_GDIPlus_GraphicsDrawImage($hGraphic2, $hImage2, 0, 0) _GDIPlus_GraphicsDrawImageRectRect($hGraphic2, $hImage2, 0, 0, $iX, $iY, 0, 0, 400, 300) ;$hImage3 = _GDIPlus_BitmapCreateFromHBITMAP ($hImage2) ;ConsoleWrite("$hImage2 " & $hImage2 & @CRLF) ; Clean up screen shot file FileDelete(@MyDocumentsDir & "\GDIPlus_Image.jpg") ; Loop until user exits Do If _IsPressed("01") And _IsPressed("11") Then ;Left click + Ctrl key to save image Do Until Not _IsPressed("01") Local $PathFile = FileSaveDialog("Choose a name.", @ScriptDir & "\", "Images (*.bmp;*.jpg;*.png;*.gif)|All files (*.*)", 16, "ColourModify.png") If Not @error Then _GDIPlus_ImageSaveToFile($hImage2, $PathFile) ;ShellExecute(@ScriptDir & "\ColMod.png") EndIf EndIf Sleep(10) Until GUIGetMsg() = $GUI_EVENT_CLOSE ; Release resources _GDIPlus_GraphicsDispose($hGraphic1) _GDIPlus_GraphicsDispose($hGraphic2) _GDIPlus_ImageDispose($hImage) _GDIPlus_ImageDispose($hImage2) _GDIPlus_Shutdown() EndFunc ;==>_Main Func _ImagePixelPos($hImage, $iColSrch, $iCount = 0) Local $hTimer = TimerInit() Local $Reslt, $stride, $format, $Scan0, $iIW, $iIH, $hBitmap1 Local $v_BufferA, $AllPixels, $sREResult1, $sResult, $iNum,$iNum2, $i, $sRes, $y $iIW = _GDIPlus_ImageGetWidth($hImage) $iIH = _GDIPlus_ImageGetHeight($hImage) ;ConsoleWrite($iIW & " " & $iIH & @LF) $hBitmap1 = _GDIPlus_BitmapCloneArea($hImage, 0, 0, $iIW, $iIH, $GDIP_PXF32ARGB) ; Locks a portion of a bitmap for reading or writing $Reslt = _GDIPlus_BitmapLockBits($hBitmap1, 0, 0, $iIW, $iIH, BitOR($GDIP_ILMREAD, $GDIP_ILMWRITE), $GDIP_PXF32ARGB) ;Get the returned values of _GDIPlus_BitmapLockBits () $width = DllStructGetData($Reslt, "width") $height = DllStructGetData($Reslt, "height") $stride = DllStructGetData($Reslt, "stride") $format = DllStructGetData($Reslt, "format") $Scan0 = DllStructGetData($Reslt, "Scan0") $v_BufferA = DllStructCreate("byte[" & $height * $width * 4 & "]", $Scan0) ; Create DLL structure for all pixels $AllPixels = DllStructGetData($v_BufferA, 1) ;ConsoleWrite("$AllPixels, raw data, first 9 colours = " & StringRegExpReplace($AllPixels, "(.{98})(.*)", "\1") & @CRLF) ; Searches on this string - $sREResult1 whch has the prefix "0x" removed and a space put between pixels 8 characters long. $sREResult1 = StringRegExpReplace(StringTrimLeft($AllPixels, 2), "(.{8})", "\1 ") ;ConsoleWrite("$sREResult1 first 9 colours = " & StringRegExpReplace($sREResult1, "(.{81})(.*)", "\1") & @CRLF) If StringInStr($sREResult1, $iColSrch) > 0 Then StringRegExpReplace($sREResult1, "(" & $iColSrch & ")", "\1") Local $iTotalPixelsFound = @extended $sRes &= $iTotalPixelsFound & " matches found between" & @CRLF $iNum = StringInStr($sREResult1, $iColSrch, 0, 1) $y = Int(($iNum) / (9 * $width)) $sRes &= "(" & Int(((($iNum) / (9 * $width)) - $y) * $width) & "," & $y & ")" & @LF $iNum2 = StringInStr($sREResult1, $iColSrch, 0, -1) $y = Int(($iNum2) / (9 * $width)) $sRes &= " and " & @CRLF & "(" & Ceiling(((($iNum2) / (9 * $width)) - $y) * $width) & "," & $y & ")" &@LF ;ConsoleWrite($iTotalPixelsFound & @LF) ;Local $aPixs[$iTotalPixelsFound +1][2] ;$aPixs[0][0] = $iTotalPixelsFound ;For $i = 1 To $iTotalPixelsFound step 5 ; $iNum = StringInStr($sREResult1, $iColSrch, 0, $i) + 1 ; $y = Int(($iNum) / (9 * $width)) ; $sRes &= Int(($iNum) / (9 * $width)) & "," & Int(((($iNum) / (9 * $width)) - $y) * $width) & @LF ; $aPixs[$i][1] = Int(($iNum) / (9 * $width)) ; $aPixs[$i][0] = Int(((($iNum) / (9 * $width)) - $aPixs[$i][1]) * $width) ; ConsoleWrite($i & " " & $aPixs[$i][0] & " " & $aPixs[$i][1] & " " & @LF) ;Next EndIf _GDIPlus_BitmapUnlockBits($hBitmap1, $Reslt) ; releases the locked region ConsoleWrite("Time =" & TimerDiff($hTimer) & @LF) ConsoleWrite($sRes & @LF) ;_ArrayDisplay($aPixs) Return $hBitmap1 EndFunc ;==>_ImagePixelPos ; ========= _ImageColorRegExpReplace($hImage, $iColSrch, $iColNew,$iCount = 0) ===================== ;Paramerers:- ; $hImage - Handle to a Bitmap object ; $iColSrch - A regular expression pattern to compare the colours to in 0xBBGGRRAA hex colour format. ; $iColNew - The colour which will replace the regular expression matching colour.Also in 0xBBGGRRAA hex colour format. ; $iCount - [optional] The number of times to execute the replacement in the image. The default is 0. Use 0 for global replacement. ; Note :- Colour format in hex 0xBBGGRRAA ; Func _ImageColorRegExpReplace($hImage, $iColSrch, $iColNew, $iCount = 0) Local $Reslt, $stride, $format, $Scan0, $iIW, $iIH, $hBitmap1 Local $v_BufferA, $AllPixels, $sREResult1, $sResult $iIW = _GDIPlus_ImageGetWidth($hImage) $iIH = _GDIPlus_ImageGetHeight($hImage) ;ConsoleWrite($iIW & " " & $iIH & @LF) $hBitmap1 = _GDIPlus_BitmapCloneArea($hImage, 0, 0, $iIW, $iIH, $GDIP_PXF32ARGB) ; Locks a portion of a bitmap for reading or writing $Reslt = _GDIPlus_BitmapLockBits($hBitmap1, 0, 0, $iIW, $iIH, BitOR($GDIP_ILMREAD, $GDIP_ILMWRITE), $GDIP_PXF32ARGB) ;Get the returned values of _GDIPlus_BitmapLockBits () $width = DllStructGetData($Reslt, "width") $height = DllStructGetData($Reslt, "height") $stride = DllStructGetData($Reslt, "stride") $format = DllStructGetData($Reslt, "format") $Scan0 = DllStructGetData($Reslt, "Scan0") $v_BufferA = DllStructCreate("byte[" & $height * $width * 4 & "]", $Scan0) ; Create DLL structure for all pixels $AllPixels = DllStructGetData($v_BufferA, 1) ;ConsoleWrite("$AllPixels, raw data, first 9 colours = " & StringRegExpReplace($AllPixels, "(.{98})(.*)", "\1") & @CRLF) ; Searches on this string - $sREResult1 whch has the prefix "0x" removed and a space put between pixels 8 characters long. $sREResult1 = StringRegExpReplace(StringTrimLeft($AllPixels, 2), "(.{8})", "\1 ") ;ConsoleWrite("$sREResult1 first 9 colours = " & StringRegExpReplace($sREResult1, "(.{81})(.*)", "\1") & @CRLF) If StringInStr($iColNew, "0x") > 0 Then $iColNew = StringReplace($iColNew, "0x", ""); Remove "0x" not needed ; StringRegExpReplace performed and white spaces removed $sResult = StringStripWS(StringRegExpReplace($sREResult1, $iColSrch, $iColNew, $iCount), 8) ; Replace "0x" prefix and set modified data back to DLL structure, $v_BufferA DllStructSetData($v_BufferA, 1, "0x" & $sResult) _GDIPlus_BitmapUnlockBits($hBitmap1, $Reslt) ; releases the locked region Return $hBitmap1 EndFunc ;==>_ImageColorRegExpReplace Func MY_PAINT($hWnd, $msg, $wParam, $lParam) If $hWnd = $hGUI1 Then _GDIPlus_GraphicsDrawImageRectRect($hGraphic1, $hImage, 0, 0, $iX, $iY, 0, 0, 400, 300) ; _GDIPlus_GraphicsDrawImageRect($hGraphic1, $hImage, 0, 0, $width, $height) If $hWnd = $hGUI2 Then _GDIPlus_GraphicsDrawImageRectRect($hGraphic2, $hImage2, 0, 0, $iX, $iY, 0, 0, 400, 300) ; _GDIPlus_GraphicsDrawImageRect($hGraphic2, $hImage2, 0, 0, $width, $height) Return $GUI_RUNDEFMSG EndFunc ;==>MY_PAINT ;======================== _ShadeVariationREPattern =========================== ; Description - Checks if a 24bit color matches a specified color plus or minus a shade varation. ; Parameters ; $iColor - A 'RGB' or 'ARGB' color to test. ; $ShadeVariation - An integer added to and substracted from each color channel of the color, $iColPlusShade. ; $ARGB - If $iColor Format is in 0xAARRGGBB hex format then $ARGB = True is default. ; If false then $iColor Format is in 0xBBGGRRAA hex format. ; Returns 1 (matched) or 0 (no match) ; Func _ShadeVariationREPattern($iColor, $ShadeVariation, $ARGB = True) Local $Pattern, $iColSrchMin, $iColSrchMax If $ARGB Then $iColor = "0x" & StringRegExpReplace(Hex($iColor, 8), "(..)(..)(..)(..)", "\4\3\2\1") ;ConsoleWrite(hex($iColor, 8) & @CRLF) Local $aColor = StringRegExp(Hex("0x" & $iColor, 8), "(.{2})", 3) ;_ArrayDisplay($aColor) For $i = 0 To UBound($aColor) - 2 $Pattern &= "(" $iColSrchMin = Hex(Int(((("0x" & $aColor[$i]) - $ShadeVariation) > 0) ? (("0x" & $aColor[$i]) - $ShadeVariation) : 0), 2) $iColSrchMax = Hex(Int(((("0x" & $aColor[$i]) + $ShadeVariation) < 256) ? (("0x" & $aColor[$i]) + $ShadeVariation) : 255), 2) ;$iColSrchMax = "0x" & Hex((Dec(Hex("0x" & $aColor[$i], 2)) + $ShadeVariation + 1) * ((Dec(Hex("0x" & $aColor[$i], 2)) + $ShadeVariation + 1) < 255) + 255, 2) ConsoleWrite($iColSrchMin & " to " & $iColSrchMax & @CRLF) For $n = "0x" & $iColSrchMin To "0x" &$iColSrchMax $Pattern &= Hex($n, 2) & "|" Next $Pattern = StringTrimRight($Pattern, 1) & ")" Next ;ConsoleWrite($Pattern & "(FF)" & @LF) Return $Pattern & "(FF)" EndFunc ;==>_ShadeVariationREPattern Func _NotHexColorREPattern($In, $iVariation = 0x00) Local $aPat[16][16] = [ _ ; 2 3 4 5 6 7 8 9 A B C D E T ["1-9A-F", "2-9A-F" , "3-9A-F" , "4-9A-F" , "5-9A-F" , "6-9A-F" , "7-9A-F" , "8-9A-F" , "9A-F" , "A-F" , "B-F" , "C-F" , "D-F" , "EF" , "F" , "."], _ ;0 ["2-9A-F", "02-9A-F", "03-9A-F" , "04-9A-F" , "05-9A-F" , "06-9A-F" , "07-9A-F" , "08-9A-F" , "09A-F" , "0A-F" , "0B-F" , "0B-F" , "0C-F" , "0D-F" , "0EF" , "0"], _ ;1 ["3-9A-F", "03-9A-F", "013-9A-F", "014-9A-F" , "015-9A-F" , "016-9A-F" , "017-9A-F" , "018-9A-F" , "019A-F" , "01A-F" , "01B-F" , "01C-F" , "01D-F" , "01EF" , "01F" , "01"], _ ;2 ["4-9A-F", "04-9A-F", "014-9A-F", "0-24-9A-F", "0-25-9A-F", "0-26-9A-F", "0-27-9A-F", "0-28-9A-F", "0-29A-F", "0-2A-F" , "0-2B-F" , "0-2C-F" , "0-2D-F" , "0-2EF" , "0-2F" , "0-2"], _ ;3 ["5-9A-F", "05-9A-F", "015-9A-F", "0-25-9A-F", "0-35-9A-F", "0-36-9A-F", "0-37-9A-F", "0-38-9A-F", "0-39A-F", "0-3A-F" , "0-3B-F" , "0-3C-F" , "0-3D-F" , "0-3EF" , "0-3F" , "0-3"], _ ;4 ["6-9A-F", "06-9A-F", "016-9A-F", "0-26-9A-F", "0-36-9A-F", "0-46-9A-F", "0-47-9A-F", "0-48-9A-F", "0-49A-F", "0-4A-F" , "0-4B-F" , "0-4C-F" , "0-4D-F" , "0-4FF" , "0-4F" , "0-4"], _ ;5 ["7-9A-F", "07-9A-F", "017-9A-F", "0-27-9A-F", "0-37-9A-F", "0-47-9A-F", "0-57-9A-F", "0-58-9A-F", "0-59A-F", "0-5A-F" , "0-5B-F" , "0-5C-F" , "0-5D-F" , "0-5EF" , "0-5F" , "0-5"], _ ;6 ["89A-F" , "089A-F" , "0189A-F" , "0-289A-F" , "0-389A-F" , "0-489A-F" , "0-589A-F" , "0-689A-F" , "0-69A-F", "0-6A-F" , "0-6B-F" , "0-6C-F" , "0-6D-F" , "0-6EF" , "0-6F" , "0-6"], _ ;7 ["9A-F" , "09A-F" , "019A-F" , "0-29A-F" , "0-39A-F" , "0-49A-F" , "0-59A-F" , "0-69A-F" , "0-79A-F", "0-7A-F" , "0-7B-F" , "0-7C-F" , "0-7D-F" , "0-7EF" , "0-7F" , "0-79"], _ ;8 ["A-F" , "0A-F" , "01A-F" , "0-2A-F" , "0-3A-F" , "0-4A-F" , "0-5A-F" , "0-6A-F" , "0-7A-F" , "0-8A-F" , "0-8B-F" , "0-8C-F" , "0-8D-F" , "0-8EF" , "0-8F" , "0-8"], _ ;9 ["B-F" , "0B-F" , "01B-F" , "0-2B-F" , "0-3B-F" , "0-4B-F" , "0-5B-F" , "0-6B-F" , "0-7B-F" , "0-8B-F" , "0-9B-F" , "0-9C-F" , "0-9D-F" , "0-9EF" , "0-9F" , "0-9"], _ ;A ["C-F" , "0C-F" , "01C-F" , "0-2C-F" , "0-3C-F" , "0-4AC-F" , "0-5C-F" , "0-6C-F" , "0-7C-F" , "0-8C-F" , "0-9C-F" , "0-9AC-F" , "0-9AD-F" , "0-9AEF" , "0-9AF" , "0-9AC"], _ ;B ["D-F" , "0D-F" , "01D-F" , "0-2D-F" , "0-3D-F" , "0-4D-F" , "0-5D-F" , "0-6D-F" , "0-7D-F" , "0-8D-F" , "0-9D-F" , "0-9AD-F" , "0-9ABD-F", "0-9ABEF" , "0-9ABF" , "0-9AB"], _ ;C ["EF" , "0EF" , "01EF" , "0-2EF" , "0-3EF" , "0-4EF" , "0-5EF" , "0-6EF" , "0-7EF" , "0-8EF" , "0-9EF" , "0-9AEF" , "0-9ABEF" , "0-9A-CEF", "0-9A-CF", "0-9A-C"], _ ;D ["F" , "0F" , "01F" , "0-2F" , "0-3F" , "0-4F" , "0-5F" , "0-6F" , "0-7F" , "0-8F" , "0-9F" , "0-9AF" , "0-9ABF" , "0-9A-CF" , "0-9A-DF", "0-9A-D"], _ ;E ["." , "0" , "01" , "0-2" , "0-3" , "0-4" , "0-5" , "0-6" , "0-7" , "0-8" , "0-9" , "0-9A" , "0-9AB" , "0-9A-C" , "0-9A-D" , "0-9A-E"]] ;F Local $sRet, $aArr, $iLower, $iUpper, $aVari[8][2] If IsNumber($In) Then $In = Hex($In, 8) If StringLeft($In, 2) == "0x" Then $In = StringTrimLeft($In, 2) $aArr = StringRegExp($In, "(..)", 3) For $i = 0 To 2 $iLower = Hex(Int(((("0x" & $aArr[$i]) - $iVariation) > 0) ? (("0x" & $aArr[$i]) - $iVariation) : 0), 2) $iUpper = Hex(Int(((("0x" & $aArr[$i]) + $iVariation) < 256) ? (("0x" & $aArr[$i]) + $iVariation) : 255), 2) $aVari[$i * 2][0] = "0x" & StringLeft($iLower, 1) $aVari[$i * 2][1] = "0x" & StringLeft($iUpper, 1) $aVari[$i * 2 + 1][0] = "0x" & StringRight($iLower, 1) $aVari[$i * 2 + 1][1] = "0x" & StringRight($iUpper, 1) Next ;Alpha channel $aVari[6][0] = "0xF" $aVari[6][1] = "0xF" $aVari[7][0] = "0xF" $aVari[7][1] = "0xF" For $i = 0 To UBound($aVari) - 1 $sRet &= "([0-9A-F]{" & $i & "}[" & $aPat[$aVari[$i][0]][$aVari[$i][1]] & "][0-9A-F]{" & (7 - $i) & "})|" Next ;if StringInStr(StringRegExpReplace(StringTrimRight($sRet, 1), "(\Q[0-9A-F]{0}\E)|(\Q{1}\E)", ""),"[]") Then ; MsgBox(0,"Error", " ConsoleWrite(StringRegExpReplace(StringTrimRight($sRet, 1), "(\Q[0-9A-F]{0}\E)|(\Q{1}\E)", "") & @LF) ;_ArrayDisplay($aVari) Return StringRegExpReplace(StringTrimRight($sRet, 1), "(\Q[0-9A-F]{0}\E)|(\Q{1}\E)", "") ; |(\[\]) ;Return StringTrimRight($sRet, 1) EndFunc ;==>_NotHexColorREPattern  Edited May 6, 2017 by Malkey Updated example Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now