Sign in to follow this  
Followers 0
Quietas

Dir Copy, File Rename Randomly, Photo Resize

3 posts in this topic

Howdy folks, lurked for years off and on as I had need. I'm stumped though how to do this cleanly. Here's the basic gist of what I'm trying to do.

I have a bunch of pictures in a directory, the directory needs to be copied, renamed randomly, and then resized for a digital picture frame. I'd like to have a simple GUI that chooses a directory, copies that directory with -Resized added on to the name. Then do the automatic resize and cropping, based on whether the image is landscape or portrait.

I can call Mogrify (part of ImageMaick static download) from Autoit to do the resize and cropping if Autoit can't.

Currently I have:

$SourceDir = FileSelectFolder('Select Source Folder', '', 2)
$TargetDir = $SourceDir & "-Resized" 
$Search = FileFindFirstFile ($TargetDir & '\*.*')

; Copy directory and rename it
DirCopy($SourceDir, $TargetDir, 9)


; Check if there are any files
If $Search = -1 Then
    MsgBox(0, "Error", "No files to rename")
    Exit
EndIf

; Add random Numbers to end of filename so no dupes
While 1
    $File = FileFindNextFile($Search)
    $RandNum = Random(0,9999999,1)
    If @error Then ExitLoop
    $FileNew = $File
    $FileNew = StringReplace($FileNew,".jpg","-" & $RandNum & ".jpg")
    If $File <> $FileNew then FileMove($TargetDir & "\" & $File, $TargetDir & "\" & $FileNew)
WEnd

; Close the search handle
FileClose($Search)

; Resize *.* in new folder
RunWait(@ComSpec & " /c " & "@echo | mogrify -monitor -scale 1024x768! -monitor -scale 788x768! -monitor -crop 788x591+0+89 -quality 90 " & $TargetDir & "\*.*")

Exit

Currently it works somewhat, but while Windows is copying files, the script advances before anything is copied. Thus it #Search comes back -1 each time. Or at least this is what I assume, because I get the error message each time. How can I get the script to pause until the files are finished copying, whether it is 1 file ot 1000?

Also mogrify.exe can't tell if the image is portrait or landscape. Is there a way to do this with Autoit?

Thanks in advance folks.

FYI I'm writing this so I can send the picture frame to my Mother and have an easy way for her to resize the photos with minimal knowledge. It's a Mustek PF-A700B from CompUSA, it was on sale last week quite cheap, but the display is annoying.

To get a picture to display full screen, I have to scale the photo to 1024x768, then scale to 788x768 (screws aspect but more to come). After scaling the photo, I crop to 591 *89 off top, 88 off bottom). You end up with a widescreen image, that looks like it is stretched vertically. It is stretched, but when displayed on the picture frame it displays perfectly in full widescreen with no stretch. Weird, but it works. That took forever to figure out correctly.

- Culley Morrow

- - aka quietas

Share this post


Link to post
Share on other sites



$FileNew = $TargetDir & "\" & StringReplace($FileNew,".jpg","-" & $RandNum & ".jpg")
If $File <> $FileNew then FileMove($TargetDir & "\" & $File, $FileNew)
$Timer = TimerInit()
While FileExists($FileNew) = 0
   If TimerDiff($Timer) > 20000 Then ExitLoop
   Sleep(100)
Wend

This will wait for the file to exist before continuing or alternatly to time out and move to the next file.

You can play around with the TimerDiff which I set to time out at 20 seconds. That may not be long enough for large files.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Thanks for the idea. As it turns out I just needed to move the line declaring $Search to a point after DirCopy. I guess it was coming up null before it could copy.

$SourceDir = FileSelectFolder('Select Source Folder', '', 2)
$TargetDir = $SourceDir & "-Resized" 

; Copy directory and rename it
DirCopy($SourceDir, $TargetDir, 9)

; Declare Search after DirCopy, otherwise it errors out before moving
$Search = FileFindFirstFile ($TargetDir & '\*.*')

Any ideas on the photo resizing anyone?

Share this post


Link to post
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
Sign in to follow this  
Followers 0