Sign in to follow this  
Followers 0
mesale0077

bmp file background color delete

5 posts in this topic

The only color of the background of the pictures, how do I delete the background color

thank you help

bmp file ,gif file ,jpg file ,background color delete

out file ,gif or png file make?

Share this post


Link to post
Share on other sites



Did you want to do this in AutoIT of from another program like Paint.NET

C there is a lot of white in there. Is not going to be easy.


Share this post


Link to post
Share on other sites

With this example, zero shade variation gives your girlfriend a white aura.

A 0x10 shade variation makes part of her waist transparent.

A 0x08 shade variation looks fine.

#include <GDIPlus.au3>

Global $sRegPath, $sImageIn, $sImageOut

$sImageIn = FileOpenDialog("First image", "", "All images (*.jpg;*.png;*.gif;*.bmp;)")
If $sImageIn = "" Then Exit
$sImageOut = @TempDir & "\ModifiedImage.png"

_ImageToTransPNG($sImageIn, $sImageOut)

If FileExists($sImageIn) Then ShellExecute($sImageIn)
Sleep(1000)
MsgBox(0, "", 'Press "OK" to continue, and, to view image in black & white')
If FileExists($sImageOut) Then ShellExecute($sImageOut)


;parameters
;   $iKeepColor - The pixel colour to retain in the image.(Default colour is black, 0xFF000000, in 0xAARRGGBB hex format.)
;   $iChangeNotColorTo - The pixel colour to change every colour that is NOT $iKeepColor colour in the image.
;                       (Default colour is white, 0xFFFFFFFF, in 0xAARRGGBB hex format.)
Func _ImageToTransPNG($sInFile, $sOutFile, $iKeepColor = 0xFF000000, $iChangeNotColorTo = 0xFFFFFFFF)
    Local $hImage, $iW, $iH, $tBitmapData, $iStride, $iScan0
    $iKeepColor = StringRegExpReplace(Hex($iKeepColor, 8), "(..)(..)(..)(..)", "\4\3\2\1") ; convert to 0xBBGGRRAA
    $iChangeNotColorTo = StringRegExpReplace(Hex($iChangeNotColorTo, 8), "(..)(..)(..)(..)", "\4\3\2\1")
    _GDIPlus_Startup()

    $hImage = _GDIPlus_ImageLoadFromFile($sInFile)
    $iW = _GDIPlus_ImageGetWidth($hImage)
    $iH = _GDIPlus_ImageGetHeight($hImage)

    ;=> Start Work around For XP, GDIPBitmapLockBits() seem to hard crash autoit When using images that are less then 24bpp
    ; If your using Vista or Newer OS then this won't be called or needed.
    ;If StringInStr("WIN_2003,WIN_XP,WIN_2000", @OSVersion) Then
    Local $aRet, $hBmp, $hBitmap, $hGraphic
    $aRet = _GDIPlus_ImageGetPixelFormat($hImage)
    If Int(StringRegExpReplace($aRet[1], "\D+", "")) < 24 Then
        $hBmp = _WinAPI_CreateBitmap($iW, $iH, 1, 32)
        $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hBmp)
        _WinAPI_DeleteObject($hBmp)
        $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBitmap)
        _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0)
        _GDIPlus_ImageDispose($hImage)
        _GDIPlus_GraphicsDispose($hGraphic)
        $hImage = _GDIPlus_BitmapCloneArea($hBitmap, 0, 0, $iW, $iH, $GDIP_PXF32ARGB)
        _GDIPlus_BitmapDispose($hBitmap)
    EndIf
    ;EndIf
    ;=> End Work around

    $tBitmapData = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iW, $iH, $GDIP_ILMWRITE, $GDIP_PXF32ARGB)
    $iStride = DllStructGetData($tBitmapData, "stride")
    $iScan0 = DllStructGetData($tBitmapData, "Scan0")

    Local $begin = TimerInit()
    Local $v_BufferA = DllStructCreate("byte[" & $iH * $iW * 4 & "]", $iScan0) ; Create DLL structure for all pixels
    Local $AllPixels = DllStructGetData($v_BufferA, 1)
    Local $sPix1 = StringRegExpReplace(StringTrimLeft($AllPixels, 2), "(.{8})", "\1 ")

    ; Background colour to transparent
    $iBkGndCol = "0x" & StringLeft($sPix1, 8)
    ConsoleWrite($iBkGndCol & @CRLF)
    $sPix = StringRegExpReplace($sPix1, _ShadeVariationREPattern("0x" & Hex($iBkGndCol, 8), 0x08, False) & " ", "00000000" & " ")
    ;$sPix = StringRegExpReplace($sPix1, hex($iBkGndCol,8) & " ", "00000000" & " ")
    ;ConsoleWrite( _ShadeVariationREPattern("0x" & hex($iBkGndCol,8), 0x10,False) & @CRLF)

    $sPix = "0x" & StringStripWS($sPix, 8)
    $AllPixels = DllStructSetData($v_BufferA, 1, $sPix)

    _GDIPlus_BitmapUnlockBits($hImage, $tBitmapData)
    ;ConsoleWrite("Time: " & TimerDiff($begin) & @CRLF)

    _GDIPlus_ImageSaveToFile($hImage, $sOutFile)

    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
EndFunc ;==>_ImageToTransPNG

;======================== _ShadeVariationREPattern ===========================
; Description - Checks if a 24bit color matches a specified color plus or minus a shade varation.
; Parameters
; $iColor   - A 'RGB' or 'ARGB' color to test.
; $ShadeVariation - An integer added to and substracted from each color channel of the color, $iColPlusShade.
;   $ARGB           - If $iColor Format is in 0xAARRGGBB hex format then $ARGB = True is default.
;                    If false then $iColor Format is in 0xBBGGRRAA hex format.
; Returns 1 (matched) or 0 (no match)
;
Func _ShadeVariationREPattern($iColor, $ShadeVariation, $ARGB = True)
    Local $Pattern, $iColSrchMin, $iColSrchMax
    If $ARGB Then $iColor = "0x" & StringRegExpReplace(Hex($iColor, 8), "(..)(..)(..)(..)", "\4\3\2\1")
    ;ConsoleWrite(hex($iColor, 8) & @CRLF)
    Local $aColor = StringRegExp(Hex($iColor, 8), "(.{2})", 3)
    For $i = 0 To UBound($aColor) - 2
        $Pattern &= "("
        $iColSrchMin = "0x" & Hex((Dec(Hex("0x" & $aColor[$i], 2)) - $ShadeVariation) * ((Dec(Hex("0x" & $aColor[$i], 2)) - $ShadeVariation) > 0), 2)
        $iColSrchMax = "0x" & Hex((Dec(Hex("0x" & $aColor[$i], 2)) + $ShadeVariation + 1) * ((Dec(Hex("0x" & $aColor[$i], 2)) + $ShadeVariation + 1) < 255) + 255, 2)
        ;ConsoleWrite($iColSrchMin & " to " & $iColSrchMax & @CRLF)
        For $n = $iColSrchMin To $iColSrchMax
            $Pattern &= Hex($n, 2) & "|"
        Next
        $Pattern = StringTrimRight($Pattern, 1) & ")"
    Next
    Return $Pattern & "(FF)"
EndFunc ;==>_ShadeVariationREPattern

Share this post


Link to post
Share on other sites

thank you Malkey

Share this post


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
Sign in to follow this  
Followers 0