Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename a bunch of PNG images with ".jpg" extension to ".png"

So I have a folder with thousands of image files, all of them saved as .jpg.

The problem is that some of those files are actually PNG image files, so they don't open in a lot of programs, unless I manually change their extension to .png. For example, the Ubuntu image viewer throws this error:

"Error interpreting JPEG image file (Not a JPEG file: starts with 0x89 0x50)"

I've already ran a hexdump of some of these files to confirm this error and it checks out.

I'm looking for a simple way to find all the files with the wrong extension among the other files and change their extension. How would I do this with a bash script for example? So far I have no idea. All help apreciated!

like image 747
Frank Avatar asked Jun 06 '15 15:06

Frank


People also ask

Can I just rename a PNG to JPG?

png file, you can just rename image. png to image. jpeg or image. gif , and it automatically gets converted to the other format and works perfectly fine.

How do I change multiple images from PNG to JPG?

Go to File>Automate>Batch. Choose the created set and action, and select the PNG images you want to convert. Photoshop will bulk convert all PNG images to JPEG on your Windows.

How do I convert a bunch of files to PNG?

Open Finder and select the images you want to convert. Open them in Preview and then Select All. Go to "File" and choose "Export Selected Images". Choose the export format as PNG.


1 Answers

for f in *.jpg ; do
  if [[ $(file -b --mime-type "$f") = image/png ]] ; then
    mv "$f" "${f/%.jpg/.png}"
  fi
done

This gets a list of .jpg files, then for each calls the file utility on it to get the mime type. If that's image/png, then it renames the file using a string manipulation substitution.

like image 146
Mat Avatar answered Sep 19 '22 16:09

Mat