Searching color from images

Creating macros. Working with QM.

Searching color from images

Postby rothe on Wed Jul 04, 2012 11:12 am

Hello! I searched QM manual and forum but didn't find solution to my small and trivial problem.

1) I am using CaptureImageOnScreen function to save my screen image to bmp file (2 times per second) and the size of the file is ~6MB. Is it possible to save directly to some other format so that it would not take so much space? png?
2) Now when I have saved the images to different files - is it possible to recognize the color of certain coordinates of that file without opening the files in viewer? All commands and functions like pixel/FindColor/scan are for opened screen recognizing...
rothe
 
Posts: 20
Joined: Sun May 13, 2007 5:08 pm

Re: Searching color from images

Postby Gintaras on Wed Jul 04, 2012 12:34 pm

Examples with GflAx component.
viewtopic.php?f=2&t=893
Need it to save/load png.

Macro capture image and save in png format     ?
;file
str sFile="$desktop$\qm test png.png"

;________________________

;capture to memory
__GdiHandle hb
CaptureImageOnScreen(0 0 ScreenWidth ScreenHeight "" hb)

;save as png
typelib GflAx {059321F1-207A-47A7-93A1-29CDF876FDD3} 1.0
GflAx.GflAx g._create
g.SetPicture(BitmapHandleToIPicture(hb))
g.SaveFormat=GflAx.AX_PNG ;;use other constants to save in other formats
g.SaveBitmap(_s.expandpath(sFile))

;________________________

;show the image
run sFile



Macro get color from png file     ?
;file, pixel
str sFile="$desktop$\qm test png.png"
int x(8) y(100)
;int x(xm) y(ym) ;;from mouse

;________________________

;open file
typelib GflAx {059321F1-207A-47A7-93A1-29CDF876FDD3} 1.0
GflAx.GflAx g._create
g.LoadBitmap(_s.expandpath(sFile))

;get pixel color
int color=g.GetColorAt(x y)
out "0x%06X" color



Macro find color or image in png file     ?
;file, pixel
str sFile="$desktop$\qm test png.png"
;int color=0xFFE0C0
int color=pixel(xm ym) ;;color from mouse

;________________________

;open file
typelib GflAx {059321F1-207A-47A7-93A1-29CDF876FDD3} 1.0
GflAx.GflAx g._create
g.LoadBitmap(_s.expandpath(sFile))

;get bitmap handle
IPicture p=g.GetPicture
int hb; p.get_Handle(hb)

;find color in bitmap (in memory)
RECT r
if scan(F"color:{color}" hb r 0x280)
,out "color 0x%06X found at x=%i y=%i" color r.left r.top
else out "not found"

;to find image: if scan("file.bmp" hb r 0x280)



Function BitmapHandleToIPicture     ?
;/
function'IPicture hBitmap

;Creates IPicture (OLE picture object) from Windows bitmap handle.


IPicture ip
PICTDESC pd.cbSizeofstruct=sizeof(pd)
pd.picType=PICTYPE_BITMAP
pd.bmp.hbitmap=hBitmap

OleCreatePictureIndirect(&pd uuidof(IPicture) 0 &ip)
ret ip

Gintaras
Site Admin
 
Posts: 10385
Joined: Thu Dec 19, 2002 7:36 pm
Location: Lithuania

Re: Searching color from images

Postby Gintaras on Wed Jul 04, 2012 4:40 pm

Examples with GDI+.
Does not require external components, just import GDI+.qml from viewtopic.php?f=2&t=4321
Need it to save/load png.

Macro capture image and save in png format, using GDI+     ?
;file
str sFile="$desktop$\qm test png gdip.png"

;________________________

;capture to memory
__GdiHandle hb
CaptureImageOnScreen(0 0 ScreenWidth ScreenHeight "" hb)

;save as png
#compile "__Gdip"
GdipBitmap im
if(!im.FromHBITMAP(hb)) end "error"
if(!im.Save(sFile)) end "error"

;________________________

;show the image
run sFile


;NOTES
;GDI+ may be unavailable on Windows 2000.
;Png file created with GDI+ is 33% bigger than with GflAx.



Macro get color from png file, using GDI+     ?
;file, pixel
str sFile="$desktop$\qm test png gdip.png"
;int x(8) y(100)
int x(xm) y(ym) ;;from mouse

;________________________

;open file
#compile "__Gdip"
GdipBitmap im
if(!im.FromFile(sFile)) end "error"

;get pixel color
int color
GDIP.GdipBitmapGetPixel(+im x y &color)
color=ColorToARGB(color 0) ;;0xAARRGGBB to 0xBBGGRR
out "0x%06X" color



Macro find color or image in png file, using GDI+     ?
;file, pixel
str sFile="$desktop$\qm test png.png"
;int color=0xFFE0C0
int color=pixel(xm ym) ;;color from mouse

;________________________

;open file
#compile "__Gdip"
GdipBitmap im
if(!im.FromFile(sFile)) end "error"

;get bitmap handle
__GdiHandle hb=im.GetHBITMAP

;find color in bitmap (in memory)
RECT r
if scan(F"color:{color}" hb r 0x280)
,out "color 0x%06X found at x=%i y=%i" color r.left r.top
else out "not found"

;to find image: if scan("file.bmp" hb r 0x280)



Member function GdipBitmap.FromHBITMAP     ?
function'GDIP.GpBitmap* hBitmap [hPalette]

;Create this bitmap from GDI bitmap.


if(!GdipInit) ret
Delete

_hresult=GDIP.GdipCreateBitmapFromHBITMAP(hBitmap hPalette +&m_i)
ret +m_i

Gintaras
Site Admin
 
Posts: 10385
Joined: Thu Dec 19, 2002 7:36 pm
Location: Lithuania

Re: Searching color from images

Postby r0n on Fri Aug 04, 2017 7:43 pm

Is it possible to modify the below example to get the clipboard contents which is bitmap and then convert it to JPG?

Macro Macro16     ?
;file
str sFile="$desktop$\qm test jpg gdip.jpg"

;________________________

;capture to memory
__GdiHandle hb
CaptureImageOnScreen(0 0 ScreenWidth ScreenHeight "" hb)

;save as png
#compile "__Gdip"



GdipBitmap im
if(!im.FromHBITMAP(hb)) end "error"
if(!im.Save(sFile)) end "error"

;________________________

;show the image
run sFile
r0n
Active Member
 
Posts: 498
Joined: Tue Jul 24, 2012 5:47 am

Re: Searching color from images

Postby Gintaras on Fri Aug 04, 2017 7:56 pm

Macro Macro2900     ?
;file
str sFile="$desktop$\qm test jpg gdip.jpg"

;________________________

;get clipboard bitmap
if(!OpenClipboard(0)) end "failed" ;;TODO: wait until succeeds
_i=GetClipboardData(CF_BITMAP)
__GdiHandle hb; if(_i) hb=CopyImage(_i 0 0 0 LR_CREATEDIBSECTION)
CloseClipboard
if(_i=0) end "no bitmap in clipboard"

;save as jpg
#compile "__Gdip"



GdipBitmap im
if(!im.FromHBITMAP(hb)) end "error"
if(!im.Save(sFile)) end "error"

;________________________

;show the image
run sFile

Gintaras
Site Admin
 
Posts: 10385
Joined: Thu Dec 19, 2002 7:36 pm
Location: Lithuania

Re: Searching color from images

Postby r0n on Fri Aug 04, 2017 9:04 pm

Thank you!
r0n
Active Member
 
Posts: 498
Joined: Tue Jul 24, 2012 5:47 am


Return to General

Who is online

Users browsing this forum: No registered users and 2 guests