Convert images which have the same names but different extensions

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
john1120
Posts: 1
Joined: 2017-05-31T04:29:13-07:00
Authentication code: 1151

Convert images which have the same names but different extensions

Post by john1120 »

For example, I have two files:

Code: Select all

aaa.jpg        (with cat)
aaa.png        (with dog)
As you can see, images are different, despite of their names, which are the same.

I want to convert both these images to one single format.

The basic attempt for this task is

Code: Select all

mogrify -format jpg *.png
But it doesn't work, for obvious reasons (one folder cannot contain multiple files with the same name and extension).

Also, some notes from myself.

a) Please note, this is just example. In real life, it would be about 100-200 images.

b) From my point of view, there seems to exist at least 2 ways to fix this issue: add a timestamp to filename **OR** add random text. I think random text will be better because timestamps sometimes could be equal (if processing is very fast).

So, what do you suggest for this task, and how it could be done? Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert images which have the same names but different extensions

Post by fmw42 »

One folder can hold both aaa.jpg and aaa.png, since the suffixes are the same. At least you can do this in Unix. The problem comes when you try to rename to the same suffix. What you can do is write a loop over each image and copy all pngs to one directory and mogrify all jpegs into pngs into another directory. Then rename as needed and merge.

You could also use your OS and scripting to add the creation date and time (or successive indices) to your file names and then process them.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert images which have the same names but different extensions

Post by fmw42 »

Two good answers (better than mine above) can be found at https://stackoverflow.com/questions/442 ... extensions
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert images which have the same names but different extensions

Post by snibgo »

Please tell us the version of IM you use, on what platform.

Another method is to include the extension of the input file into the name of the output file (the part before the extension), eg:

aaa.jpg => aaa_jpg.jpg
aaa.png => aaa_png.jpg

Mogrify will always use the same name for the output as the input, so you would do the job with convert, in a shell loop.
snibgo's IM pages: im.snibgo.com
Post Reply