GDIP filter all but a certain color out of an image Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
grimboto
Posts: 53
Joined: 09 Jul 2014, 19:20

GDIP filter all but a certain color out of an image

05 Feb 2019, 17:21

hey i have a little function that launches windows screen sketch and then saves the image to file after the copy to clipboard button is pressed. I will only ever be using the red pen to draw on these images. Just looking for a way to save another copy of the image that only contains the lines i've drawn on the screen. See images below

Before:
Sketch.png
Sketch.png (1.47 MiB) Viewed 2641 times
After:
Sketch2.png
Sketch2.png (116.65 KiB) Viewed 2641 times
it doesn't matter if there are small artifacts around the drawing as long as you can clearly see all the lines.


below is the code i'm currently using

Code: Select all



#include %A_ScriptDir%\Gdip_All.ahk
LaunchScreenSketch(A_ScriptDir . "\test.png")
return

LaunchScreenSketch(FileName){
	;save clipboard
	currentClipboard := clipboardAll
	clipboard := 
	clipStatus := ""
	;launch screen sketch
	Run, ms-penworkspace://Capture
	
	CoordMode, Mouse, Screen
	;get current mousePos
	MouseGetPos, currentX, CurrentY
	
	while(Title != "Windows Ink Workspace"){
		
		;Move mouse ontop of windows ink work space
		MouseMove, 10, 10 , 0
		;get window HWND
		MouseGetPos, , , WindowsInkHWND
		;get window title
		WinGetTitle, Title, % "Ahk_id " WindowsInkHWND
		sleep 50
	}
	;return Mouse to previous position
	MouseMove, %currentX%, %currentY% , 0
	
	while(!clipStatus && winexist("Ahk_id " WindowsInkHWND)){
		;wait for clipboard to contain something or windows ink to close
		sleep 40
		clipStatus := clipboardAll
	}

	;if something is on the clipboard save it as a png
	if(clipStatus){
		;send esc to close screen sketch, keep sending until it is closed
		while(winexist("Ahk_id " WindowsInkHWND)){
			sleep 300
			;sendinput {esc}
			WinClose , Ahk_id %WindowsInkHWND%
		}
				
		;save image from clipboard
		pToken := Gdip_Startup()
		pBitmap := Gdip_CreateBitmapFromClipboard()
		Gdip_SaveBitmapToFile(pBitmap, FileName)
		
		; ************************************************************************
		;code here to filter out all colors except the red pen and save it as a different filename
		; ************************************************************************
		
		Gdip_DisposeImage(pBitmap)
		Gdip_Shutdown(pToken)
		clipboard := currentClipboard
		return FileName
	}
	;sleep 500
	;reset clipboard
	clipboard := currentClipboard

	return 0
	

}
any help would be appreciated.
thanks grimboto
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: GDIP filter all but a certain color out of an image  Topic is solved

05 Feb 2019, 18:19

Code: Select all

; ************************************************************************
w := Gdip_GetImageWidth(pBitmap)
h := Gdip_GetImageHeight(pBitmap)

Gdip_LockBits(pBitmap, 0, 0, w, h, stride, scan0, bmData)

Loop % w
{
	x := A_Index - 1

	Loop % h
	{
		y := A_Index - 1

		ARGB := Gdip_GetLockBitPixel(scan0, x, y, stride)
		Gdip_FromARGB(ARGB, "", R, G, B)

		if (R > (Max(G, B) * 3) && Abs(G - B) < 10)
			Gdip_SetLockBitPixel(0xFFFF0000, scan0, x, y, stride)
		else
			Gdip_SetLockBitPixel(0xFFFFFFFF, scan0, x, y, stride)
	}
}

Gdip_UnlockBits(pBitmap, bmData)
Gdip_SaveBitmapToFile(pBitmap, SubStr(FileName, 1, -4) "_onlyred.png")
; ************************************************************************
grimboto
Posts: 53
Joined: 09 Jul 2014, 19:20

Re: GDIP filter all but a certain color out of an image

05 Feb 2019, 18:30

thanks that works well, its a little slow but gets the job done.

