Bild vom Querformat in Hochformat Konvertieren Topic is solved

Stelle Fragen zur Programmierung mit Autohotkey

Moderator: jNizM

effel
Posts: 542
Joined: 16 Jan 2018, 13:34

Bild vom Querformat in Hochformat Konvertieren

Post by effel » 08 Apr 2021, 06:28

hallo,
wenn ich ein Querformat Foto im ListView anzeige, sieht das nicht so schön aus. Hat jemand eine Idee wie ich es mit AHK umgesetzt bekomme, das Foto vom Querformat ins Hochformat zu Konvertieren ?

z.B. hinein zoomen und den Fokus im Zentrum belassen? Auf der Webseite Sieht es gut aus.

20210408134137-Monitor-1.jpg
20210408134137-Monitor-1.jpg (36.92 KiB) Viewed 1536 times

20210408130851-Monitor-1.jpg
20210408130851-Monitor-1.jpg (212.54 KiB) Viewed 1545 times

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Bild vom Querformat in Hochformat Konvertieren

Post by BoBo » 08 Apr 2021, 08:01

Um die Spaltenbreite einheitlich zu halten, würde sich eine Format-Konvertierung anbieten, ähnlich wie wenn im quadratischen Instagram-Raster Bilder in abweichenden Formaten angezeigt werden sollen (also jeweils mit 'Balken' versehen). Das Zauberwort hierfür: "aspect ratio" - diese soll dabei erhalten bleiben.

http://autohotkey.com/board/topic/6297-display-picture-keep-aspect-ratio/?p=38307

effel
Posts: 542
Joined: 16 Jan 2018, 13:34

Re: Bild vom Querformat in Hochformat Konvertieren

Post by effel » 10 Apr 2021, 05:07

BoBo wrote:
08 Apr 2021, 08:01
... Das Zauberwort hierfür: "aspect ratio"
Hallo BoBo, meinst du sowas? https://www.autohotkey.com/boards/viewtopic.php?p=392132#p392132

Code: Select all

#Include Gdip_All.ahk
y := 935
Loop 4
   CreateSnap("1615|" . (y += 15) . "|30|15", "Hash" . A_Index . ".png", 3.3)

CreateSnap(coords, filePath, ratio := 1) {
   pToken := Gdip_Startup()
   pBitmap := Gdip_BitmapFromScreen(coords)
   if (ratio != 1) {
      RegExMatch(coords, "(?<width>\d+)\|(?<height>\d+)$", old)
      width := oldWidth*ratio, height := oldHeight*ratio
      pNewBitmap := Gdip_CreateBitmap(width, height)
      G := Gdip_GraphicsFromImage(pNewBitmap)
      Gdip_SetInterpolationMode(G, HighQualityBicubic := 7)
      Gdip_DrawImage(G, pBitmap, 0, 0, width, height, 0, 0, oldWidth, oldHeight)
      Gdip_DisposeImage(pBitmap), Gdip_DeleteGraphics(G)
      pBitmap := pNewBitmap
   }
   Gdip_SaveBitmapToFile(pBitmap, filePath)
   Gdip_DisposeImage(pBitmap)
   Gdip_Shutdown(pToken)
}

effel
Posts: 542
Joined: 16 Jan 2018, 13:34

Re: Bild vom Querformat in Hochformat Konvertieren

Post by effel » 10 Apr 2021, 05:11

effel wrote:
10 Apr 2021, 05:07
BoBo wrote:
08 Apr 2021, 08:01
... Das Zauberwort hierfür: "aspect ratio"
Hallo BoBo, meinst du sowas? https://www.autohotkey.com/boards/viewtopic.php?p=392132#p392132

EDIT:
Danke,- :) Du hast einen Link hinzugefügt, den habe ich nicht gesehen http://autohotkey.com/board/topic/6297-display-picture-keep-aspect-ratio/?p=38307

Code: Select all

#Include Gdip_All.ahk
y := 935
Loop 4
   CreateSnap("1615|" . (y += 15) . "|30|15", "Hash" . A_Index . ".png", 3.3)

