double extensions in output filenames

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
rossdv8
Posts: 47
Joined: 2014-03-12T21:54:20-07:00
Authentication code: 6789

double extensions in output filenames

Post by rossdv8 » 2014-08-06T20:14:27-07:00

Ok, I searched this and can't find it, so I'm posting it as a new topic.
This is for beginners who are messing with scripts, and I was discussin it with Fred in another topic.
It worked for me in Linux. I don't know if it works in Mac or Windows.

If you have cobbled together a batch processing script that involves not only converting multiple images from say input.jpeg to output.png, but in renaming the files, you might get a heap of files named like this:
output1.jpg.png
output2.jpg.png
output3.jpg.png

It is caused my my lousy scripting and one day I will learn not to let it happen.
But today is not the day. In fact in a month I have got the script working perfectly but I cannot stop this stupid extension thing.

So a quick way to get rid of the '.jpg' out of the name is:

Now I have managed to remove the .jpg.png double extension and only have the output file as .png using this:

for i in *.png
do
mv "$i" "`echo $i | sed 's/.jpg//'`"
done

It may be clumsy, and it might not be correct, but it worked for me to solve this really annoying problem caused my my inexperience with BASH.
Again, it works in Linux. Not sure about Win and Mac, but it should be similar. I hope it helps someone else stuck in the loop.

Cheers,

RossD.

User avatar
fmw42
Posts: 25584
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: double extensions in output filenames

Post by fmw42 » 2014-08-06T20:48:06-07:00

If you are processing a folder of images to get one output for each input, then mogrify will do all you want. You tell it what file extension you want for the output images ( -format png ) and what file extension you are processing ( *.jpg ) or for all files ( just * ).

see
http://www.imagemagick.org/Usage/basics/#mogrify

Otherwise, the code example I gave you for processing each step to remove the suffix to get the basename should do just what you want without having to mv files afterwards. You just need to loop over each file you want to process.

Code: Select all

infile="somefilename.jpg"
basename=`convert "$infile" -format "%t" info:`
outfile="${basename}.png"
convert "$infile" <some processing> "$outfile"
If you need to add more to the basename for each step, then you have to create a variable for each step.

Code: Select all

infile="somefilename.jpg"
step="_resize"
basename=`convert "$infile" -format "%t" info:`
outfile="${basename}_${step}.png"
convert "$infile" <some processing> "$outfile"

In many of my test scripts, I create the output file with each argument appended, so when I create my examples file, I can see what the arguments were.

outfile="${basename}_a${a}_b${b}...etc.png"

You can do something similar if you know all the steps you want or iterate the process in the above code for each step.

rossdv8
Posts: 47
Joined: 2014-03-12T21:54:20-07:00
Authentication code: 6789

Re: double extensions in output filenames

Post by rossdv8 » 2014-08-07T16:30:35-07:00

Thanks Fred,

1 - You have given me enough of a tutorial to re-learn some stuff.
2 - There is enough information here to help me and any OPD write a much cleaner script to prevent the problem happening inthe first place.

I'm going off to have a play with what you are teaching.

Thanks again :-)

rossdv8
Posts: 47
Joined: 2014-03-12T21:54:20-07:00
Authentication code: 6789

Re: double extensions in output filenames

Post by rossdv8 » 2014-08-19T18:31:40-07:00

UPDATE:
For any other relative newcomers that end up with the double file extension problem.
These are quick, simple fixes (for LINUX) that don;t seem to appear in google searches etc. so have been adapted from other stuff.

By far the easiest is:
rename 's/\.jpg.png$/\.png/' *.jpg.png

This gets rid of the .jpg in a file like myimage.jpg.png and leaves simply myimage.png.
I just add it as the last line in a script, so I don't have to remember to manually rename every file I have processed.
Obviously a little substitution will fix other double extensions.

The other is the one I mentioned earlier:
for i in *.png
do
mv "$i" "`echo $i | sed 's/.jpg//'`"
done

