Img2Clicks, translate color values <n of img into mouse clicks

Post your working scripts, libraries and tools
John
Posts: 77
Joined: 10 Apr 2014, 08:59

Img2Clicks, translate color values <n of img into mouse clicks

16 Nov 2015, 18:08

Image

Wrote a script to an ask for help topic (https://www.autohotkey.com/boards/viewt ... =5&t=10405), the original was pretty slow and crummy but, it was quite fun so I went on and finished the script using GDIP.ahk instead of gdi32 and runs pretty smoothly now.
Basically what it does is that it takes in an image, converts it to a bitmap, finds all colors under the given treshold and once done will translate to mouse clicks (in paint or.. desktop to accidentally mess up everything :'<) after a button click to 'load the image' and a mmb click to start the action.

Not sure what it's good for but, if nothing else it's entertaining to watch your mouse print a simplified version of an image on paint :)

Code: Select all

#include, Gdip.ahk
SetBatchLines,-1
SetWinDelay,-1
SetMouseDelay,0
Gui, Add, Button, x2 y0 w190 h30 gLoadImage, Load image
Gui, Add, GroupBox, x2 y30 w190 h50, Color treshold
Gui, Add, Slider, x12 y50 w140 h28 range0-255 vSetProgress gprogressLabel, 80
Gui, Add, Edit, x152 y50 w30 h20 vprogressEdit gprogressLabel, 80
Gui, Add, Progress, x2 y80 w0 h30 range0-100 vloadProgress, 0
Gui, Add, Button, x2 y80 w190 h30 vActivateImage_arr vactivate gactivateButton,
Gui, Add, Picture, x2 y110 w190 h190 hwndhpControl vtempImg,
gui, +alwaysontop
Gui, Show, x0 y0 w194 h80, Img2Clicks
return

guiclose:
guiescape:
exitapp

progressLabel:
GuiControlGet,%a_guicontrol%
if (a_guicontrol == "SetProgress")
	guicontrol,,progressEdit, % %a_guicontrol%
else
	guicontrol,,SetProgress, % %a_guicontrol%
return

LoadImage:
if (!gdip) {
	gdip := Gdip_Startup()
}	
FileSelectFile,img_src
gui 2:add, picture, vsrc_, %img_src%
GuiControlGet, src_, 2:Pos
gui 2:destroy
if (src_w > src_h)
	src_h := src_h*(190/src_w), src_w := 190, src_x := 2
else if (src_h > src_w)
	src_w := src_w*(190/src_h), src_h := 190, src_x := (194-src_w)/2
guicontrol,,tempImg, %img_src%
guicontrol,move,tempImg, % "x" src_x " w" src_w " h" src_h
gui,show, % "h" 112+src_h
clipboard := img_src
if (ErrorLevel)
	return
guicontrol,move,loadProgress, w190
guicontrol,move,activate, w0
GuiControlGet,SetProgress
arr := {}
pBmp := Gdip_CreateBitmapFromFile(img_src)
image_width := Gdip_GetImageWidth(pBmp)
image_height := Gdip_GetImageHeight(pBmp)
lockBits := Gdip_LockBits(pBmp,0,0,image_width-1,image_height-1,Stride,Scan0,BitmapData)
loop % image_height {
	y_index := a_index
	loop % image_width {
		
		Gdip_FromARGB(Get_LockBitPixelRGB(Scan0,a_index-1,y_index-1,Stride),A,R,G,B)
		current_progress := round(((((y_index-1)*image_width)+a_index)/(image_height*image_width))*100)
		if (current_progress > prev_progress) {
			guicontrol,,loadProgress, % current_progress
			prev_progress := current_progress
		}
		arr[a_index, y_index] := ((r+g+b)/3 < SetProgress) ? 1 : 0
	}
}
Gdip_UnlockBits(pBmp, BitmapData)
Gdip_DisposeImage(pBmp)
guicontrol,move,loadProgress, w0
guicontrol,,activate, Activate image
guicontrol,move,activate, w190
return

activateButton:
if (!arr)
	return
loop {
	if (GetKeyState("MButton","P")) {
		break
	}
	else if (GetKeyState("Space","P")) {
		return
	}
	Tooltip, Image loaded`, waiting for mmb`, space to cancel.
	sleep, 50
}
tooltip,
guicontrol,,activate,Press space to abort.
mousegetpos,mx,my
loop % arr[1].MaxIndex() {
	y_index := a_index
	loop % arr.MaxIndex() {
		if (GetKeyState("Space","P"))
			return
		if (arr[a_index, y_index]) {
			draw_x := a_index+mx
			draw_y := y_index+my
			click left %draw_x%, %draw_y%
			clicked++
		}
	}
	sleep, % clicked*5
	clicked := 0
}
guicontrol,,activate, Activate image
return

Get_LockBitPixelRGB(Scan0,x,y,Stride) {
	f := Gdip_GetLockBitPixel(Scan0, x, y, Stride)
	SetFormat, Integer, H
	t := 0x00ffffff & f
	SetFormat, Integer, D
	return t
}
User avatar
WAZAAAAA
Posts: 75
Joined: 13 Jan 2015, 19:48

Re: Img2Clicks, translate color values <n of img into mouse clicks

05 Dec 2016, 21:45

This is amazing. Having a lot of fun using it on DoodleToo haha
http://www.doodletoo.com/#/white

My only complaint is that completely transparent pixels should not be drawn.

Drag'n'drop support would be great too.
YOU'RE NOT ALEXANDER

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 63 guests