Remove specific color from image Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Qhimin
Posts: 16
Joined: 30 Nov 2020, 19:24

Remove specific color from image

Post by Qhimin » 13 Feb 2021, 07:06

Hello,


I have been trying to remove all the black pixels from a grayscale image using GDIP, but no success.
I have already tryed to use

Code: Select all

Gdip_GraphicsClear(pBitmap, 0x00000000)
and also

Code: Select all

Gdip_SetBitmapTransColor(pBitmap, 0x000000)
but both didn't work.

Here's the code:

Code: Select all

If !pToken := Gdip_Startup()
    MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system

fileName = test.png
pBitmap:= Gdip_CreateBitmapFromFile(fileName)
Width := Gdip_GetImageWidth(pBitmap), Height := Gdip_GetImageHeight(pBitmap) 
pBitmapGrayscale := Gdip_CreateBitmap(Width,Height)  
G := Gdip_GraphicsFromImage(pBitmapGrayscale) 
Gdip_GraphicsClear(G, 0x00000000)
Matrix = 0.299|0.299|0.299|0|0|0.587|0.587|0.587|0|0|0.114|0.114|0.114|0|0|0|0|0|1|0|0|0|0|0|1 
Gdip_DrawImage(G, pBitmap, 0, 0, Width, Height, 0, 0, Width, Height, Matrix)
Gdip_SaveBitmapToFile(pBitmap1, "pBitmapGrayscale.png")
Gdip_DisposeImage(pBitmap)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
The grayscale image is being saved and it's ok, but still with black pixels in it.

Can somebody help me, please?

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Remove specific color from image

Post by mikeyww » 13 Feb 2021, 07:37

I haven't tried it, but you will find examples if you search the forum. The ones that I saw used CreateCompatibleDC().

teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Remove specific color from image

Post by teadrinker » 13 Feb 2021, 12:12

Qhimin wrote: remove all the black pixels from a grayscale image
What do you mean by this? What result are you expecting to see? Maybe, you mean "replace black pixels with transparent ones"?

Qhimin
Posts: 16
Joined: 30 Nov 2020, 19:24

Re: Remove specific color from image

Post by Qhimin » 13 Feb 2021, 15:10

teadrinker wrote:
13 Feb 2021, 12:12
Qhimin wrote: remove all the black pixels from a grayscale image
What do you mean by this? What result are you expecting to see? Maybe, you mean "replace black pixels with transparent ones"?
I have a image with like 2000x2000 pixels and most of them are black pixels. To search inside of this image I take a screenshot from a region on my screen and if there are many black pixels in this screenshot image the search become very slow, which make sense because of the amount of pixels to compare. If I could remove the black pixels, or like you said, replace them with transparent ones, the results could be faster. None of the functions that I mentioned above worked, dunno if I was using them right, also.
Sandly I can't resize the source image nor remove the black pixels from the file.

teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Remove specific color from image

Post by teadrinker » 13 Feb 2021, 16:04

I'm not an expert in GDI+, I'd just replace black pixels directly:

Code: Select all

SetBatchLines, -1

inputFilePath := "C:\Users\User\Desktop\GrayScale.png"
outputFilePath := "C:\Users\User\Desktop\ModifiedGrayScale.png"

pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmapFromFile(inputFilePath)
Gdip_GetImageDimensions(pBitmap, w, h)
Gdip_LockBits(pBitmap, 0, 0, w, h, stride, pPix, bitmapData)

Loop % h {
   i := A_Index - 1
   Loop % w {
      offset := pPix + stride*i + 4*(A_Index - 1)
      if NumGet(offset + 0, "UInt") = 0xFF000000
         NumPut(0, offset + 0, "UInt")
   }
}
Gdip_UnlockBits(pBitmap, bitmapData)
Gdip_SaveBitmapToFile(pBitmap, outputFilePath)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)

Gui, Color, Red
Gui, Add, Pic, w200 h-1, % inputFilePath
Gui, Add, Pic, w200 h-1, % outputFilePath
Gui, Show
Return

GuiClose:
   ExitApp
Perhaps there is a smarter way.

teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Remove specific color from image  Topic is solved

Post by teadrinker » 13 Feb 2021, 18:15

The same, but faster:

Code: Select all

SetBatchLines, -1

inputFilePath := "C:\Users\User\Desktop\GrayScale.png"
outputFilePath := "C:\Users\User\Desktop\ModifiedGrayScale.png"

pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmapFromFile(inputFilePath)
Gdip_SetBitmapTransColor(pBitmap, 0)
Gdip_SaveBitmapToFile(pBitmap, outputFilePath)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)

Qhimin
Posts: 16
Joined: 30 Nov 2020, 19:24

Re: Remove specific color from image

Post by Qhimin » 14 Feb 2021, 07:16

teadrinker wrote:
13 Feb 2021, 18:15
The same, but faster:

Code: Select all

SetBatchLines, -1

inputFilePath := "C:\Users\User\Desktop\GrayScale.png"
outputFilePath := "C:\Users\User\Desktop\ModifiedGrayScale.png"

pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmapFromFile(inputFilePath)
Gdip_SetBitmapTransColor(pBitmap, 0)
Gdip_SaveBitmapToFile(pBitmap, outputFilePath)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
Thank you very much @teadrinker, you are awesome ! It worked perfectly!

User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Remove specific color from image

Post by mikeyww » 14 Feb 2021, 07:56

Very nice one!


Post Reply

Return to “Ask for Help (v1)”