Does pretty much the same thing. Both seem to work. (in LINUX)

It is not an ImageMagick problem, but if you are a beginner at shell scripting and use IM long enough you are bound to come across it if your programming skills are not sharp.
This is a simple way to fix sloppy programming until you learn how not to let the double extensions happen.

In my case, no amount of reading has solved the problem of creating those extensions in the first place - and no amount of googling or asking here or on BASH forums has led to a solution.

User avatar
fmw42
Posts: 25584
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: double extensions in output filenames

Post by fmw42 » 2014-08-19T18:49:49-07:00

My code above shows you how to avoid it, but getting the filename without the suffix and using that with any suffix you want later in your script for the output name.

rossdv8
Posts: 47
Joined: 2014-03-12T21:54:20-07:00
Authentication code: 6789

Re: double extensions in output filenames

Post by rossdv8 » 2014-09-01T21:55:59-07:00

fmw42 wrote:My code above shows you how to avoid it, but getting the filename without the suffix and using that with any suffix you want later in your script for the output name.
Thanks Fred,

When I went to 'have a play' with your code, I was still getting just a heap of error messages telling me there were things wrong.
I tried using the code exactly as you wrote it, then when that didn't work, I tried to guess it that code was just a guide to point me in a direction, and tried variations on it.

So eventually, for simplicity, I fell back to this rename line at the end.

User avatar
fmw42
Posts: 25584
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: double extensions in output filenames

Post by fmw42 » 2014-09-01T22:30:03-07:00

What error messages do you get from running this code: (just copy and paste into a bash terminal window)

Code: Select all

infile="somefilename.jpg"
basename=`convert "$infile" -format "%t" info:`
outfile="${basename}.png"
convert "$infile"  "$outfile"
where somefilename.jpg is to be replace with a real file in your working directory or with some path to that file.

What version of IM and what platform are you on? If Windows, then my code won't work, because it is unix OS syntax and not Windows OS syntax.

rossdv8
Posts: 47
Joined: 2014-03-12T21:54:20-07:00
Authentication code: 6789

Re: double extensions in output filenames

Post by rossdv8 » 2014-09-02T00:55:27-07:00

Hello Fred,
I'm using ImageMagick 6.8.9-3 on Linux.
That particular script is no problem. It was just telling me strange things when I tried to use it in various ways to stop my script giving an output filename of something like processed.jpg.png

I suppose the problem was that I could find no way of actually using that script to stop the IM script creating a file with a .jpg.png extension. It just threw up annoying messages. So I went pack to simply stripping the .jpg out of the double extension. Now I have an even simpler way to strip it using rename. So I just ran one line at the end of the process.

Right now I can't even find the script lines that were creating the double extensions because I've rewritten the DiaGrid diagonal collage script so many times in the last few months. I spent a whole week getting it to work better and now I think I've accidentally lost the double extensions anyway by changing the IM function I was using.

Of course, the crazy thing was that you wrote a similar script for someone to do a 3 image collage in about 5 minutes. Mine took months to write and has 4 images. Yours takes about 10 seconds to make the collage, mine takes at least 3 minutes to make a high resolution png.

rich2005
Posts: 32
Joined: 2012-04-09T11:07:36-07:00
Authentication code: 8675308

Re: double extensions in output filenames

Post by rich2005 » 2014-09-02T02:46:13-07:00

PClinuxOS KDE 32bit & ImageMagick 6.8.0-2 2013-11-07 Q16
'm using ImageMagick 6.8.9-3 on Linux.
That particular script is no problem. It was just telling me strange things when I tried to use it in various ways to stop my script giving an output filename of something like processed.jpg.png..
yet another way that does not involve mv

Code: Select all

#!/bin/bash
  find . -name "*.jpg" | while read fname ; do
     echo "Doing:  $fname"
     convert "$fname" "${fname%%jpg}png"
  done

Post Reply