Jump to content

Replace a Color in an Image to transparent


Recommended Posts

Hey Guys!

I am currently trying to automate making icons for my small fan made game. Basically i just take a screenshot of a 3D Model in 32x32 and remove the BG color via Photoshop, then saving it as .png.
Atm my script creates a rectangle to visually show me the area of the screenshot and hides the rectangle once it takes a screenshot:
 

$Rectangle = GUICreate("Test", 32, 32, 1358, 438,$WS_POPUP,$WS_EX_TOPMOST)
GUISetState(@SW_SHOW)
WinSetTrans($Rectangle, "", 150)

HotKeySet("{DOWN}" , "close")
HotKeySet("{UP}" , "TakeScreenshot")

Global $funcs = False

While 1
    If $funcs = True Then
        ToolTip("Cheeeseee" , 0 , 30)
        Sleep(10)
        GUISetState(@SW_HIDE, $Rectangle);Hide rectangle
        _ScreenCapture_Capture("C:\Users\PAT\Pictures\Item" & "\Test.jpg", 1358, 438, 1390, 470);Take Screenshot and save the Picture
        GUISetState(@SW_SHOW, $Rectangle);Unhide rectangle
        $funcs = False
    
    EndIf
WEnd

Func close()
    ToolTip("Closing..." , 0 , 30)
    Sleep(200)
    Exit
EndFunc

Func TakeScreenshot()
   If $funcs = False Then
      $funcs = True
   Else
      $funcs = False
      ToolTip("Ready!" , 0 , 30)
   EndIf
EndFunc


 

The Screenshot it takes looks like this:
Test.jpg.ddbcf364990410a2a7169b6a8cfb8cb4.jpg

But i want it to look like this(transparent background):
Part_fCloIrinVoteUpper.png.14ecd4844b32faf5e6be6f22f09c9dc1.png


How would you guys do it? I'm pretty new to GDI and graphics related stuff in AutoIT so i have no clue what to do 😕

Any help would be really appreciated!




Edit: Alright i found a solution! :)
I've used this script from @UEZ to make it work like intended. Thanks to UEZ! ♥

 

;benötigt 3.3.12.0
#include <ScreenCapture.au3>


_GDIPlus_Startup()
Global $iWidth = 400, $iHeight = 400
Global $hBitmap_GDI = _ScreenCapture_Capture("", 0, 0, $iWidth - 1, $iHeight - 1, 0)
Global $hBitmap_GDIPlus = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap_GDI)
Global $hBitmap_Result = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
Global $hBitmap_Result_Ctxt = _GDIPlus_ImageGetGraphicsContext($hBitmap_Result)


Global $aRemapTable[2][2]
$aRemapTable[0][0] = 1
$aRemapTable[1][0] = 0xFFFFFFFF ;Farbe, die Transparent gemacht werden soll
$aRemapTable[1][1] = 0x08000000 ;Semi Transparenz - Format 0xAARRGGBB


Global $hIA = _GDIPlus_ImageAttributesCreate()
_GDIPlus_ImageAttributesSetRemapTable($hIA, 1, True, $aRemapTable)
_GDIPlus_GraphicsDrawImageRectRect($hBitmap_Result_Ctxt, $hBitmap_GDIPlus, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA)
_GDIPlus_ImageSaveToFile($hBitmap_Result, @ScriptDir & "\Result.png")


_GDIPlus_GraphicsDispose($hBitmap_Result_Ctxt)
_GDIPlus_BitmapDispose($hBitmap_GDIPlus)
_GDIPlus_BitmapDispose($hBitmap_Result)
_WinAPI_DeleteObject($hBitmap_GDI)
_GDIPlus_ImageAttributesDispose($hIA)
_GDIPlus_Shutdown()


ShellExecute(@ScriptDir & "\Result.png")


Func _GDIPlus_ImageAttributesSetRemapTable($hImageAttributes, $iColorAdjustType = 0, $fEnable = False, $aColorMap = 0)
Local $iI, $iCount, $tColorMap, $aResult
If IsArray($aColorMap) And UBound($aColorMap) > 1 Then
$iCount = $aColorMap[0][0]
$tColorMap = DllStructCreate("uint ColorMap[" & $iCount * 2 & "]")
For $iI = 1 To $iCount
$tColorMap.ColorMap((2 * $iI - 1)) = $aColorMap[$iI][0]
$tColorMap.ColorMap((2 * $iI)) = $aColorMap[$iI][1]
Next
$aResult = DllCall($__g_hGDIPDll, "int", "GdipSetImageAttributesRemapTable", "handle", $hImageAttributes, "int", $iColorAdjustType, "int", $fEnable, "int", $iCount, "struct*", $tColorMap)
If @error Then Return SetError(@error, @extended, False)
If $aResult[0] Then Return SetError(10, $aResult[0], False)
Return True
EndIf
Return SetError(11, 0, False)
EndFunc ;==>_GDIPlus_ImageAttributesSetRemapTable

 

Edited by Sebiix3
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...