Jump to content

Quickly replace colors of an image with "gdi"


Recommended Posts

#include <GDIPlus.au3>

Example()

Func Example()
    Local $hImage1, $hGraphics
    Local Const $iWidth = 100, $iHeight = 100
    
    ; Initialize GDI+ library
    _GDIPlus_Startup()

    $hImage1 = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\p0.bmp")
    
    ; Draw one image in another
    $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage1)
    
    Local $aRemapTable[4][2]
    $aRemapTable[0][0] = 3
    $aRemapTable[1][0] = 0xFF2D1200 ;Old Color1
    $aRemapTable[1][1] = 0xFF000000 ;New Color1
    $aRemapTable[2][0] = 0xFF491D00 ;Old Color2
    $aRemapTable[2][1] = 0xFF000000 ;New Color2
    $aRemapTable[3][0] = 0xFF381600 ;Old Color3
    $aRemapTable[3][1] = 0xFF000000 ;New Color3        
    
    Local $hIA = _GDIPlus_ImageAttributesCreate()
    _GDIPlus_ImageAttributesSetRemapTable($hIA, $aRemapTable)
    
    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hImage1, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA)
    
    ; Save resultant image
    _GDIPlus_ImageSaveToFile($hImage1, @ScriptDir & "\p1.bmp")

    ; Clean up resources
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_ImageDispose($hImage1)
    
    ; Shut down GDI+ library
    _GDIPlus_Shutdown()

EndFunc   ;==>Example

https://i.postimg.cc/ZYXCH5MQ/p0.png

https://i.postimg.cc/0jS5F7BB/p1.png

 

Hello,

Is there a quick way to use "gdi" to convert the image "p0.bmp" to "p1.bmp"?

With "_GDIPlus_ImageAttributesSetRemapTable" I could convert the relevant pixels to the desired color. Now I just need a quick function to replace the remaining non-relevant pixels with white "0xFFFFFFFF".

 

thanks in advance

 

p1.bmp p0.bmp

Edited by Socrathes
Link to comment
Share on other sites

edit: Somehow i misunderstood what you want to do...

As you have already replaced some colors, then you can use the _GDIPlus_BitmapGetPixel then _GDIPlus_BitmapSetPixel to replace all colors which are not needed.

Or create a color table by using _GDIPlus_BitmapGetPixel, excluding the needed colors, then do the same thing as in your opening script ?!

Edited by Dan_555

Some of my script sourcecode

Link to comment
Share on other sites

1 hour ago, Dan_555 said:

edit: Somehow i misunderstood what you want to do...

As you have already replaced some colors, then you can use the _GDIPlus_BitmapGetPixel then _GDIPlus_BitmapSetPixel to replace all colors which are not needed.

Or create a color table by using _GDIPlus_BitmapGetPixel, excluding the needed colors, then do the same thing as in your opening script ?!

"_GDIPlus_BitmapSetPixel,_GDIPlus_BitmapGetPixel" is very slow, "colortable" makes no sense for the non-relevant pixels because there can be too many different pixels and they are not always the same. the whole thing is also not limited to 100x100 pixels as with p0 and p1 it is just an example.

i was thinking more of new handle with 100x100 white pixel and then put the relevant pixel over it (transparency)?

Edited by Socrathes
Link to comment
Share on other sites

I think that what you want is LockBits and UnlockBits, check out the example on _GDIPlus_BitmapUnlockBits
https://www.autoitscript.com/autoit3/docs/libfunctions/_GDIPlus_BitmapUnlockBits.htm

Seems like what you're looking to do, and it will likely be faster than Get/SetPixel. I would make an example but I'm already busy with some other things and will get easily distracted by this. Besides, I think the example in the helpfile should be what you need, you just need to loop through your replace array and I bet you'd be good.

Let me know if you have any questions about it, I have used it a bit in the past, so I can try and help out if you get something going from this.

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

5 hours ago, mistersquirrle said:

I think that what you want is LockBits and UnlockBits, check out the example on _GDIPlus_BitmapUnlockBits
https://www.autoitscript.com/autoit3/docs/libfunctions/_GDIPlus_BitmapUnlockBits.htm

Seems like what you're looking to do, and it will likely be faster than Get/SetPixel. I would make an example but I'm already busy with some other things and will get easily distracted by this. Besides, I think the example in the helpfile should be what you need, you just need to loop through your replace array and I bet you'd be good.

Let me know if you have any questions about it, I have used it a bit in the past, so I can try and help out if you get something going from this.

I had already thought about that, but i thought there might be a prefabricated very fast function.
For example with "_GDIPlus_ImageAttributesSetThreshold" and "_GDIPlus_ImageAttributesSetRemapTable" you can already create a pure black/white image. However, the relevant pixels would have to become completely white and I would like to have the option to colorize the relevant pixels according to my own wishes.

Of course with "_GDIPlus_BitmapUnlockBits" you could put this together somehow, but it would be a bit time consuming and I was hoping that maybe a better solution already exists.

as it looks there seems to be no good ready solution, then I'll probably have to do the work :)

Edited by Socrathes
Link to comment
Share on other sites

  • Developers

Those images look a lot like captcha so please read our forums rules carefully before posting again.

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Jos locked this topic
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...