CreateSnap(coords, filePath, ratio := 1) {
   pToken := Gdip_Startup()
   pBitmap := Gdip_BitmapFromScreen(coords)
   if (ratio != 1) {
      RegExMatch(coords, "(?<width>\d+)\|(?<height>\d+)$", old)
      width := oldWidth*ratio, height := oldHeight*ratio
      pNewBitmap := Gdip_CreateBitmap(width, height)
      G := Gdip_GraphicsFromImage(pNewBitmap)
      Gdip_SetInterpolationMode(G, HighQualityBicubic := 7)
      Gdip_DrawImage(G, pBitmap, 0, 0, width, height, 0, 0, oldWidth, oldHeight)
      Gdip_DisposeImage(pBitmap), Gdip_DeleteGraphics(G)
      pBitmap := pNewBitmap
   }
   Gdip_SaveBitmapToFile(pBitmap, filePath)
   Gdip_DisposeImage(pBitmap)
   Gdip_Shutdown(pToken)
}

just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Bild vom Querformat in Hochformat Konvertieren

Post by just me » 10 Apr 2021, 05:51

Moin,

ein Problem ist die ImageList, die an sich nur Bilder identischer Größe nimmt. Es gibt ab Win Vista einen Style, der auch kleinere Bilder akzeptiert, aber das sieht nicht immer überzeugend aus. Besser wäre es deshalb, aus einem 'Fehlformat' den passenden Auschnitt auf die gewünschte Größe zu skalieren. Dafür muss man allerdings wissen, wo im Bild sich der passende Ausschnitt befindet.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Bild vom Querformat in Hochformat Konvertieren

Post by BoBo » 10 Apr 2021, 08:37

Der verlinkte Beitrag skaliert das Image unter Beibehalt der 'aspect ratio' (Seitenverhältnis). Da die Abmessungen der 'Bild-'Zelle' im ListView-Raster bekannt ist, lässt sich das Bild in dieser Größe entsprechend einbetten. Damit wäre (AFAICS) eine dynamische Anpassung des Bildes möglich, ohne fixe Umwandlung.
@just me's Vorschlag wäre vorzuziehen, da so Bildbestandteile abgeschnitten werden, welche weniger von Interesse sind, und so die Abbildungsgröße beibehalten werden kann. IMHO automatisiert jedoch nur schwer fass-/umsetzbar (Bilderkennung/face detection), und somit fraglich bzgl des Aufwands :)

effel
Posts: 542
Joined: 16 Jan 2018, 13:34

Re: Bild vom Querformat in Hochformat Konvertieren

Post by effel » 10 Apr 2021, 09:34

Danke eure Zeit,

so schlimm sieht es nun auch wieder nicht aus, das es unbedingt gelöst werden muss.

Ich habe in der Vergangenheit hier sehr viel mit Bildern gesehen, was ich für nicht möglich hielt.

Da nahm ich an das es ein WenigZeiler wird, wenn vom Bildzentrum heraus gezoomt wird bis ein Bildrand erreicht wird und dann ein Hochformat ausschneiden.

just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Bild vom Querformat in Hochformat Konvertieren

Post by just me » 11 Apr 2021, 04:43

Moin,

wenn die Bilder als Dateien vorhanden sind und Du sie vom Zentrum ausgehend auf ein vorgegebenes Seitenverhältnis zuschneiden willst, geht das ohne allzu großen Aufwand. Willst Du das mal probieren?

effel
Posts: 542
Joined: 16 Jan 2018, 13:34

Re: Bild vom Querformat in Hochformat Konvertieren

Post by effel » 11 Apr 2021, 05:07

Hallo Just Me, ja gerne

User avatar
Frosti
Posts: 426
Joined: 27 Oct 2017, 14:30
Contact:

Re: Bild vom Querformat in Hochformat Konvertieren

Post by Frosti » 11 Apr 2021, 05:30

