Jump to content

Fill image with other colors - (Moved)


Recommended Posts

  • Moderators

Moved to the appropriate forum, as the AutoIt Example Scripts forum very clearly states:

Quote

Share your cool AutoIt scripts, UDFs and applications with others.


Do not post general support questions here, instead use the AutoIt Help and Support forums.

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Look at _GDIPlus_BitmapGetPixel in the autoit help file.  It is pretty much all you need in there.  You will need to adjust the, lets call it, filter. And you will need to read a file instead of a screencapture. This one easy way to do it, among other solutions.

Good scripting !

Link to comment
Share on other sites

#include <GDIPlus.au3>

; base color 0xFF0080 (255,0,128)
Global $iBaseR = 255, $iBaseG = 0, $iBaseB = 128
; shade-variation (0-255)
Global $iVary = 107
; color to change base color
Global $iChangeBaseColor = 0x000000
; color to change other colors
Global $iChangeOtherColor = 0xFFFFFF

Global $sSourceFile = "1.jpg"
Global $sDestFile = "2.jpg"

SplashTextOn("", "Processing...", 300, 100, Default, Default, 33)
_GDIPlus_Startup()

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

$tBits = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iW, $iH, BitOR($GDIP_ILMWRITE, $GDIP_ILMREAD))
$iScan0 = DllStructGetData($tBits, "Scan0")
$tPixel = DllStructCreate("int[" & $iW * $iH & "];", $iScan0)
For $iY = 0 To $iH - 1
  $iOffset = $iY * $iW + 1
  For $iX = 0 To $iW - 1
    $iColor = DllStructGetData($tPixel, 1, $iOffset + $iX)
    $iR = BitAND(BitShift($iColor, 16), 0xFF)
    $iG = BitAND(BitShift($iColor, 8), 0xFF)
    $iB = BitAND($iColor, 0xFF)
    If  $iR > $iBaseR - $iVary And $iR < $iBaseR + $iVary _
    And $iG > $iBaseG - $iVary And $iG < $iBaseG + $iVary _
    And $iB > $iBaseB - $iVary And $iB < $iBaseB + $iVary Then
      DllStructSetData($tPixel, 1, $iChangeBaseColor, $iOffset + $iX)
    Else
      DllStructSetData($tPixel, 1, $iChangeOtherColor, $iOffset + $iX)
    EndIf
  Next
Next
_GDIPlus_BitmapUnlockBits($hImage, $tBits)

_GDIPlus_ImageSaveToFile($hImage, $sDestFile)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()

SplashOff()
ShellExecute($sDestFile)

 

Link to comment
Share on other sites

Check this out (example is from help file but modified appropriately):

;coded by UEZ
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $sFile = "C:\Temp\Ballons_364x227.png"
    If Not FileExists($sFile) Then
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", $sFile & " not found!", 30)
        Return False
    EndIf

    _GDIPlus_Startup()
    Local $hImage = _GDIPlus_ImageLoadFromFile($sFile) ;create an image object based on a file
    If @error Then
        _GDIPlus_Shutdown()
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "An error has occured - unable to load image!", 30)
        Return False
    EndIf

    Local $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage) ;get width and height of the image
    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH)
    Local $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsDrawImageRect($hContext, $hImage, 0, 0, $iW, $iH)

    Local $tBitmapData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $iW, $iH, BitOR($GDIP_ILMWRITE, $GDIP_ILMREAD), $GDIP_PXF32ARGB) ;locks a portion of a bitmap for reading and writing. More infor at http://msdn.microsoft.com/en-us/library/windows/desktop/ms536298(v=vs.85).aspx
    Local $iScan0 = DllStructGetData($tBitmapData, "Scan0") ;get scan0 (pixel data) from locked bitmap
    Local $tPixel = DllStructCreate("int argb[" & $iW * $iH & "];", $iScan0)
    Local $iRGBA, $iRowOffset, $aHSL

    For $iY = 0 To $iH - 1
        $iRowOffset = $iY * $iW + 1
        For $iX = 0 To $iW - 1 ;get each pixel in each line and row
            $iRGBA = DllStructGetData($tPixel, 1, $iRowOffset + $iX)
            $aHSL = RGB2HSL($iRGBA) ;convert color to HSL color space
            ;check if HSL color is in the red / purple range -> see http://hslpicker.com for details
            If ($aHSL[0] < 30 And InRange(Round(100 * $aHSL[1]), 100) And InRange(Round(100 * $aHSL[2]), 50)) Or _
               ($aHSL[0] > 300 And InRange(Round(100 * $aHSL[1]), 100) And InRange(Round(100 * $aHSL[2]), 50)) Then
                DllStructSetData($tPixel, 1, 0xFFFFFFFF, $iRowOffset + $iX) ;if yes then set white pixel
            Else
                DllStructSetData($tPixel, 1, 0xFF000000, $iRowOffset + $iX) ;else black pixel
            EndIf
        Next
    Next
    _GDIPlus_BitmapUnlockBits($hBitmap, $tBitmapData) ;unlocks a portion of a bitmap that was locked by _GDIPlus_BitmapLockBits


    ;display manipulated image
    Local $hGUI = GUICreate("_GDIPlus_BitmapLockBits Demo", $iW, $iH)
    GUISetState(@SW_SHOW)

    Local $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a Graphics object from a window handle
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $iW, $iH) ;copy manipulated image to graphics handle

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ;cleanup resources
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_GraphicsDispose($hContext)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func InRange($iNumber, $iRange, $iPlus = 25, $iMinus = 25)
    If $iNumber > ($iRange - $iMinus) And $iNumber < ($iRange + $iPlus) Then Return 1
    Return 0
EndFunc

Func RGB2HSL($iColor)
    Local $r = BitAND(BitShift($iColor, 16), 255) / 255, $g = BitAND(BitShift($iColor, 8), 255) / 255, $b = BitAND($iColor, 255) / 255
    Local $Cmax = Max(Max($r, $g), $b), $Cmin = Min(Min($r, $g), $b), $deltaChroma = $Cmax - $Cmin
    Local $L = ($Cmin + $Cmax) / 2, $H, $S
    If $deltaChroma = 0 Then
        $H = 0
        $S = 0
    Else
        $S = $deltaChroma / (1 - Abs(2 * $L - 1))
        If $r = $Cmax Then ;r is max
            $H = Mod(($g - $b) / $deltaChroma, 6)
        ElseIf $g = $Cmax Then ;g is max
            $H = ($b - $r) / $deltaChroma + 2
        Else ;else b is max
            $H = ($r - $g) / $deltaChroma + 4
        EndIf
        $H *= 60
        If $H < 0 Then $H += 360
    EndIf
    Local $aHSL[3] = [$H, $S, $L]
    Return $aHSL
EndFunc

Func Min($a, $b)
    Return ($a < $b) ? $a : $b
EndFunc

Func Max($a, $b)
    Return ($a > $b) ? $a : $b
EndFunc

Replace $sFile with proper path to the image before you run the script!

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

8 hours ago, qwert said:

@UEZ: thanks for that annotated variation. I now have plenty to look at for a few days. I'm still determined to have a tunable script than can render a monochome result. This looks promising.

Have a look to 

There is a function called _GDIPlus_BlackAndWhite() which can convert an image to b/w aka monochrom.

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
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
 Share

×
×
  • Create New...