Search code examples
regexbashimage-processingpathimagemagick-convert

convert all BMP files recursively to JPG handling paths with spaces and getting the file extension right under Linux


I have files with beautiful, glob-friendly pathnames such as:

/New XXXX_Condition-selected FINAL/677193  2018-06-08 Xxxx Event-Exchange_FINAL/Xxxxx Dome Yyyy Side/Xxxx_General016 #07-08.BMP

(the Xxx...Yyyy strings are for privacy reasons). Of course the format is not fixed: the depth of the folder hierarchy can vary, but spaces, letters and symbols such as _, - and # can all appear, either as part of the path or part of the filename, or both.

My goal is to recurse all subfolders, find the .BMP files and convert them to JPG files, without having "double" extensions such as .BMP.JPG: in other words, the above filename must become

/New XXXX_Condition-selected FINAL/677193  2018-06-08 Xxxx Event-Exchange_FINAL/Xxxxx Dome Yyyy Side/Xxxx_General016 #07-08.JPG

I can use either bash shell tools or Python. Can you help me?

PS I have no need for the original files, so they can be overwritten. Of course a solution which doesn't overwrite them is also fine - I'll just follow up with a find . -name "*.BMP" -type f -delete command.


Solution

  • Would you please try:

    find . -type f -iname "*.BMP" -exec mogrify -format JPG '{}' +
    

    The command mogrify is a tool of ImageMagick suite and mogrify -format JPG file.BMP is equivalent to convert file.BMP file.JPG. You can add the same options which are accepted by convert such as -quality. The benefit of mogrify is it can perform the same conversion on multiple files all at once without specifying the output (converted) filenames. If the command issues a warning: mogrify-im6.q16: length and filesize do not match, it means the image size stored in the BMP header discords with the actual size of image data block. If JPG files are properly produced, you may ignore the warnings. Otherwise you will need to repair the BMP files which cause the warnings.

    If the input files and the output files have the same extention (in such a case JPG to JPG conversion with a resize), the original files are overwritten. If they have different extentions like this time, the original BMP files are not removed. You can remove them using the find as well.