Jump to content

MS Paint, change size for multiple files and save to path


Recommended Posts

Hello AutoIT masters

I am gonna start writing a fun little script to resize X amount of images with the Horizontal/Vertical aspects to 15/15 instead of the standard 100/100
558ca72d2279f09a94df64f9ca623a2a.png

I need to be able to do this with X amount of images and after changing the dimensions I need to save all the images in a path.
so to keep things simple.

1: File open prompt
2: Choose X amount of images
3: Automatically choose 15/15 for all images
4: Save in path chosen by user
5: Might be more to come.
6: the faster the better!
 

I am gonna start this project tomorrow, any help/references is highly appreciated!

Thanks in advance and Wish me luck;)

 

 

#### I Hit a bump on the road ###

I successfully manage to do what I want to do and I successfully manage to rewrite names but 3 of the image names won't rename?

So lets say I have 10 images that all get resized, then 7 of them gets renamed but not the last three, confuses me quite a bit?
Thanks in advance!
Rename script:
 

Func renameall()
local $path = @ScriptDir & "\" & $nymappe & "\"
local $ret
Local $hSearch = FileFindFirstFile($path & "*.jpg")
$i = 1
While 1
    $sFileName = FileFindNextFile($hSearch)
    If @error Then ExitLoop
    if FileMove($path & $sFileName, $path & String("NewName" & +$i) & ".jpg", 0) = 1 then
        ConsoleWrite($path & $sFileName & ' renamed to ' & $path & String("NewName" & +$i) & ".jpg" & @LF)
    Else
        ConsoleWrite('File rename failed for file = ' & $path & $sFileName & @LF)
    endif
    $i += 1
WEnd
FileClose($hSearch)
EndFunc

Full script:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=car.ico
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GDIPlus.au3>
#include <File.au3>
#include <Array.au3>
#include <ColorConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ProgressConstants.au3>

; Declare array
Dim $Images[1]
; Gets all JPG files in the current directory (@ScriptDir).
Local $search = FileFindFirstFile("*.jpg")

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

; 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

; Close the search handle
FileClose($search)

; Create directory "resized" if not there yet
$nymappe = InputBox("Mappe / Bil Navn", "Mappe / Bil Navn")
If NOT FileExists(@ScriptDir & "\" & $nymappe & "\") Then
    DirCreate(@ScriptDir & "\" & $nymappe & "\")
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) * 15
        Local $NewImageHeight = ($ImageHeight / 100) * 15

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

; 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()
    Call("renameall")
EndFunc

Func renameall()
local $path = @ScriptDir & "\" & $nymappe & "\"
local $ret
Local $hSearch = FileFindFirstFile($path & "*.jpg")
$i = 1
While 1
    $sFileName = FileFindNextFile($hSearch)
    If @error Then ExitLoop
    if FileMove($path & $sFileName, $path & String("NewName" & +$i) & ".jpg", 0) = 1 then
        ConsoleWrite($path & $sFileName & ' renamed to ' & $path & String("NewName" & +$i) & ".jpg" & @LF)
    Else
        ConsoleWrite('File rename failed for file = ' & $path & $sFileName & @LF)
    endif
    $i += 1
WEnd
FileClose($hSearch)
EndFunc

 

Edited by RyukShini
Hit a bump....
Link to comment
Share on other sites

To resize images use IrfanView ;)
Has a batch mode to do this on a lot of images.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

1 minute ago, water said:

To resize images use IrfanView ;)
Has a batch mode to do this on a lot of images.

Thanks but I could easily do this on photoshop as well using a macro or whatever.
but I am looking to do this only by using AutoIT and MS Paint since it is a project to ease the tasks my brother do in he's company.
Anyways thanks a lot for the suggestion @water
I will also be uploading all files to a site, but thats when everything else runs ;)

Link to comment
Share on other sites

1 minute ago, Synapsee said:
15 / 15 = 1
100 / 100 = 1

i can't understand

Sorry if I am being unclear, I haven't written any code yet :)
I will start tmr, heading home now.
but to keep things short!
I am in need of opening say 10 image files.
After opening the images I need to Resize the images(Just automatically input 15 in the horizontal which will automatically change the vertical to 15 as well, since it maintains the aspects)
After changing the values of all 10 images I need a Save As prompt to open and I would be able to choose where to save the 10 edited images.
dafa40c9a686c7904b821f36dd95f5c6.gif

826fb8dc39014ece89e3dc2ad4b2ecb5.gif

 

 

Link to comment
Share on other sites

so a 1000x1000 pixel img, give a 150x150 pixel img ?

i have this script on my drive, resize image if > 800x800 to 800x800.

u can probably adapt it for u.

something like :

pseudo code :

_ImageResize($sInImage, $sOutImage, $iW * 0.15, $iH * 0.15)

 

main code source :

my script :

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

