bilder komprimieren

  • HI
    Wie kann man bilder komprimieren mit autoit?

    Edit:
    Ich hab hier mal was schönes gefunden:

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    #include <File.au3>
    #include <Array.au3>

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

    ; Declare array
    Dim $Images[1]

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

    ; Gets all JPG files in the current directory (@ScriptDir).
    Local $search = FileFindFirstFile("*.jpg")

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

    ; Check if the search was successful
    If $search = -1 Then
    MsgBox(0, "Error", "No JPG files could be found.")
    Exit
    EndIf

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

    ; Resize array
    While 1
    If IsArray($Images) Then
    Local $Bound = UBound($Images)
    ReDim $Images[$Bound+1]
    EndIf
    $Images[$Bound] = FileFindNextFile($search)
    If @error Then ExitLoop
    WEnd

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

    ; Close the search handle
    FileClose($search)

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

    ; Create directory "resized" if not there yet
    If NOT FileExists(@ScriptDir & "\resized\") Then
    DirCreate(@ScriptDir & "\resized\")
    EndIf
    ; Loop for jpgs - gets dimension of JPG and calls Resize function to Resize to 50% width and 50% height
    For $i = 1 to Ubound($Images)-1
    If $Images[$i] <> "" AND FileExists(@ScriptDir & "\" & $Images[$i]) Then
    Local $ImagePath = @ScriptDir & "\" & $Images[$i]
    _GDIPlus_Startup()
    Local $hImage = _GDIPlus_ImageLoadFromFile($ImagePath)
    Local $ImageWidth = _GDIPlus_ImageGetWidth($hImage)
    Local $ImageHeight = _GDIPlus_ImageGetHeight($hImage)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
    ;MsgBox(0,"DEBUG", $ImageWidth & " x " & $ImageHeight)
    Local $NewImageWidth = ($ImageWidth / 100) * 50
    Local $NewImageHeight = ($ImageHeight / 100) * 50

    ;MsgBox(0,"DEBUG: " & $i,$Images[$i])
    _ImageResize(@ScriptDir & "\" & $Images[$i], @ScriptDir & "\resized\" & $Images[$i], $NewImageWidth, $NewImageHeight)
    EndIf
    Next

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

    ; Resize function
    Func _ImageResize($sInImage, $sOutImage, $iW, $iH)
    Local $hWnd, $hDC, $hBMP, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0

    ;OutFile path, to use later on.
    Local $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))

    ;OutFile name, to use later on.
    Local $sOF = StringMid($sOutImage, StringInStr($sOutImage, "\", 0, -1) + 1)

    ;OutFile extension , to use for the encoder later on.
    Local $Ext = StringUpper(StringMid($sOutImage, StringInStr($sOutImage, ".", 0, -1) + 1))

    ; Win api to create blank bitmap at the width and height to put your resized image on.
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH)
    _WinAPI_ReleaseDC($hWnd, $hDC)

    ;Start GDIPlus
    _GDIPlus_Startup()

    ;Get the handle of blank bitmap you created above as an image
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)

    ;Load the image you want to resize.
    $hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)

    ;Get the graphic context of the blank bitmap
    $hGraphic = _GDIPlus_ImageGetGraphicsContext ($hImage1)

    ;Draw the loaded image onto the blank bitmap at the size you want
    _GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iH)

    ;Get the encoder of to save the resized image in the format you want.
    $CLSID = _GDIPlus_EncodersGetCLSID($Ext)

    ;Generate a number for out file that doesn't already exist, so you don't overwrite an existing image.
    Do
    $i += 1
    Until (Not FileExists($sOP & $i & "_" & $sOF))

    ;Prefix the number to the begining of the output filename
    $sOutImage = $sOP & $i & "_" & $sOF

    ;Save the new resized image.
    _GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)

    ;Clean up and shutdown GDIPlus.
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _WinAPI_DeleteObject($hBMP)
    _GDIPlus_Shutdown()
    EndFunc

    [/autoit]


    bloß ich hab keine ahnung wie ich das so umschreibe, dass es bloß ein bild kleiner macht mit dem namen .bild.jpg
    Kann mir wer helfen, was ich da machen muss?

    2 Mal editiert, zuletzt von 93lucas (18. August 2010 um 19:14)

  • Dafür kannst d GDI+ vcverwenden:

    Spoiler anzeigen
    [autoit]

    #include<Gdiplus.au3>

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

    _GDIPlus_Startup()

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

    $oFile = @ScriptDir & "\Bild.bmp" ;Pfade anpassen
    $nFile = @ScriptDir & "\Bild7.jpg"

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

    #cs
    Local $aDT = FileGetTime($oFile, 1, 0)
    $sInfo = $aDT[0] & "/" & $aDT[1] & "/" & $aDT[2] & " " & $aDT[3] & ":" & $aDT[4] & ":" & $aDT[5]
    ConsoleWrite($sInfo & @CRLF)
    #ce
    $himg = _GDIPlus_ImageLoadFromFile($oFile)

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

    $hGraphics = _GDIPlus_ImageGetGraphicsContext($himg)
    #cs
    $iX = _GDIPlus_ImageGetWidth($himg)
    $iy = _GDIPlus_ImageGetHeight($himg)
    $hBrush = _GDIPlus_BrushCreateSolid(); $Color)
    $hFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    $hFont = _GDIPlus_FontCreate($hFamily, 20, 2)
    $tLayout = _GDIPlus_RectFCreate($iX -220, $iY - 30)
    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphics, $sInfo, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphics, $sInfo, $hFont, $aInfo[0], $hFormat, $hBrush)
    #ce
    _GDIPlus_GraphicsDrawImage($hGraphics, $himg, 0, 0)
    _GDIPlus_ImageSaveToFile($himg, $nFile)
    ;_GDIPlus_BrushDispose($hBrush)
    _GDIPlus_ImageDispose($himg)
    _GDIPlus_Shutdown()
    ShellExecute($nFile)

    [/autoit]

    die auskommentierten Zeilen würden das Änderungsdatum der Originaldatei in die neue schreiben

    mfg (Auto)Bert

  • mit dem script im ersten post verkleiner ich das bild auf 50KB mit deinem in 175kb. Im ersten ist zwar die quali nicht so gut aber das macht mir nix.
    Hauptsache es ist klein.
    Also wie mache ich das am besten jetzt :D?

  • mit dem script im ersten post verkleiner ich das bild auf 50KB mit deinem in 175kb. Im ersten ist zwar die quali nicht so gut aber das macht mir nix.Hauptsache es ist klein.Also wie mache ich das am besten jetzt :D?


    einfach die func _imageResize mit deinen Daten aufrufen:

    Spoiler anzeigen
    [autoit]

    #include<Gdiplus.au3>

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

    $oFile = @ScriptDir & "\Bild.bmp" ;Pfade anpassen
    $nFile = @ScriptDir & "\Bild8.jpg"
    _GDIPlus_Startup()
    Local $hImage = _GDIPlus_ImageLoadFromFile($oFile)
    Local $ImageWidth = _GDIPlus_ImageGetWidth($hImage)
    Local $ImageHeight = _GDIPlus_ImageGetHeight($hImage)
    Local $NewImageWidth = ($ImageWidth / 100) * 50
    Local $NewImageHeight = ($ImageHeight / 100) * 50
    _ImageResize($oFile, $nFile, $NewImageWidth, $NewImageHeight) ;Breite Höhe 50%
    ;_ImageResize($oFile, $nFile, $ImageWidth, $ImageHeight) ;Original

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

    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()

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

    ; Resize function
    Func _ImageResize($sInImage, $sOutImage, $iW, $iH)
    Local $hWnd, $hDC, $hBMP, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0

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

    ;OutFile path, to use later on.
    Local $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))

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

    ;OutFile name, to use later on.
    Local $sOF = StringMid($sOutImage, StringInStr($sOutImage, "\", 0, -1) + 1)

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

    ;OutFile extension , to use for the encoder later on.
    Local $Ext = StringUpper(StringMid($sOutImage, StringInStr($sOutImage, ".", 0, -1) + 1))

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

    ; Win api to create blank bitmap at the width and height to put your resized image on.
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH)
    _WinAPI_ReleaseDC($hWnd, $hDC)

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

    ;Start GDIPlus
    _GDIPlus_Startup()

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

    ;Get the handle of blank bitmap you created above as an image
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)

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

    ;Load the image you want to resize.
    $hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)

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

    ;Get the graphic context of the blank bitmap
    $hGraphic = _GDIPlus_ImageGetGraphicsContext ($hImage1)

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

    ;Draw the loaded image onto the blank bitmap at the size you want
    _GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iH)

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

    ;Get the encoder of to save the resized image in the format you want.
    $CLSID = _GDIPlus_EncodersGetCLSID($Ext)

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

    ;Generate a number for out file that doesn't already exist, so you don't overwrite an existing image.
    Do
    $i += 1
    Until (Not FileExists($sOP & $i & "_" & $sOF))

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

    ;Prefix the number to the begining of the output filename
    $sOutImage = $sOP & $i & "_" & $sOF

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

    ;Save the new resized image.
    _GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)

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

    ;Clean up and shutdown GDIPlus.
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _WinAPI_DeleteObject($hBMP)
    _GDIPlus_Shutdown()
    EndFunc ;==>_ImageResize

    [/autoit]

    Dass das 1. Skript weniger kb verbraucht ist logisch weil ja gleichzeitig auch die Breite und Höhe auf 50% reduziert werden. Wenn du die Routine mit den Originalmassen aufrufst ist die Grösse idedntisch zu meiner Lösung.

    Hier kannst du dir die Hilfe herunterladen. Darin solltest du auch die nativen AutoIt-Funktionen durcharbeiten, damit du die Grundlagen beherscht,

    mfg (Auto)Bert

  • Ich hatte mal dazu was geschrieben ;)

    Spoiler anzeigen
    [autoit]


    ;fast hack by UEZ
    #include <GuiConstantsEx.au3>
    #include <GDIPlus.au3>
    #include <ScreenCapture.au3>

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

    Opt('MustDeclareVars', 1)

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

    _Main()

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

    Func _Main()
    Local $hGUI1, $hGUI2, $hGUI3, $hImage, $hGraphic1, $hGraphic2, $hGraphic3, $sCLSID
    Local $bitmap1, $bitmap2, $bitmap3, $image1, $image2, $image3
    Local $tData, $tParams, $pParams, $pData

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

    ; Capture top left corner of the screen
    _ScreenCapture_Capture (@MyDocumentsDir & "\GDIPlus_Image.jpg", 0, 0, 400, 300)

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

    ; Create a GUI for the original image
    $hGUI1 = GUICreate("Original", 400, 300, 0, 0)
    GUISetState()

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

    ; Create a GUI for the scaled up image
    $hGUI2 = GUICreate("Scaled Up", 800, 600, 0, 350)
    GUISetState()

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

    ; Create a GUI for the scaled down image
    $hGUI3 = GUICreate("Scaled Down", 200, 150, 820, 400)
    GUISetState()

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

    ; Initialize GDI+ library and load image
    _GDIPlus_Startup ()
    $hImage = _GDIPlus_ImageLoadFromFile (@MyDocumentsDir & "\GDIPlus_Image.jpg")

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

    $sCLSID = _GDIPlus_EncodersGetCLSID ("JPG")

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

    ; Draw original image
    $hGraphic1 = _GDIPlus_GraphicsCreateFromHWND ($hGUI1)
    $bitmap1 = _GDIPlus_BitmapCreateFromGraphics(400, 300, $hGraphic1)
    $image1 = _GDIPlus_ImageGetGraphicsContext($bitmap1)
    _GDIPlus_GraphicsDrawImageRect($image1, $hImage, 0, 0, 400, 300)
    _GDIPlus_GraphicsDrawImageRect($hGraphic1, $bitmap1, 0, 0, 400, 300)
    _GDIPlus_ImageSaveToFileEx($bitmap1, @ScriptDir & "\GDIPlus_Image1.jpg", $sCLSID)
    _WinAPI_DeleteObject($bitmap1)
    _GDIPlus_GraphicsDispose($image1)

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

    ; Draw 2x scaled up image and save with lower jpg quality
    $hGraphic2 = _GDIPlus_GraphicsCreateFromHWND ($hGUI2)
    $bitmap2 = _GDIPlus_BitmapCreateFromGraphics(800, 600, $hGraphic2)
    $image2 = _GDIPlus_ImageGetGraphicsContext($bitmap2)
    _GDIPlus_GraphicsDrawImageRectRect ($image2, $hImage, 0, 0, 400, 300, 0, 0, 800, 600)
    _GDIPlus_GraphicsDrawImageRect($hGraphic2, $bitmap2, 0, 0, 800, 600)
    $tParams = _GDIPlus_ParamInit (1)
    $tData = DllStructCreate("int Quality")
    DllStructSetData($tData, "Quality", 10) ;quality 0-100
    $pData = DllStructGetPtr($tData)
    _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, $pData)
    $pParams = DllStructGetPtr($tParams)
    _GDIPlus_ImageSaveToFileEx($bitmap2, @ScriptDir & "\GDIPlus_Image2.jpg", $sCLSID, $pParams)
    _WinAPI_DeleteObject($bitmap2)
    _GDIPlus_GraphicsDispose($image2)
    $tData = 0

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

    ; Draw 2x scaled down image
    $hGraphic3 = _GDIPlus_GraphicsCreateFromHWND ($hGUI3)
    $bitmap3 = _GDIPlus_BitmapCreateFromGraphics(200, 150, $hGraphic3)
    $image3 = _GDIPlus_ImageGetGraphicsContext($bitmap3)
    _GDIPlus_GraphicsDrawImageRectRect ($image3, $hImage, 0, 0, 400, 300, 0, 0, 200, 150)
    _GDIPlus_GraphicsDrawImageRect($hGraphic3, $bitmap3, 0, 0, 200, 150)
    _GDIPlus_ImageSaveToFileEx($bitmap3, @ScriptDir & "\GDIPlus_Image3.jpg", $sCLSID)
    _WinAPI_DeleteObject($bitmap3)
    _GDIPlus_GraphicsDispose($image3)

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

    ; Release resources
    _GDIPlus_GraphicsDispose ($hGraphic1)
    _GDIPlus_GraphicsDispose ($hGraphic2)
    _GDIPlus_GraphicsDispose ($hGraphic3)
    _GDIPlus_ImageDispose ($hImage)
    _GDIPlus_Shutdown ()

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

    ; Clean up screen shot file
    FileDelete(@MyDocumentsDir & "\GDIPlus_Image.jpg")

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

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

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

    Exit

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

    EndFunc ;==>_Main

    [/autoit]

    Schaue dir die Zeilen 52-58 genauer an :D

    Gruß,
    UEZ