Wenn ich es perfekt haben wollte, würde ich eine HTML Seite erstellen. Mit Neutron.ahk kann man die Nutzerinteraktion handhaben. Mit entsprechender Kenntnis in CSS und HTML gibt es auch keine Probleme bei unterschiedlichen Auflösungen. Für mich ist das aber noch ein mächtiges gefummel am HTML Code, weil ich mich da zu wenig mit beschäftige. Gemacht habe ich das schon, weil mein Ziel über die Windows Steuerelemente nicht möglich war.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Bild vom Querformat in Hochformat Konvertieren

Post by BoBo » 11 Apr 2021, 05:34

Für das vorab gezeigte Bild ist das durchaus tolerabel :arrow: https://imgur.com/a/NJtlNrM (wobei die gezeigte Spalte für alle Bilder in der Breite bereits gestaucht scheint).

240x320 (zielgröße)
458x320 (aktuelle größe)
218 (differenz)
106 (differenz/2 = zu entfernende bildanteile in px)

just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Bild vom Querformat in Hochformat Konvertieren

Post by just me » 12 Apr 2021, 05:31

Moin,

ich have vor längerer Zeit ein paar Windowsfunktionen für Bildbearbeitung nach AHK übersetzt: WIA - Windows Image Acquisition. Damit kann man u.a. recht einfach Bilder zuschneiden. Ich habe den Code der Kernfunktionen in eine eigene Funktion gepackt. Vielleicht kannst Du das brauchen.

Der Funktion WIA_GetCroppedImage() werden der Pfad zur Bilddatei und die Werte für Breite und Höhe zur Bestimmung des Seitenverhältnisses übergeben. Die Funktion sucht sich dann vom Bildzentrum aus den größten passenden Bildauschnitt und gibt ein HBITMAP Handle für diesen Auschnitt zurück. Wenn Du den Ausschnitt innerhalb der Funktion auf die übergebenen Zielwerte skalieren willst, kannst du in CopyImage() als dritten und vierten Parameter RatioW und RatioH einsetzen.

Code: Select all

; ======================================================================================================================
; WIA - Windows Image Acquisition -> https://www.autohotkey.com/boards/viewtopic.php?f=6&t=7254
; ======================================================================================================================
#NoEnv
SetBatchLines, -1
Loop, Files, %A_WinDir%\Web\WallPaper\*.jpg, R
   ImgPath := A_LoopFileLongPath
