Alle Pixel eines Bildes in ein Array schreiben

  • Hi Community,

    ich wollte mal fragen ob es möglich ist, ein Bild einzulesen und die Farbinformation über jedes Pixel in ein Array zu schreiben.

    Endergebnis wäre dann so ein Array, bei einem 500 x 430 Bild:

    [autoit]


    Dim $aPixInfo[500][430]

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

    ....
    $aPixInfo[188][2] = 0x0 ; Schwarz
    ...

    [/autoit]


    Edit: Ich habe "$tagBITMAPINFO" entdeckt, kann damit allerdings nichts anfangen.

  • TheShadowAE
    die lösung wäre aber sehr unpraktisch wenn man immer erst das bild auf 1,1 öffnen muss^^

    ps das gibt sicher auch n fehler wenndann musst du das array um 1 größer dimensionieren.
    $pixel[500] heißt es gibt von 0 bis 499

  • Ich möchte Bilder speziell verschlüsseln. Also eine schwarz-weiß Kryptographie erzeugen & einen Chiffretext.

  • Spoiler anzeigen
    [autoit]

    #include <gdiplus.au3>
    #include <array.au3>
    _GDIPlus_Startup()

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

    Global $Pic = _GDIPlus_ImageLoadFromFile("C:\test.bmp")
    $array = _GetPixelsOfImage($Pic)
    _ArrayDisplay($array)

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

    Func _GetPixelsOfImage($hImage)
    Local $iWidth = _GDIPlus_ImageGetWidth($hImage), $iHeight = _GDIPlus_ImageGetHeight($hImage)
    Local $aPixel[$iWidth + 1][$iHeight + 1]
    For $j = 1 To $iWidth
    For $i = 1 To $iHeight
    $aPixel[$j][$i] = Hex(_GDIPlus_GetPixel($hImage, $j, $i), 6)
    Next
    Next
    Return $aPixel
    EndFunc ;==>_GetPixelsOfImage

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

    Func _GDIPlus_GetPixel($hBitmap, $X, $Y)
    Local $result = DllCall($ghGDIPDll, "int", "GdipBitmapGetPixel", "ptr", $hBitmap, "int", $X, "int", $Y, "dword*", 0)
    If @error Then Return SetError(1, 0, 0)
    Return SetError($result[0], 1, $result[4])
    EndFunc ;==>_GDIPlus_GetPixel

    [/autoit]
  • Wesentlich schneller ist die LockBits-Methode:

    Spoiler anzeigen
    [autoit]

    #include <gdiplus.au3>
    #include <array.au3>
    _GDIPlus_Startup()

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

    Global $Pic = _GDIPlus_ImageLoadFromFile("C:\test.bmp")
    $array = _GetPixelsOfImage($Pic)
    _ArrayDisplay($array)

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

    Func _GetPixelsOfImage($hImage)
    Local $iWidth = _GDIPlus_ImageGetWidth($hImage), $iHeight = _GDIPlus_ImageGetHeight($hImage)
    Local $aPixel[$iWidth][$iHeight]
    _GetPixels($aPixel,$hImage,0,0,$iWidth,$iHeight)
    Return $aPixel
    EndFunc ;==>_GetPixelsOfImage

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

    Func _GetPixels(ByRef $aPixel, $hBitmap, $iX, $iY, $iW, $iH)
    Local $BitmapData = _GDIPlus_BitmapLockBits($hBitmap, $iX, $iY, $iW, $iH, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    Local $Stride = DllStructGetData($BitmapData, "Stride")
    Local $Width = DllStructGetData($BitmapData, "Width")
    Local $Height = DllStructGetData($BitmapData, "Height")
    Local $Scan0 = DllStructGetData($BitmapData, "Scan0")
    Local $PixelData
    For $row = 0 To $Height - 1 ; Reihe für Reihe
    For $col = 0 To $Width - 1 ; Spalte für Spalte
    $PixelData = DllStructCreate("dword", $Scan0 + ($row * $Stride) + ($col * 4))
    $aPixel[$col][$row]=Hex(DllStructGetData($PixelData, 1))
    Next
    Next
    _GDIPlus_BitmapUnlockBits($hBitmap, $BitmapData)
    EndFunc ;==>_GreyScale

    [/autoit]

    Wobei es da eine noch schnellere Version gibt, bei der nicht jedesmal DllStructCreate aufgerufen wird,
    leider finde ich die grad nicht!

    lgE


    Edit:
    dies hab ich gemeint, ist jedoch langsamer
    Damit kann man aber besser die einzelnen Farben auslesen

    [autoit]

    Local $PixelData = DllStructCreate("ubyte lData[" & (Abs($Stride) * $Height - 1) & "]", $Scan0)
    For $row = 0 To $Height - 1 ; Reihe für Reihe
    For $col = 0 To $Width - 1 ; Spalte für Spalte
    $aPixel[$col][$row] = Hex(BitShift(DllStructGetData($PixelData, 1, ($row * $Stride) + ($col * 4)), -24) + BitShift(DllStructGetData($PixelData, 1, ($row * $Stride) + ($col * 4) + 3), -16) + BitShift(DllStructGetData($PixelData, 1, ($row * $Stride) + ($col * 4) + 2), -8) + DllStructGetData($PixelData, 1, $row * $Stride + ($col * 4) + 1))
    Next
    Next

    [/autoit]