0
$\begingroup$

i want to import an image, modify it and then export the modified image.

Something like this:

img = Import["C:\\Images\\e4271-5.jpg"]

Some modifications happens.

StringTake[img,{21,27}]
cond=StringTake[img,7]
If[cond=="5",Export["C:\\Images\\e4271-55.jpg"],Export["C:\\Images\\e4271-11.jpg"]]

So i'll encounter two types of files, files with file names "name-1" and files with file names "name-5". I want mathematica to import the files, modify them and save them in the same folder with nearly the same name, only difference to the filename is that for the "name-1"-ones there is an extra "1" added to the file name. Resulting in "name-1" -> "name-11" and analoge "name-5" -> "name-55".

How do i do that? StringTake wont work because of the quotation marks with my string of interest.

$\endgroup$
1
$\begingroup$

If you are looking for a function which takes in a filepath and generates a filepath to a file with nearly the same name, but with a duplication of the final digit of the base name, then you can use String pattern recognition like this:

formatString = First@StringCases[#, x__ ~~ y_ ~~ ".jpg" :> x <> y <> y <> ".jpg"] &;
formatString@"C:\\Images\\e4271-5.jpg"

resulting in

"C:\Images\e4271-55.jpg"

Edit

I noticed that in version 10, the x_ and y_ show up in orange, which they didn't used to do. Normally this indicates an error, but the code executes fine. I swear there was a previous question which asked this, but I can't remember where it was. Why is this occurring?

$\endgroup$
0
$\begingroup$

You can use the various FileName functions to get the result you want. The following function will import all the files in the folder ../images, modifies them with the function DoThings and exports them with the original file name extended by the last character of the file name.

SetDirectory[".../images"];
Export[FileBaseName[#] <> StringTake[FileBaseName[#], -1] <> "." <> 
    FileExtension[#], DoThings[Import[#]]] & /@ FileNames[]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.