Suche Lösung: Grafiktyp erkennen und ggf Umwandeln in 8-Bit Grayscale

  • Hallo, wie die Überschirft schon sagt, bin ich auf der Suche nach einer Lösung, wie man:

    a) Einen Grafikheader auslesen kann um festzustellen, um welchen Grafiktyp es sich handelt. Also z.B. PNG, JPG, BMP, TIF. - Oder geht das nur über die Dateiendung - Ich würde das gerne aus den internen Informationen der Grafik erhalten.

    b) Wenn ich nun die Grafik geöffnet habe, Wie kann ich definitiv feststellen, ob es eine Grayscale Grafik ist und ob sie in 8-Bit ist, also nur Farbwerte von 0-254 hat.

    c) Sollte die Grafik Punkt b) nicht entsprechen, würde ich sie gerne nach 8-Bit Greyscale wandeln.

    Bisher habe ich mich schon ziemlich durchgewühlt durch diverse Foren, Aber so richtig habe ich noch nicht gefunden, was ich suche. Im übrigen, eine Lösung a la:"Öffne doch in IrfanView o.ä. und speichere es als 8-Bit Greyscale" , scheidet aus. Ich würde das ganz gerne komplett in AutoIT lösen.

    Wäre super, wenn Jemand von Euch die Lösung hätte ^^

    Danke.

  • Danke chip für die schnelle Antwort.

    DAS UDF habe ich bisher noch nicht gefunden...Prima.

    Werde es mal durchstöbern, vielleicht hilft es mir weiter. Sieht auf jeden Fall vielversprechend aus.

    Mal sehen. Wenn nicht komme ich wieder... :rolleyes:

  • So, ich habe mich mal durch's UDF bzw. Help script gewühlt. Es scheint dort wirklich Funktionen zu geben, welche meineAufgabenstellung lösen würden. - Überhaupt, eine tolle Umsetzung von progandy. Da kann man sich ja fast seine eigene Grafiksoftware basteln (Wenn es nicht denn schon genug dabon gäbe ;) )

    Nun ja, den einzigsten Nachteil, den ich allerdings darin sehe, ist die (doch recht große) FreeImage.dll immer mitschleppen zu müssen,

    damit das Programm läuft :(

    Vielleicht hat noch Jemand andere Vorschläge für mich, ohne das ich irgendwelche "Zusatzprogramme" mitschleppen muß.

    Aber trotzdem mal Danke Chip für Deinen Hinweis.

  • Darum habe ich ja auch direkt auf den Post verlinkt und nicht auf den ganzen Thread ;). Weil in dem Post wird es ohne FreeImage.dll und rein mit GDIPlus gemacht.

    Spoiler anzeigen
    [autoit]

    #Include <GDIPlus.au3>

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

    _GDIPlus_Startup()

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

    $img = FileOpenDialog("Select an image", @ScriptDir & "\", "Images (*.jpg;*.bmp;*.png;*.gif)", 1)

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

    $hImage1 = _GDIPlus_ImageLoadFromFile($img)
    $hImage2 = _GDIPlus_ImageGreyscale($hImage1)
    $ext = StringMid($img, StringLen($img) -2)
    $sCLSID = _GDIPlus_EncodersGetCLSID ($ext)
    _GDIPlus_ImageSaveToFileEx($hImage2, StringMid($img, 1, StringLen($img) - 4) & "_grey." & $ext, $sCLSID)
    $iwx = _GDIPlus_ImageGetWidth($hImage2)
    $iwy = _GDIPlus_ImageGetHeight($hImage2)
    $hGUI = GUICreate("Convert image to greyscale by Siao", $iwx, $iwy)
    GUISetState()

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

    $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iwx, $iwy)

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

    While GUIGetMsg() <> -3 * Sleep(50)
    WEnd

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

    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)

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

    _GDIPlus_ShutDown()
    Exit

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

    ;; _GDIPlus_ImageGreyscale()
    ;; Creates a greyscale copy of Image object and returns its handle. To destroy it, use _GDIPlus_ImageDispose() or _GDIPlus_BitmapDispose()
    Func _GDIPlus_ImageGreyscale(Const ByRef $hImage)
    Local $tColorMatrix, $x, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage), $hGraphics, $hGraphics2, $hBitmap
    ;;create color matrix data
    $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]")
    ;greyscale values:
    $x = DllStructSetData($tColorMatrix, 1, 0.30, 1) * DllStructSetData($tColorMatrix, 1, 0.30, 2) * DllStructSetData($tColorMatrix, 1, 0.30, 3) * _
    DllStructSetData($tColorMatrix, 2, 0.59, 1) * DllStructSetData($tColorMatrix, 2, 0.59, 2) * DllStructSetData($tColorMatrix, 2, 0.59, 3) * _
    DllStructSetData($tColorMatrix, 3, 0.11, 1) * DllStructSetData($tColorMatrix, 3, 0.11, 2) * DllStructSetData($tColorMatrix, 3, 0.11, 3) * _
    DllStructSetData($tColorMatrix, 4, 1.00, 4) * DllStructSetData($tColorMatrix, 5, 1.00, 5)
    ;;create an image attributes object and update its color matrix
    $hImgAttrib = _GDIPlus_ImageAttributesCreate()
    _GDIPlus_ImageAttributesSetColorMatrix($hImgAttrib, 1, DllStructGetPtr($tColorMatrix))
    ;;copy image
    $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
    $hBitmap = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphics)
    $hGraphics2 = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    ;;draw original into copy with attributes
    _GDIPlus_GraphicsDrawImageRectRectEx($hGraphics2, $hImage, 0, 0, $iW, $iH, 0, 0, $iW, $iH, 2, $hImgAttrib)
    ;;clean up
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_GraphicsDispose($hGraphics2)
    _GDIPlus_ImageAttributesDispose($hImgAttrib)

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

    Return $hBitmap
    EndFunc

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

    ;;;;; other GDIPlus wrappers ;;;;;

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

    #cs
    _GDIPlus_ImageAttributesSetColorMatrix()
    Sets ColorMatrix of ImageAttributes object
    Parameters:
    $hImgAttrib = ImageAttributes object
    $iColorAdjustType = can be:
    ColorAdjustTypeDefault = 0
    ColorAdjustTypeBitmap = 1
    ColorAdjustTypeBrush = 2
    ColorAdjustTypePen = 3
    ColorAdjustTypeText = 4
    ColorAdjustTypeCount = 5
    ColorAdjustTypeAny (Reserved) = 6
    $pColorMatrix = pointer to ColorMatrix structure
    $pGrayMatrix = pointer to GreyMatrix structure
    $iColorMatrixFlags = can be:
    ColorMatrixFlagsDefault = 0
    ColorMatrixFlagsSkipGrays = 1
    ColorMatrixFlagsAltGray = 2
    Return value: True/False
    #ce
    Func _GDIPlus_ImageAttributesSetColorMatrix($hImgAttrib, $iColorAdjustType, $pColorMatrix = 0, $pGrayMatrix = 0, $iColorMatrixFlags = 0)
    Local $fEnable = 1, $aResult = DllCall($ghGDIPDll, "int", "GdipSetImageAttributesColorMatrix", "ptr",$hImgAttrib, "int",$iColorAdjustType, _
    "int",$fEnable, "ptr",$pColorMatrix, "ptr",$pGrayMatrix, "int",$iColorMatrixFlags)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
    EndFunc

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

    ;;Creates ImageAttributes object
    Func _GDIPlus_ImageAttributesCreate()
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipCreateImageAttributes", "ptr*", 0)
    Return SetError($aResult[0], 0, $aResult[1])
    EndFunc

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

    ;;Deletes ImageAttributes object
    Func _GDIPlus_ImageAttributesDispose($hImgAttrib)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDisposeImageAttributes", "ptr", $hImgAttrib)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
    EndFunc

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

    ;; _GDIPlus_GraphicsDrawImageRectRectEx()
    ;; Same as _GDIPlus_GraphicsDrawImageRectRect(), but adds 1 optional parameter - $hImgAttrib (handle to ImageAttributes object)
    Func _GDIPlus_GraphicsDrawImageRectRectEx($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth, $iSrcHeight, $iDstX, $iDstY, $iDstWidth, $iDstHeight, $iUnit = 2, $hImgAttrib = 0)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDrawImageRectRectI", "hwnd", $hGraphics, "hwnd", $hImage, "int", $iDstX, "int", _
    $iDstY, "int", $iDstWidth, "int", $iDstHeight, "int", $iSrcX, "int", $iSrcY, "int", $iSrcWidth, "int", _
    $iSrcHeight, "int", $iUnit, "ptr", $hImgAttrib, "int", 0, "int", 0)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
    EndFunc

    [/autoit]

    Andy hat mir ein Schnitzel gebacken aber da war ein Raupi drauf und bevor Oscar das Bugfixen konnte kam Alina und gab mir ein AspirinJunkie.

  • Stimmt, Chip. Wenn man genauer schaut, entdeckt man das Script. :D

    Also..habe es mal getestet. Die Umwandlung nach Grayscale funktioniert - Prima.
    Wenn allerdings das Eingangsbild z.B. ein farbiges RGB war, macht er mir ein Grayscale RGB draus.

    Was mir noch fehlt ist: Ich würde gerne feststellen:
    a) Welches Farbmodell das Bild hat: z.B. RGB, CMYK, HSB
    b) Welcher Farbraum: z.B. innerhalb RGB: Adobe RGB, sRGB, oder ProPhoto RGB
    c) Aus wievielen Farbkanälen besteht das Bild? z.B. 1,2,3?
    d) Welche Bit-Tiefe hat das Bild? z.B.: 8,16,24,32bit

    Das sollte man doch irgendwie aus einem Bild (Header) feststellen bzw. auslesen können.
    Warum? - Oder was ist mein Ziel?
    Mein Ziel ist, bei Bedarf ein Bild in Graustufen abzuspeichern mit der geringsten Bitzahl.
    Wenn ich das bei Photoshop mache, gibt es sehrwohl nochmals einen Unterschied in der
    Bildgröße zwischen 8bit/RGB GrayScale und nur 8Bit/Greyscale.


    Ich vermute mal bei 8Bit/RGB GreyScale wird die GrayScale aus 3 Kanälen gebildet also 0-254, 0-254, 0-254
    Mir würde aber ein Kanal reichen a'la 0-254 - Was im Prinzip einem Drittel der Daten entsprechen würde bei gleicher
    Grayscale-Bereich Darstellung.


    Wie erkenne ich also die relevanten Bilddaten, wie speichere ich sie als 1Kanal Greyscale ab und wie öffne ich Sie
    so auch wieder?

    3 Mal editiert, zuletzt von Dirgi (14. September 2011 um 14:16)