Graustufeneinteiler

  • Hey :)

    Wie ihr vllt wisste beschäftige ich mich (unter anderem) im Moment mit Hexadezimal etc. Und da ein Freund von mir in Kunst gerade Bilder in Graustufen einteilen muss dachte ich mir, hei, das kann ich auch am PC ^^

    Nunja, jetzt es es fertig. Man kann angeben wv Graustufen erstellt werden sollen, ich empfehle 3-5, je nach Bild. Man kann auch "Weiß als Grauton werten", was soviel bedeutet wie: Jede Graustufe wird eine Graustufe höher gesetzt. Einfach mal angucken man sollte es eig. direkt sehen :) Toleranz solltet ihr immer auf 1 machen, hat aber eig im Moment garkeinen Effekt (Sollte anfangs eine Art Umweldmiteinbeziehung sein, das hat leider nicht ganz geklappt, vllt kommt es noch irgendwann ^^)

    Der Code ist im Anhang, ich hoffe ich bekomm noch gute Kritik und vllt sogar Lob ;)


    Graustufeneinteiler
    [autoit]

    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_outfile=..\..\..\Desktop\Graustufeneinteiler.exe
    #AutoIt3Wrapper_UseX64=n
    #AutoIt3Wrapper_res_language = German
    #AutoIt3Wrapper_res_fileversion = 1.0
    #Auto
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
    #include <GDIPlus.au3>
    #include <GDIP.au3>

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()
    Opt("GUIONEVENTMODE", 1)

    [/autoit] [autoit][/autoit] [autoit]

    Global $hWnd_ImageGUI = -123, $hImage, $hGraphics, $hBitmap, $hBackbuffer, $hBackbuffer_GreyPic, $iWidth_ImageGUI, $iHeight_ImageGUI, $Edited
    Global $iWidth_ToolGUI = 530, $iHeight_ToolGUI = 50, $DrawImage = False, $EndColorSubtrahend

    [/autoit] [autoit][/autoit] [autoit]

    $hWnd_ToolGUI = GUICreate("Graustufenbilder", $iWidth_ToolGUI, $iHeight_ToolGUI + 30, 100, 100)
    GUISetBkColor(0xAAAAAA, $hWnd_ToolGUI)
    GUISetOnEvent(-3, "_Exit", $hWnd_ToolGUI)

    [/autoit] [autoit][/autoit] [autoit]

    $iButton_LoadImage = GUICtrlCreateButton("Load", 0, 0, 100, $iHeight_ToolGUI)
    GUICtrlSetFont($iButton_LoadImage, 14)
    GUICtrlSetOnEvent($iButton_LoadImage, "_LoadImage")

    [/autoit] [autoit][/autoit] [autoit]

    $iButton_Start = GUICtrlCreateButton("Start...", 100, 0, 100, $iHeight_ToolGUI)
    GUICtrlSetFont($iButton_Start, 14)
    GUICtrlSetOnEvent($iButton_Start, "_StartGreyfields")

    [/autoit] [autoit][/autoit] [autoit]

    $iButton_Save = GUICtrlCreateButton("Save", 200, 0, 100, $iHeight_ToolGUI)
    GUICtrlSetFont($iButton_Save, 14)
    GUICtrlSetOnEvent($iButton_Save, "_SaveImage")

    [/autoit] [autoit][/autoit] [autoit]

    $iInput_CountGreyfields = GUICtrlCreateInput("", 300, 0, 100, 23, 0x2000)
    GUICtrlSetFont($iInput_CountGreyfields, 10)
    GUICtrlSendMsg($iInput_CountGreyfields, 0x1501, 0, "Graustufen") ;; Wie viele Grautöne entstehen sollen

    [/autoit] [autoit][/autoit] [autoit]

    $iInput_Toleranz = GUICtrlCreateInput("", 300, 25, 100, 23, 0x2000)
    GUICtrlSetFont($iInput_Toleranz, 10)
    GUICtrlSendMsg($iInput_Toleranz, 0x1501, 0, "Toleranz") ;; Wie viele Umliegende Felder in den Durchnitswert mit einbezogen werden sollen

    [/autoit] [autoit][/autoit] [autoit]

    $iCheckbox_EndcolorSubtrahend = GUICtrlCreateCheckbox("Weiß als Grau werten", 400, 0, 130)
    GUICtrlSetFont($iCheckbox_EndcolorSubtrahend, 8)

    [/autoit] [autoit][/autoit] [autoit]

    $iProgress = GUICtrlCreateProgress(0, $iHeight_ToolGUI, $iWidth_ToolGUI, 30)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState(@SW_SHOW, $hWnd_ToolGUI)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    While 1
    While $DrawImage
    If $Edited = False Then _GDIPlus_GraphicsDrawImageRect($hBackbuffer, $hImage, 0, 0, $iWidth_ImageGUI, $iHeight_ImageGUI)
    If $Edited Then _GDIPlus_GraphicsDrawImageRect($hBackbuffer, $hBackbuffer_GreyPic, 0, 0, $iWidth_ImageGUI, $iHeight_ImageGUI)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, $iWidth_ImageGUI, $iHeight_ImageGUI)
    WEnd
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _SaveImage()
    If $hImage = "" Then Return MsgBox(0, "Fehler", "Kein Bild ausgewählt!")
    $sFileSavePath = FileSaveDialog("Bild speichern...", @DesktopDir & "\", "Bilder (*.jpg;*.png;*.jpg)", 18)
    If @error Then Return
    _GDIPlus_ImageSaveToFile($hBitmap, $sFileSavePath)
    EndFunc ;==>_SaveImage

    [/autoit] [autoit][/autoit] [autoit]

    Func _StartGreyfields()
    If $hImage = "" Then Return MsgBox(0, "Fehler", "Kein Bild ausgewählt!")

    [/autoit] [autoit][/autoit] [autoit]

    $Count = GUICtrlRead($iInput_CountGreyfields)
    $Toleranz = GUICtrlRead($iInput_Toleranz)
    If $Count = "" Or $Toleranz = "" Or Not StringIsInt($Count) Or Not StringIsInt($Toleranz) Then Return MsgBox(0, "Fehler", "Toleranz und/oder Anzahl nicht ausgewählt oder falsche Eingabe!")

    [/autoit] [autoit][/autoit] [autoit]

    If GUICtrlRead($iCheckbox_EndcolorSubtrahend) = 1 Then
    $EndColorSubtrahend = 0
    Else
    $EndColorSubtrahend = 1
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    $iAnzahl = $iWidth_ImageGUI
    For $i = 0 To $iWidth_ImageGUI
    For $j = 0 To $iHeight_ImageGUI
    $Color = _GetGreyColor($i, $j, $Toleranz, $Toleranz)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    For $k = 1 To $Count

    [/autoit] [autoit][/autoit] [autoit]

    If Dec($Color) > (255 / $Count) * ($k - 1) And Dec($Color) <= (255 / $Count) * $k Then
    $Color = Hex((255 / $Count) * ($k - $EndColorSubtrahend), 2)
    EndIf
    Next

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $hBrush = _GDIPlus_BrushCreateSolid("0xFF" & $Color & $Color & $Color)
    _GDIPlus_GraphicsFillRect($hBackbuffer_GreyPic, $i, $j, 1, 1, $hBrush)
    _GDIPlus_BrushDispose($hBrush)
    Next
    GUICtrlSetData($iProgress, (100 / $iAnzahl) * $i)
    ToolTip("Daten: " & $i & " von " & $iAnzahl & @CRLF & "Prozent (%): " & (100 / $iAnzahl) * $i & " von 100")
    Next
    GUICtrlSetData($iProgress, 100)
    GUICtrlSetData($iProgress, 0)

    [/autoit] [autoit][/autoit] [autoit]

    $Edited = True
    EndFunc ;==>_StartGreyfields

    [/autoit] [autoit][/autoit] [autoit]

    Func _GetGreyColor($iX, $iY, $iWidth, $iHeight)
    Local $R, $G, $B, $Color

    [/autoit] [autoit][/autoit] [autoit]

    For $i = $iX To $iX
    For $j = $iY To $iY
    $Hex = Hex(_GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY))
    ;; Entfernt Alphakanal
    $Hex = StringTrimLeft($Hex, 2)
    $R += Dec(StringLeft($Hex, 2))
    $G += Dec(StringMid($Hex, 3, 2))
    $B += Dec(StringRight($Hex, 2))
    Next
    Next

    [/autoit] [autoit][/autoit] [autoit]

    $Color = Hex(($R + $G + $B) / 3, 2) ;$Color = Hex((($R + $G + $B) / (Round ($iWidth / 2) * Round ($iHeight / 2))) / 3, 2)
    Return $Color
    EndFunc ;==>_GetGreyColor

    [/autoit] [autoit][/autoit] [autoit]

    Func _LoadImage()
    $sFilePath = FileOpenDialog("Wählen Sie ein Bild", @WindowsDir & "\", "Bilder (*.jpg;*.png;*.jpg)", 1 + 4)
    If @error Then Return
    GUIDelete($hWnd_ImageGUI)

    [/autoit] [autoit][/autoit] [autoit]

    $hImage = _GDIPlus_ImageLoadFromFile($sFilePath)
    $iWidth_ImageGUI = _GDIPlus_ImageGetWidth($hImage)
    $iHeight_ImageGUI = _GDIPlus_ImageGetHeight($hImage)

    [/autoit] [autoit][/autoit] [autoit]

    $hWnd_ImageGUI = GUICreate("Image", $iWidth_ImageGUI, $iHeight_ImageGUI)
    GUISetOnEvent(-3, "_CloseImageGUI", $hWnd_ImageGUI)
    GUISetState(@SW_SHOW, $hWnd_ImageGUI)

    [/autoit] [autoit][/autoit] [autoit]

    $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hWnd_ImageGUI)
    $hBitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth_ImageGUI, $iHeight_ImageGUI, $hGraphics)
    $hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    $hBackbuffer_GreyPic = _GDIPlus_ImageGetGraphicsContext($hBitmap)

    [/autoit] [autoit][/autoit] [autoit]

    $DrawImage = True
    $Edited = False
    EndFunc ;==>_LoadImage

    [/autoit] [autoit][/autoit] [autoit]

    Func _CloseImageGUI()
    GUIDelete($hWnd_ImageGUI)
    EndFunc ;==>_CloseImageGUI

    [/autoit] [autoit][/autoit] [autoit]

    Func _Exit()
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hBitmap)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Shutdown()
    Exit
    EndFunc ;==>_Exit

    [/autoit]
  • Die waren Genial!! Du warst/bist ein super Lehrer, macht Spaß und man lernt was. Und der Minecraftclient hat bis jetzt nur gutes gebracht (abgesehen davon das er noch nicht richtig klappt ;D)

    mfg BB

    "IF YOU'RE GOING TO KILL IT
    OPEN SOURCE IT!"

    by Phillip Torrone

    Zitat von Shoutbox

    [Heute, 11:16] Andy: ....böseböseböseböse....da erinnere ich mich daran, dass man den Puschelschwanz eines KaRnickels auch "Blume" nennt....ob da eins zum anderen passt? :rofl: :rofl: :rofl: :rofl:

    https://autoit.de/index.php?page…leIt#post251138

    Neon Snake