A common operation when working with images is to save them in a different format. Not surprisingly, ImageMagick has tools that make this very easy to perform at the shell.
The easiest way to save an image in a different format is to use
$ convert foo.png bar.jpg
This saves an existing
foo.png in PNG format to another file
bar.jpg in the JPEG format.
Note that this command does not work if you want to convert a set of images. To do that, we use the
mogrify command with its
To convert all PNG images in the current directory to JPEG format images keeping the same filename:
$ mogrify -format "jpg" *.png
Mogrify usually overwrites its source files. In this case however, the original PNG files are left untouched since the new files have a different file extension.
Tried with: ImageMagick 6.7.7 and Ubuntu 14.04
I tried to open a JPG image file using Eye of GNOME. It failed with this error:
Could not load image 'foo.jpg'
Error interpreting JPEG image file (Not a JPEG file: starts with 0x89 0x50)
When I opened the same file using another image viewer, it opened without any problem!
Another symptom of this problem is that the thumbnail of the image file is not displayed in Nautilus or in Nemo file explorers. All of these programs use the same backend for image display.
This problem is typically caused when the image is actually a PNG file. Some apps on Android are notorious for saving all image formats as JPG.
As noted by EOG, the file starts with the magic identifier
0x89 0x50, which is for PNG format. You can also check the same by using the
$ file foo.jpg
foo.jpg: PNG image data, 716 x 960, 8-bit/color RGB, non-interlaced
We can see that
file reports this JPG file as containing PNG image data.
Just rename the JPG file with the correct
.png file extension and EOG will open it correctly. You will also see that thumbnails are now rendered correctly.
Admittedly, EOG could have detected the format correctly and shown a more informative error message. I have seen other image viewers do that. This has been filed as a bug here.
Tried with: Eye of GNOME 3.10.2 and Ubuntu 14.04
I had this line of code in a program that uses OpenCV to write an image buffer to a PNG file:
I was using Visual Studio 2013 to compile and run this file. But, on running I got this error:
opencvmoduleshighguisrcloadsave.cpp:276: error: (-2) could not find a writer for the specified extension
I have found that there are two reasons that this can be caused.
The most logical reason is that OpenCV was compiled with the PNG write feature (or any other format) left out. In this case, you may have to get the OpenCV source code and compile it, making sure you choose to enable
WITH_PNG or similar option enabled in CMake.
Surprisingly, I found that this error can also occur when the OpenCV library you are using has PNG compiled in. However, you may be linking a OpenCV release library (or DLL) with a compilation being done in Debug mode (or vice versa). Ensure that you are using Debug version of the library files in Debug mode and Release files in Release mode. I found that this resolved the error when I got it on Windows! 🙂
Tried with: OpenCV 2.4.9, Visual Studio 2013 and Windows 7 x64