Func _ArrayAddColumns(ByRef $aArr, $iNumColToAdd = 1)
    If IsArray($aArr) = 0 Then Return SetError(1, 0, -1) ; Filter out non-array
    If $iNumColToAdd < 1 Then Return SetError(2, 0, -1) ; $iNumColToAdd must be greater than zero to add a column.
    If UBound($aArr, 0) > 2 Then Return SetError(3, 0, -1) ; Only allows a 1d or 2d array pass this line.

    If UBound($aArr, 0) = 1 Then ; ====== For 1d array ========
        Local $aRet[UBound($aArr)][$iNumColToAdd + 1] ; Create new 2d array.
        For $r = 0 To UBound($aArr) - 1
            $aRet[$r][0] = $aArr[$r]
        Next
    Else ; ======= For 2d array ============
        Local $aRet = $aArr ; So that ByRef $aArr is not altered outside of function.
        ReDim $aRet[UBound($aRet)][UBound($aRet, 2) + $iNumColToAdd] ; ReDim 2d array only.
    EndIf

    Return $aRet
EndFunc   ;==>_ArrayAddColumns

$list = _FileListToArrayRec("..\Crop","*.jpg;*.jpeg",0,0,0,2)
$list_filename = _FileListToArrayRec("..\Crop","*.jpg;*.jpeg",0,0,0,0)
$list = _ArrayAddColumns($list, 2)
;_ArrayDisplay($list)

for $i = 1 to UBound($list)-1

_GDIPlus_Startup()
$FileLoad = _GDIPlus_ImageLoadFromFile(_PathFull($list[$i][0]))
$h = _GDIPlus_ImageGetHeight($FileLoad)
$w = _GDIPlus_ImageGetWidth($FileLoad)
_GDIPlus_Shutdown()

$list[$i][1] = $w
$list[$i][2] = $h

next

_ArrayDisplay($list)

for $i = 1 to UBound($list)-1

    $temp = @ScriptDir & "\" & $list_filename[$i]
    $temp_resize = StringReplace($temp, ".jpeg", ".jpg")

    if $list[$i][1] = $list[$i][2] Then
        if $list[$i][1] > 800 Then
            _ImageResize($list[$i][0], $temp_resize, 800, 800)
        Else
            FileCopy($list[$i][0], $temp)
        endif
    Else
        ;Get Min
        if $list[$i][1] < $list[$i][2] Then
            if  $list[$i][1] > 800 then
                _ImageResize($list[$i][0], $temp_resize, 800, 800)
            Else
                _ImageResize($list[$i][0], $temp_resize, $list[$i][1], $list[$i][1])
            endif
        Else
            if  $list[$i][2] > 800 then
                _ImageResize($list[$i][0], $temp_resize, 800, 800)
            Else
                _ImageResize($list[$i][0], $temp_resize, $list[$i][2], $list[$i][2])
            endif
        Endif
    endif

next

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

    $sOutImage = $sOP & $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

Exit




;135

 

Link to comment
Share on other sites

8 minutes ago, Synapsee said:

so a 1000x1000 pixel img, give a 150x150 pixel img ?

i have this script on my drive, resize image if > 800x800 to 800x800.

u can probably adapt it for u.

something like :

pseudo code :

_ImageResize($sInImage, $sOutImage, $iW * 0.15, $iH * 0.15)

 

main code source :

my script :

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

Func _ArrayAddColumns(ByRef $aArr, $iNumColToAdd = 1)
    If IsArray($aArr) = 0 Then Return SetError(1, 0, -1) ; Filter out non-array
    If $iNumColToAdd < 1 Then Return SetError(2, 0, -1) ; $iNumColToAdd must be greater than zero to add a column.
    If UBound($aArr, 0) > 2 Then Return SetError(3, 0, -1) ; Only allows a 1d or 2d array pass this line.

    If UBound($aArr, 0) = 1 Then ; ====== For 1d array ========
        Local $aRet[UBound($aArr)][$iNumColToAdd + 1] ; Create new 2d array.
        For $r = 0 To UBound($aArr) - 1
            $aRet[$r][0] = $aArr[$r]
        Next
    Else ; ======= For 2d array ============
        Local $aRet = $aArr ; So that ByRef $aArr is not altered outside of function.
        ReDim $aRet[UBound($aRet)][UBound($aRet, 2) + $iNumColToAdd] ; ReDim 2d array only.
    EndIf

    Return $aRet
EndFunc   ;==>_ArrayAddColumns

$list = _FileListToArrayRec("..\Crop","*.jpg;*.jpeg",0,0,0,2)
$list_filename = _FileListToArrayRec("..\Crop","*.jpg;*.jpeg",0,0,0,0)
$list = _ArrayAddColumns($list, 2)
;_ArrayDisplay($list)

for $i = 1 to UBound($list)-1

_GDIPlus_Startup()
$FileLoad = _GDIPlus_ImageLoadFromFile(_PathFull($list[$i][0]))
$h = _GDIPlus_ImageGetHeight($FileLoad)
$w = _GDIPlus_ImageGetWidth($FileLoad)
_GDIPlus_Shutdown()

$list[$i][1] = $w
$list[$i][2] = $h

next

_ArrayDisplay($list)

