How To Repair Images with ImageMagick

I have been doing a lot of importing of photos on my photo site, and have noticed some images do not display properly. SinceI love ImageMagick so much,  I set up  my gallery to use it. However, there appears to be a check in the Coppermine software that aborts reading the file upon errors in the image. So I typed the command “display imagename.jpg” for some of the files that did not import properly, and everything appeared to be fine. But I did notice something peculiar about the images: they were taken with my phone’s camera and all had errors printed out when I tried to open them, but seem to display correctly.

Since these files could be opened by ImageMagick, I figured if I saved them again, I would be able to fix the errors. I ran “convert image1.jpg image1.jpg”, got the familiar errors printed and opened the file again. This time, no errors! But since I have a  lot of files I want to import, I needed a way to convert a bunch at once.

I created a neat little Perl script to do just that. All you need is ImageMagick and perl installed. This script does not rely on PerlMagick, as it runs the convert command. So here it is:

  1. #!/usr/bin/perl
  2.  
  3. processFile("$_") foreach (@ARGV);
  4.  
  5. #recursively fix images
  6. sub processFile {
  7.    my $file=shift;
  8.    return (-d "$file")?( do{ processFile("$_") foreach (<"$file"/*>) }) : `convert "$file" "$file"`;
  9. }

I saved this file as $HOME/bin/fiximage. All you need to do to run it is type “fiximage file1 file2 directory1 directory2 …”. One other thing to note is that this script has been purposefully shortened. I like to use the ternary operator whenever possible in my scripts for just this reason. As such, it does not print anything to the command line itself, but if the convert command encounters an error, that will be printed.

I hope you have found this article useful!

5 Responses to “How To Repair Images with ImageMagick”

  1. Derekp says:

    I think i’ve seen this somewhere before…but it’s not bad at all

  2. mogrifier says:

    It’s nice to have fun with perl (until it gets out of hand :) ) but if you’re on linux or unix it’s easier to use find, go into the directory tree that needs images repaired

    find -iname ‘*.jpg’ -exec nice mogrify {} \;

    (i used mogrify which converts images in-place and nice because it’s a batch job that doesn’t need immediate attention if you’re on a server)

    works for me, at least.

  3. Get that fixed I am sick to death of the mobile site get rid or at least give me the choice to view full or jst make an app

  4. rbe enterprises of ny says:

    I think this is among the most vital information for me. And i am glad reading your article. But should remark on some general things, The web site style is great, the articles is really nice : D. Good job, cheers

  5. sam says:

    All mobile pages have links for the full site at the bottom of the page. Please look or you risk looking like an idiot!

Leave a Reply