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.


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.


2 Answers 2


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"] &;

resulting in



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?


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.

Export[FileBaseName[#] <> StringTake[FileBaseName[#], -1] <> "." <> 
    FileExtension[#], DoThings[Import[#]]] & /@ FileNames[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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