for $i = 1 to UBound($list)-1

    $temp = @ScriptDir & "\" & $list_filename[$i]
    $temp_resize = StringReplace($temp, ".jpeg", ".jpg")

    if $list[$i][1] = $list[$i][2] Then
        if $list[$i][1] > 800 Then
            _ImageResize($list[$i][0], $temp_resize, 800, 800)
        Else
            FileCopy($list[$i][0], $temp)
        endif
    Else
        ;Get Min
        if $list[$i][1] < $list[$i][2] Then
            if  $list[$i][1] > 800 then
                _ImageResize($list[$i][0], $temp_resize, 800, 800)
            Else
                _ImageResize($list[$i][0], $temp_resize, $list[$i][1], $list[$i][1])
            endif
        Else
            if  $list[$i][2] > 800 then
                _ImageResize($list[$i][0], $temp_resize, 800, 800)
            Else
                _ImageResize($list[$i][0], $temp_resize, $list[$i][2], $list[$i][2])
            endif
        Endif
    endif

next

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

    $sOutImage = $sOP & $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

Exit




;135

 

This is why I love this forum!
Thanks a lot, I can adapt to this script I am sure.
will work on it first thing in the morning.

Link to comment
Share on other sites

#### I Hit a bump on the road ###

I successfully manage to do what I want to do and I successfully manage to rewrite names but 3 of the image names won't rename?

So lets say I have 10 images that all get resized, then 7 of them gets renamed but not the last three, confuses me quite a bit?
Thanks in advance!

Rename Script:
 

Func renameall()
local $path = @ScriptDir & "\" & $nymappe & "\"
local $ret
Local $hSearch = FileFindFirstFile($path & "*.jpg")
$i = 1
While 1
    $sFileName = FileFindNextFile($hSearch)
    If @error Then ExitLoop
    if FileMove($path & $sFileName, $path & String("NewName" & +$i) & ".jpg", 0) = 1 then
        ConsoleWrite($path & $sFileName & ' renamed to ' & $path & String("NewName" & +$i) & ".jpg" & @LF)
    Else
        ConsoleWrite('File rename failed for file = ' & $path & $sFileName & @LF)
    endif
    $i += 1
WEnd
FileClose($hSearch)
EndFunc

 

Full Script:
 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=car.ico
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GDIPlus.au3>
#include <File.au3>
#include <Array.au3>
#include <ColorConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ProgressConstants.au3>

; Declare array
Dim $Images[1]
; Gets all JPG files in the current directory (@ScriptDir).
Local $search = FileFindFirstFile("*.jpg")

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

; 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

; Close the search handle
FileClose($search)

; Create directory "resized" if not there yet
$nymappe = InputBox("Mappe / Bil Navn", "Mappe / Bil Navn")
If NOT FileExists(@ScriptDir & "\" & $nymappe & "\") Then
    DirCreate(@ScriptDir & "\" & $nymappe & "\")
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) * 15
        Local $NewImageHeight = ($ImageHeight / 100) * 15

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

; 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()
    Call("renameall")
EndFunc

Func renameall()
local $path = @ScriptDir & "\" & $nymappe & "\"
local $ret
Local $hSearch = FileFindFirstFile($path & "*.jpg")
$i = 1
While 1
    $sFileName = FileFindNextFile($hSearch)
    If @error Then ExitLoop
    if FileMove($path & $sFileName, $path & String("NewName" & +$i) & ".jpg", 0) = 1 then
        ConsoleWrite($path & $sFileName & ' renamed to ' & $path & String("NewName" & +$i) & ".jpg" & @LF)
    Else
        ConsoleWrite('File rename failed for file = ' & $path & $sFileName & @LF)
    endif
    $i += 1
WEnd
FileClose($hSearch)
EndFunc

 

Edited by RyukShini
Link to comment
Share on other sites

  • Moderators

@RyukShini when you reply, there is no need to quote every single post above yours. Most of us can remember what we just said, and it pads the thread unnecessarily.

 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

You create an array with all JPG files - FINE
You loop through this array and call function _ImageResize for each entry - FINE
At the end of function _ImageResize  you call function _RenameAll - ??

This means you rename ALL JPG files after the first JPG has been processed.
I think calling _RenameAll should be called at the end of the script or you should rename a single JPG after it has been resized.

BZW: You are calling _GDIPlus_Startup and _GDIPlus_Shutdown multiple times in your script and even in a loop. This sloooooowws down your script.
Call _GDIPlus_Startup  at the top of your script and end GDI+ before you exit.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

19 hours ago, water said:

You create an array with all JPG files - FINE
You loop through this array and call function _ImageResize for each entry - FINE
At the end of function _ImageResize  you call function _RenameAll - ??

This means you rename ALL JPG files after the first JPG has been processed.
I think calling _RenameAll should be called at the end of the script or you should rename a single JPG after it has been resized.

BZW: You are calling _GDIPlus_Startup and _GDIPlus_Shutdown multiple times in your script and even in a loop. This sloooooowws down your script.
Call _GDIPlus_Startup  at the top of your script and end GDI+ before you exit.

Thanks a lot savior!
I am doing this now.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...