Showing posts with label conversion. Show all posts
Showing posts with label conversion. Show all posts

Sunday, December 13, 2015

Greyscale Batch Conversion of Image Files

Using ImageMagick, it's easy to convert all the image files in a directory to black and white. Here are short scripts implemented in Perl, Python, and Ruby that open the current directory, parse through all the filenames in the directory, to create greyscale versions of jpg, gif, bmp, or png images. Each greyscale copy of an image file is provided with a new filename consisting of "_bw" appended to the filename prefix (e.g., dog.png is copied as the greyscale file, dog_bw.png)
#!/usr/bin/perl
opendir(FILES, ".") || die ("Unable to open directory");
@in_files = readdir(FILES);
closedir(FILES);
foreach $filename (@in_files)
   {
   if ($filename =~ /([a-z0-9\_]+)(\.[gifbmpjpgn]{3}) *$/i)
     {
     $out_file = $1 . "_bw" . $2;
     system("convert ${filename} -set colorspace Gray -separate -average ${out_file}");
     }
   }
exit;

#!/usr/local/bin/python
import os, re, string
filelist = os.listdir(".")
for file in filelist:
  if ".jpg" in file or ".bmp" in file or ".gif" in file or ".png" in file: 
    outfile = "bw_" + file 
    command = "convert " + file + " -set colorspace Gray -separate -average " + outfile
    os.system(command)
    #print command
exit

#!/usr/local/bin/ruby
filelist = Dir.glob("*.*")
filelist.each do
 |file|
 if file =~ /([a-z0-9\_]+)(\.[gifbmpjpgn]{3}) *$/i
    out_file = $1 + "_bw" + $2
    system("convert " + file + " -set colorspace Gray -separate -average " + out_file)
  end
end
exit
© 2008 Jules Berman

As with all of my scripts, lists, web sites, and blog entries, the following disclaimer applies. This material is provided by its creator, Jules J. Berman, "as is", without warranty of any kind, expressed or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. in no event shall the author or copyright holder be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the material or the use or other dealings.

Saturday, January 23, 2010

Batch image conversions

I'm interrupting a series of blogs on the topic of complexity to provide a pointer to my recently constructed web page on batch image conversions.

When you write your own image software, you can automate activities that would otherwise require repeated operations, on multiple image files, with off-the-shelf image processing software. For example, you might want to delete, add, or modify annotations for a group of images, or you might want to resize an image collection to conform to specified dimensions. When you have more than a few images, you will not want to repeat the process by hand, for each image. When you have thousands of images, stored in a variety of image formats, it will be impossible to implement global conversions, if you do not know how to batch your operations.

The Batch conversions: Perl, Python, Ruby page provides three equivalent scripts, in Perl, Python and Ruby, that converts a batch of images from color to greyscale. The scripts are preceded by a step-by-step explanation of the code.

- © 2010 Jules Berman

tags: perl programming, python programming, ruby programming, image magick, imagemagick, grayscale, greyscale, big data, metadata, data preparation, data analytics, data repurposing, datamining, data mining, conversion, image format, image processing, perl, python, Ruby Jules J. Berman Ph.D., M.D.
My book, Principles of Big Data: Preparing, Sharing, and Analyzing Complex Information was published in 2013 by Morgan Kaufmann.



I urge you to explore my book. Google books has prepared a generous preview of the book contents.