Convert images which have the same names but different extensions

一笑奈何 提交于 2020-01-04 06:56:09

问题


For example, I have two files:

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

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.

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

  2. 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.


回答1:


Instead of mogrify you can use convert modifying the original file name with -set filename:f, for example adding a suffix containing a counter:

convert *.png -set filename:f "%t_%p" "%[filename:f].jpg"

Where the meaning of %p and %t attributes are:

  • %p index of image in current image list
  • %t filename without directory or extension (suffix)

More details here.

If this is your starting scenario:

aaa.jpg        
aaa.png        
bbb.jpg        
bbb.png  

the final situation would be:

aaa.jpg        
aaa.png        
bbb.jpg        
bbb.png   
aaa_0.jpg
bbb_1.jpg



回答2:


In similar situations I add text (not random) so the files would be aaa_png.png and aaa_jpg.jpg, and therefore after the conversion you'd have aaa_png.jpg and aaa_jpg.jpg which should help you keep track of things and be immune to the problem you had.

As @jsv pointed out, you can do this in one step:

convert *.png -set filename:f "%t_%e" "%[filename:f].jpg

If you do it this way, the original "aaa.png" and "aaa.jpg" will still exist, along with the new "aaa_png.jpg" file.



来源:https://stackoverflow.com/questions/44284542/convert-images-which-have-the-same-names-but-different-extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!