cheers
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: GDIP filter all but a certain color out of an image

06 Feb 2019, 16:38

sorry, no faster method for u. forum no like
jk
e, since posting works now again, for posterity:

Code: Select all

; ************************************************************************
w := Gdip_GetImageWidth(pBitmap)
h := Gdip_GetImageHeight(pBitmap)

pBitmap_RedAndWhite := Gdip_CreateBitmap(w, h)
pGraphics_RedAndWhite := Gdip_GraphicsFromImage(pBitmap_RedAndWhite)

pBitmap_OnlyRed := Gdip_CreateBitmap(w, h)
pGraphics_OnlyRed := Gdip_GraphicsFromImage(pBitmap_OnlyRed)

DllCall("gdiplus\GdipCreateImageAttributes", "Ptr*", ImageAttr)
LowColor := Gdip_ToARGB(0, 0, 0, 0)
HighColor := Gdip_ToARGB(255, 225, 255, 255)
DllCall("gdiplus\GdipSetImageAttributesColorKeys", "Ptr", ImageAttr, "Int", 1, "Int", true, "UInt", LowColor, "UInt", HighColor)
DllCall("gdiplus\GdipDrawImageRectRect"
	  , "Ptr", pGraphics_RedAndWhite
	  , "Ptr", pBitmap
	  , "Float", 0
	  , "Float", 0
	  , "Float", w
	  , "Float", h
	  , "Float", 0
	  , "Float", 0
	  , "Float", w
	  , "Float", h
	  , "Int", 2
	  , "Ptr", ImageAttr
	  , "Ptr", 0
	  , "Ptr", 0)

LowColor := Gdip_ToARGB(255, 220, 220, 220)
HighColor := Gdip_ToARGB(255, 255, 255, 255)
DllCall("gdiplus\GdipSetImageAttributesColorKeys", "Ptr", ImageAttr, "Int", 1, "Int", true, "UInt", LowColor, "UInt", HighColor)
Gdip_GraphicsClear(pGraphics_OnlyRed, 0xFFFFFFFF)
DllCall("gdiplus\GdipDrawImageRectRect"
	  , "Ptr", pGraphics_OnlyRed
	  , "Ptr", pBitmap_RedAndWhite
	  , "Float", 0
	  , "Float", 0
	  , "Float", w
	  , "Float", h
	  , "Float", 0
	  , "Float", 0
	  , "Float", w
	  , "Float", h
	  , "Int", 2
	  , "Ptr", ImageAttr
	  , "Ptr", 0
	  , "Ptr", 0)

Gdip_SaveBitmapToFile(pBitmap_OnlyRed, SubStr(FileName, 1, -4) "_onlyred.png")

Gdip_DisposeImageAttributes(ImageAttr)
Gdip_DisposeImage(pBitmap_RedAndWhite)
Gdip_DeleteGraphics(pGraphics_RedAndWhite)
Gdip_DisposeImage(pBitmap_OnlyRed)
Gdip_DeleteGraphics(pGraphics_OnlyRed)
; ************************************************************************
Last edited by swagfag on 07 Feb 2019, 11:39, edited 1 time in total.
grimboto
Posts: 53
Joined: 09 Jul 2014, 19:20

Re: GDIP filter all but a certain color out of an image

06 Feb 2019, 22:22

WOW so much faster thanks a lot
Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: GDIP filter all but a certain color out of an image

20 Mar 2022, 13:43

I need exactly the same, but I have orange text written on a grayscale image. I want only the orange text to stay and the rest to be white.
I tried the skript but autohotkey crashes when executing it.

any idea why?

greets and thanks Simon
Spitzi
Posts: 313
Joined: 24 Feb 2022, 03:45

Re: GDIP filter all but a certain color out of an image

20 Mar 2022, 16:17

Sorry, my mistake. The script works nicely. I had forgotten to add

Code: Select all

Gdip_Shutdown(pToken)
at the end.

Is it possible to change the final color from red/orange to black?

Thanks and Greets Simon

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, haomingchen1998, mikeyww, Oblomov228, OrangeCat, RussF, sanmaodo and 256 guests