Showing posts with label image magick. Show all posts
Showing posts with label image magick. Show all posts

Friday, March 11, 2016

DATA SIMPLIFICATION: ImageMagick


Over the next few weeks, I will be writing on topics related to my latest book, Data Simplification: Taming Information With Open Source Tools (release date March 23, 2016). I hope I can convince you that this is a book worth reading.


Blog readers can use the discount code: COMP315 for a 30% discount, at checkout.


In yesterday's blog, I discussed using system calls within your scripts. One of my examples called an ImageMagick. Today, I thought I'd describe ImageMagick, and some of its benefits.

ImageMagick is an open source utility that supports a huge selection of robust and sophisticated image editing methods. Its source code download site is:

http://www.imagemagick.org/download/

Users may find it convenient to download the executable binaries, for their specific operating system, from:

http://www.imagemagick.org/script/binary-releases.php

Hundreds of ImageMagick methods are described in detail, and useful examples are provided, at:

http://www.imagemagick.org/Usage/

There are several things you should know about ImageMagick:

1. Unlike the commercial image processing applications, ImageMagick has no graphic user interface. ImageMagick is intended to serve as a command line utility.

2. ImageMagick is powerful. There are hundreds of available methods for creating and modifying images.

3. ImageMagick can be called from Python, Perl, Ruby, and other scripting languages, via system calls or via language-specific ImageMagick interface modules (i.e., PerlMagick, PythonMagick, or RMagick)

Here are a few examples of ImageMagick command lines that can be launched from the system prompt (which happens to be sitting at the c:\ftp subdirectory on my home computer):

Converts an image in SVG format to JPEG format.
c:\ftp>convert mtor_pathway.svg mtor_pathway.jpg
Creates a thumbnail image from image.jpg and converts it to gif format.
c:\ftp>convert -size 600x800 image.jpg -thumbnail 120x160 image_thumb.gif
Applies contrast twice to an image, and produce a new file for the results.
c:\ftp>convert original.jpg -contrast -contrast result.png
Puts the contents of file words.txt into the comment section of tar1.jpg and keeps the same filename for the output file.
c:\ftp>convert -comment @words.txt tar1.jpg tar1.jpg
Displays a verbose description of an image, including the header contents.
c:\ftp>identify -verbose tar1.jpg
The real power of ImageMagick comes when it is inserted into scripts, allowing the programmer to perform batch operations of image collections.

Here is a Python script, image_resize.py, that creates a resized copy of every jpeg file in the current directory.
#!/usr/local/bin/python
import sys, os, re 
filelist = os.listdir(".")
pattern = re.compile(".jpg$")
for filename in filelist:
  if pattern.search(filename):
    out_filename = pattern.sub('_small.jpg', filename)
    print(out_filename)
    cmdstring = "convert " + filename + " -resize 400x267! " + out_filename
    os.system(cmdstring)  
  else:
    continue
exit
Here is a Perl script, factth.pl, that reduces the size of every image in a subdirectory that exclusively contains image files. In this case, the all-image subdirectory is "c:\ftp\factth".
#!/usr/local/bin/perl
$newdir = "c\:\\ftp\\factth";
opendir (MYDIR, $newdir) || die ("Can't open directory");
chdir ($newdir);
while ($file = readdir (MYDIR))
  {
  next if (-d $file);
  next if ($file eq "." || $file eq "..");
  system("convert $file -resize 30% $file");
  }
closedir (MYFILE);
exit;

- Jules Berman

key words: computer science, data analysis, data repurposing, data simplification, data wrangling, information science, simplifying data, taming data, complexity, system calls, Perl, Python, open source tools, utility, Image Magick, ImageMagick, jules j berman

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.