Until (ImgPath <> "")
If (ImgPath = "") {
   MsgBox, 16, Error, Couldn't find a JPG image file!`n`nThe program will exit.
   ExitApp
}
; ImgPath := "TestW.png"
PicW := 240
PicH := 320
HBM := WIA_GetCroppedImage(ImgPath, PicW, PicH)
Gui, Add, Pic, w%PicW% h%PicH% vPicVar, HBITMAP:%HBM%
Gui, Show, , Cropped & Scaled
Return
GuiClose:
ExitApp
; ======================================================================================================================
WIA_GetCroppedImage(ImgPath, RatioW, RatioH) {
   ; WIA_LoadImage()
   ImgObj := ComObjCreate("WIA.ImageFile")
   ComObjError(0)
   ImgObj.LoadFile(ImgPath)
   ComObjError(1)
   If (A_LastError)
      Return !(ErrorLevel := A_LastError)
   ; Calculate the number of pixels to crop
   SFH := RatioH / RatioW
   SFW := RatioW / RatioH
   ImgW := ImgObj.Width
   ImgH := ImgObj.Height
   NewW := ImgW
   NewH := ImgW * SFH
   If (NewH > ImgH) {
      NewH := ImgH
      NewW := NewH * SFW
   }
   If (NewW > ImgW)
      NewH := NewW * SFH
   CropW := Floor((ImgW - NewW) // 2)
   CropH := Floor((ImgH - NewH) // 2)
   ; WIA_CropImage()
   ImgProc := ComObjCreate("WIA.ImageProcess")
   ImgProc.Filters.Add(ImgProc.FilterInfos("Crop").FilterID)
   ImgProc.Filters[1].Properties("Left")   := CropW
   ImgProc.Filters[1].Properties("Top")    := CropH
   ImgProc.Filters[1].Properties("Right")  := CropW
   ImgProc.Filters[1].Properties("Bottom") := CropH
   ImgObj := ImgProc.Apply(ImgObj)
   ; WIA_GetImageBitmap() - to retrieve the HBITMAP handle for the returned object use object.Handle
   Bitmap := ImgObj.Filedata.Picture
   ; Return a HBITMAP handle to a copy of the bitmap
   Return DllCall("CopyImage", "Ptr", Bitmap.Handle, "UInt", 0, "Int", 0, "Int", 0, "UInt", 0x00002000, "UPtr")
}

effel
Posts: 542
Joined: 16 Jan 2018, 13:34

Re: Bild vom Querformat in Hochformat Konvertieren

Post by effel » 12 Apr 2021, 14:01

Hallo Just Me,
mir gehen die Superlative aus, aber Hans Rosenthal würde sicherlich 'Spitze' springen :bravo:

Danke für deine Mühen!

Für den geneigten Leser poste ich einen Screen:

20210412195357-Monitor-1.jpg
20210412195357-Monitor-1.jpg (259.85 KiB) Viewed 1325 times

Zeigst du mir noch wie das geCroppedImage gespeichert wird?

Wenn das dann alles noch in einer Funktion, evt. mit Übergabe von Pfad und oder Pfad/Name geht, sind den Möglichkeiten keine Grenzen gesetzt

Klägliche Versuche mit WIA_SaveImage(A_ScriptDir "\ImgObj.jpg", A_ScriptDir)
möchte ich nicht näher erwähnen..

Code: Select all

;Bild vom Querformat in Hochformat Konvertieren
;https://www.autohotkey.com/boards/viewtopic.php?p=393389#p393389
; ======================================================================================================================
; WIA - Windows Image Acquisition -> https://www.autohotkey.com/boards/viewtopic.php?f=6&t=7254
; ======================================================================================================================
#NoEnv
SetBatchLines, -1
Loop, Files, %A_WinDir%\Web\WallPaper\*.jpg, R
   ImgPath := A_LoopFileLongPath
Until (ImgPath <> "")
If (ImgPath = "") {
   MsgBox, 16, Error, Couldn't find a JPG image file!`n`nThe program will exit.
   ExitApp
}
; ImgPath := "TestW.png"

PicW := 1480
PicH := 600
HBM := WIA_GetCroppedImage(ImgPath, PicW, PicH)
Gui, 1:Add, Pic, w%PicW% h%PicH% vPicVar2, HBITMAP:%HBM%
Gui, 1:Show, , Cropped & Scaled 1480x600

;WIA_SaveImage(A_ScriptDir "\ImgObj.jpg", A_ScriptDir)

PicW := 480
PicH := 640
HBM := WIA_GetCroppedImage(ImgPath, PicW, PicH)
Gui, 2:Add, Pic, w%PicW% h%PicH% vPicVar2, HBITMAP:%HBM%
Gui, 2:Show, , Cropped & Scaled 480x640

PicW := 240
PicH := 320
HBM := WIA_GetCroppedImage(ImgPath, PicW, PicH)
Gui, 3:Add, Pic, w%PicW% h%PicH% vPicVar2, HBITMAP:%HBM%
Gui, 3:Show, , Cropped & Scaled 240x320

Return
GuiClose:
ExitApp
; ======================================================================================================================
WIA_GetCroppedImage(ImgPath, RatioW, RatioH) {
   ; WIA_LoadImage()
   ImgObj := ComObjCreate("WIA.ImageFile")
   ComObjError(0)
   ImgObj.LoadFile(ImgPath)
   ComObjError(1)
   If (A_LastError)
      Return !(ErrorLevel := A_LastError)
   ; Calculate the number of pixels to crop
   SFH := RatioH / RatioW
   SFW := RatioW / RatioH
   ImgW := ImgObj.Width
   ImgH := ImgObj.Height
   NewW := ImgW
   NewH := ImgW * SFH
   If (NewH > ImgH) {
      NewH := ImgH
      NewW := NewH * SFW
   }
   If (NewW > ImgW)
      NewH := NewW * SFH
   CropW := Floor((ImgW - NewW) // 2)
   CropH := Floor((ImgH - NewH) // 2)
   ; WIA_CropImage()
   ImgProc := ComObjCreate("WIA.ImageProcess")
   ImgProc.Filters.Add(ImgProc.FilterInfos("Crop").FilterID)
   ImgProc.Filters[1].Properties("Left")   := CropW
   ImgProc.Filters[1].Properties("Top")    := CropH
   ImgProc.Filters[1].Properties("Right")  := CropW
   ImgProc.Filters[1].Properties("Bottom") := CropH
   ImgObj := ImgProc.Apply(ImgObj)
   ; WIA_GetImageBitmap() - to retrieve the HBITMAP handle for the returned object use object.Handle
   Bitmap := ImgObj.Filedata.Picture
   ; Return a HBITMAP handle to a copy of the bitmap
   Return DllCall("CopyImage", "Ptr", Bitmap.Handle, "UInt", 0, "Int", 0, "Int", 0, "UInt", 0x00002000, "UPtr")
}




WIA_SaveImage(ImgObj, ImgPath) {
   If (ComObjType(ImgObj, "Name") <> "IImageFile")
      Return False
   SplitPath, ImgPath, FileName, FileDir, FileExt
   If (ImgObj.FileExtension <> FileExt)
      Return False
   ComObjError(0)
   ImgObj.SaveFile(ImgPath)
   ComObjError(1)
   Return !A_LastError
}

just me wrote:
12 Apr 2021, 05:31
CopyImage()[/c] als dritten und vierten..

Das habe ich leider nicht verstanden. Kannst du bitte ein Beispiel senden?

effel
Posts: 542
Joined: 16 Jan 2018, 13:34

Re: Bild vom Querformat in Hochformat Konvertieren

Post by effel » 12 Apr 2021, 15:20

eins geht noch...
20210412221919-Monitor-1.jpg
20210412221919-Monitor-1.jpg (197.5 KiB) Viewed 1306 times

just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Bild vom Querformat in Hochformat Konvertieren  Topic is solved

Post by just me » 13 Apr 2021, 10:47

Hallo effel,

meine Funktion returniert ein Bitmaphandle (HBITMap). Das ist nur bedingt zum Speichern als Datei geeignet und WIA_SaveImage() kann damit überhaupt nichts anfangen.

Wenn Du Speichern willst, würde ich folgendes Verfahren vorschlagen:
  1. Die Bilddatei mit WIA_LoadImage() laden. Die Funktion liefert ein ImageFile Objekt.
  2. Dieses Objekt kannst Du an die Funktion WIA_CropAndScale() übergeben. Anders als die alte Version skaliert diese Funktion das Bild zusätzlich auf die übergebene Größe (Bedingung: Scale = True) und gibt widerum ein ImageFile Objekt zurück.
  3. Diesem Objekt kannst Du mit WIA_GetHBITMAP() eine Handle (HBITMAP) auf eine Kopie der enthaltenen Bitmap entlocken, das Du in AHK weiterverwenden kannst.
  4. Außerdem kannst Du das Objekt mit WIA_SaveImage() speichern. Du musst beachten, dass die Funktion keine vorhandenen Dateien überschreibt. Also musst Du der zu speichernden Datei einen neuen Namen geben oder die vorhandene vorher umbenennen oder löschen. Die Dateiendung muss beibehalten werden.

Code: Select all

; ======================================================================================================================
; WIA - Windows Image Acquisition -> https://www.autohotkey.com/boards/viewtopic.php?f=6&t=7254
; ======================================================================================================================
#NoEnv
SetBatchLines, -1
Loop, Files, %A_WinDir%\Web\WallPaper\*.jpg, R
   ImgPath := A_LoopFileLongPath
Until (ImgPath <> "")
If (ImgPath = "") {
   MsgBox, 16, Error, Couldn't find a JPG image file!`n`nThe program will exit.
   ExitApp
}
; ImgPath := "TestW.png"
; ----------------------------------------------------------------------------------------------------------------------
If !(ImgObj := WIA_LoadImage(ImgPath)) {
   MsgBox, 16, Fehler!, Die Datei`n   %ImgPath%`nkonnte nicht geladen werden!
   ExitApp
}
PicW := 240
PicH := 320
If !(ScaledObj := WIA_CropAndScale(ImgObj, PicW, PicH)) {
   MsgBox, 16, Fehler!, Das Bild konnte nicht bearbeitet werden!
   ExitApp
}
HBM := WIA_GetHBITMAP(ScaledObj)
; ----------------------------------------------------------------------------------------------------------------------
Gui, Add, Pic, w%PicW% h%PicH% vPicVar, HBITMAP:%HBM%
Gui, Show, , Cropped & Scaled
Return
; ----------------------------------------------------------------------------------------------------------------------
GuiClose:
ExitApp
; ======================================================================================================================
; Loads an image file.
; Parameters:
;     ImgPath     -  Path and name of the image file.
; ======================================================================================================================
WIA_LoadImage(ImgPath) {
   ImgObj := ComObjCreate("WIA.ImageFile")
   ComObjError(0)
   ImgObj.LoadFile(ImgPath)
   ComObjError(1)
   Return A_LastError ? False : ImgObj
}
; ======================================================================================================================
WIA_CropAndScale(ImgObj, RatioW, RatioH, Scale := True) {
   If (ComObjType(ImgObj, "Name") <> "IImageFile")
      Return False
   ; Calculate the number of pixels to crop
   ImgW := ImgObj.Width
   ImgH := ImgObj.Height
   If (ImgW / RatioW) < (ImgH / RatioH)
      NewH := Floor((NewW := ImgW) * (RatioH / RatioW))
   Else
      NewW := Floor((NewH := ImgH) * (RatioW / RatioH))
   CropW := Floor((ImgW - NewW) // 2)
   CropH := Floor((ImgH - NewH) // 2)
   ; WIA_CropImage()
   ImgProc := ComObjCreate("WIA.ImageProcess")
   ImgProc.Filters.Add(ImgProc.FilterInfos("Crop").FilterID)
   ImgProc.Filters[1].Properties("Left")   := CropW
   ImgProc.Filters[1].Properties("Top")    := CropH
   ImgProc.Filters[1].Properties("Right")  := CropW
   ImgProc.Filters[1].Properties("Bottom") := CropH
   ; WIA_ScaleImage() - if required
   If (Scale) {
      ImgProc.Filters.Add(ImgProc.FilterInfos("Scale").FilterID)
      ImgProc.Filters[2].Properties("MaximumWidth")        := RatioW
      ImgProc.Filters[2].Properties("MaximumHeight")       := RatioH
      ImgProc.Filters[2].Properties("PreserveAspectRatio") := 0
   }
   ; Apply
   Return ImgProc.Apply(ImgObj)
}
; ======================================================================================================================
WIA_GetHBITMAP(ImgObj) {
   If (ComObjType(ImgObj, "Name") <> "IImageFile")
      Return False
   ; Return a HBITMAP handle to a copy of the bitmap
   Img := ImgObj.Filedata.Picture
   Return DllCall("CopyImage", "Ptr", Img.Handle, "UInt", 0, "Int", 0, "Int", 0, "UInt", 0x00002000, "UPtr")
}
; ======================================================================================================================
; Saves an image to disk.
; Parameters:
;     ImgPath     -  Path and name of the image file.
; Return values:
;     True on success, otherwise false. The error code is stored in A_LastError.
; Note:
;     The extension of the file name passed in ImgPath must be one of "bmp", "gif", "jpg", "png", or "tif" and match
;     the extension stored in the FileExtension property of the ImageFile object. Otherwise the file won't be stored.
;     Also, the function won't overwrite existing files.
; ======================================================================================================================
WIA_SaveImage(ImgObj, ImgPath) {
   If (ComObjType(ImgObj, "Name") <> "IImageFile")
      Return False
   SplitPath, ImgPath, FileName, FileDir, FileExt
   If (ImgObj.FileExtension <> FileExt)
      Return False
   ComObjError(0)
   ImgObj.SaveFile(ImgPath)
   ComObjError(1)
   Return !A_LastError
}

effel
Posts: 542
Joined: 16 Jan 2018, 13:34

Re: Bild vom Querformat in Hochformat Konvertieren

Post by effel » 13 Apr 2021, 15:13

Boah echt mal, das ist einfach der Wahnsinn, das ist meine Standard Lib wenn ich in welcher auch immer Gui mehrere Bilder auf einem fix vorgegebenen Place situieren muss, vielen Dank das du das hier geteilt hast.

Deine Stunts hier sind manchmal sowas von.. :o

Für die Leser und damit ich es immer wieder finde:

Code: Select all

If !FileExist(A_ScriptDir "\save")
FileCreateDir, % A_ScriptDir "\save"
FileCopy, % A_ScriptFullPath, % A_ScriptDir "\save\" A_ScriptName " save " DateTime " .ahk"

; ======================================================================================================================
; WIA - Windows Image Acquisition -> https://www.autohotkey.com/boards/viewtopic.php?f=6&t=7254 by Just Me
; https://www.autohotkey.com/boards/viewtopic.php?p=393617#p393617 by Just Me
; ======================================================================================================================

#NoEnv
SetBatchLines, -1
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Loop, Files, %A_WinDir%\Web\WallPaper\*.jpg, R
   ImgPath := A_LoopFileLongPath
Until (ImgPath <> "")
If (ImgPath = "") {
   MsgBox, 16, Error, Couldn't find a JPG image file!`n`nThe program will exit.
   ExitApp
}


ImgPathSave := A_Now A_Space "Test.jpg"

;ImgPath := "M:\__ahk projekte aktuell\InternetMovieDatenbank\lib\WIA_GetCroppedImage\Detail suche History\Neuer Ordner\tt0480249 nm2405238 Willow5.jpg"


; ----------------------------------------------------------------------------------------------------------------------
;  1.
; Die Bilddatei mit WIA_LoadImage() laden. 
; Die Funktion liefert ein ImageFile Objekt.
; ----------------------------------------------------------------------------------------------------------------------
If !(ImgObj := WIA_LoadImage(ImgPath)) {
   MsgBox, 16,,%  ImgPath "`n" errorlevel
   ExitApp
}

PicW := 480
PicH := 640

; ----------------------------------------------------------------------------------------------------------------------
;  2.
; Dieses Objekt kannst Du an die Funktion WIA_CropAndScale() übergeben. 
; Anders als die alte Version skaliert diese Funktion das Bild zusätzlich 
; auf die übergebene Größe (Bedingung: Scale = True) und gibt widerum ein ImageFile Objekt zurück.
; ----------------------------------------------------------------------------------------------------------------------
If !(ScaledObj := WIA_CropAndScale(ImgObj, PicW, PicH, True)) {
   MsgBox, 16, Fehler!, Das Bild konnte nicht bearbeitet werden!
   ExitApp
}


HBM := WIA_GetHBITMAP(ScaledObj)

; ======================================================================================================================
; Saves an image to disk.
; Parameters:
;     ImgPath     -  Path and name of the image file.
; Return values:
;     True on success, otherwise false. The error code is stored in A_LastError.
; Note:
;     The extension of the file name passed in ImgPath must be one of "bmp", "gif", "jpg", "png", or "tif" and match
;     the extension stored in the FileExtension property of the ImageFile object. Otherwise the file won't be stored.
;     Also, the function won't overwrite existing files.
; WIA_SaveImage(HBM, A_ScriptDir "\" A_Now " picture.jpg")

WIA_SaveImage(ScaledObj, ImgPathSave)

; ======================================================================================================================
; ----------------------------------------------------------------------------------------------------------------------
;  3.
; Diesem Objekt kannst Du mit WIA_GetHBITMAP() eine Handle (HBITMAP) auf eine Kopie der enthaltenen Bitmap entlocken, 
; das Du in AHK weiterverwenden kannst.
;   Außerdem kannst Du das Objekt mit WIA_SaveImage() speichern. 
;     Du musst beachten, dass die Funktion keine vorhandenen Dateien überschreibt. 
;       Also musst Du der zu speichernden Datei einen neuen Namen geben 
;       oder die vorhandene vorher umbenennen oder löschen. 
; Die Dateiendung muss beibehalten werden
; ----------------------------------------------------------------------------------------------------------------------

Gui, Add, Pic, w%PicW% h%PicH% vPicVar, HBITMAP:%HBM%
Gui, Show, , Cropped & Scaled
Return

; ----------------------------------------------------------------------------------------------------------------------

GuiClose:
ExitApp

; ======================================================================================================================
; ======================================================================================================================
; Loads an image file.
; Parameters:
;     ImgPath     -  Path and name of the image file.
; ======================================================================================================================
WIA_LoadImage(ImgPath) {
   ImgObj := ComObjCreate("WIA.ImageFile")
   ComObjError(0)
   ImgObj.LoadFile(ImgPath)
   ComObjError(1)
   Return A_LastError ? False : ImgObj
}
; ======================================================================================================================
WIA_CropAndScale(ImgObj, RatioW, RatioH, Scale := True) {
   If (ComObjType(ImgObj, "Name") <> "IImageFile")
      Return False
   ; Calculate the number of pixels to crop
   ImgW := ImgObj.Width
   ImgH := ImgObj.Height
   If (ImgW / RatioW) < (ImgH / RatioH)
      NewH := Floor((NewW := ImgW) * (RatioH / RatioW))
   Else
      NewW := Floor((NewH := ImgH) * (RatioW / RatioH))
   CropW := Floor((ImgW - NewW) // 2)
   CropH := Floor((ImgH - NewH) // 2)
   ; WIA_CropImage()
   ImgProc := ComObjCreate("WIA.ImageProcess")
   ImgProc.Filters.Add(ImgProc.FilterInfos("Crop").FilterID)
   ImgProc.Filters[1].Properties("Left")   := CropW
   ImgProc.Filters[1].Properties("Top")    := CropH
   ImgProc.Filters[1].Properties("Right")  := CropW
   ImgProc.Filters[1].Properties("Bottom") := CropH
   ; WIA_ScaleImage() - if required
   If (Scale) {
      ImgProc.Filters.Add(ImgProc.FilterInfos("Scale").FilterID)
      ImgProc.Filters[2].Properties("MaximumWidth")        := RatioW
      ImgProc.Filters[2].Properties("MaximumHeight")       := RatioH
      ImgProc.Filters[2].Properties("PreserveAspectRatio") := 0
   }
   ; Apply
   Return ImgProc.Apply(ImgObj)
}
; ======================================================================================================================
WIA_GetHBITMAP(ImgObj) {
   If (ComObjType(ImgObj, "Name") <> "IImageFile")
      Return False
   ; Return a HBITMAP handle to a copy of the bitmap
   Img := ImgObj.Filedata.Picture
   Return DllCall("CopyImage", "Ptr", Img.Handle, "UInt", 0, "Int", 0, "Int", 0, "UInt", 0x00002000, "UPtr")
}
; ======================================================================================================================
; Saves an image to disk.
; Parameters:
;     ImgPath     -  Path and name of the image file.
; Return values:
;     True on success, otherwise false. The error code is stored in A_LastError.
; Note:
;     The extension of the file name passed in ImgPath must be one of "bmp", "gif", "jpg", "png", or "tif" and match
;     the extension stored in the FileExtension property of the ImageFile object. Otherwise the file won't be stored.
;     Also, the function won't overwrite existing files.
; ======================================================================================================================
WIA_SaveImage(ImgObj, ImgPath) {
   If (ComObjType(ImgObj, "Name") <> "IImageFile")
      Return False
   SplitPath, ImgPath, FileName, FileDir, FileExt
   If (ImgObj.FileExtension <> FileExt)
      Return False
   ComObjError(0)
   ImgObj.SaveFile(ImgPath)
   ComObjError(1)
   Return !A_LastError
}
20210413220043-Monitor-1.jpg
20210413220043-Monitor-1.jpg (142.94 KiB) Viewed 1246 times

Post Reply

Return to “Ich brauche Hilfe”