Wednesday, January 15, 2014

imagemagic notes

A while back I wrote about ImageMagick and editor macros. For a personal project, a comic book, I'm back to using it for some bulk image processing. And speaking of processing, this time I wrote a Processing program to let me select regions from a 1000x1000 image and then figure out the corresponding ImageMagick command to clip out that area from the 7008x7008 original, resizing the clipping to 1000 pixels wide by whatever the correct height was.

I didn't spend much time making the program "good" from a UI sense, in fact I had to edit one line it and rerun for each image- but Processing is so quick that wasn't much of a burden. I would select each area in turn and tap the space bar when I was happy with the clipping region I had drawn.

The Processing code was:
PImage img;
String file = "45"; //change this for each image
void setup(){
   img = loadImage("orig/"+file+"_1000x1000.png");

float startX, startY;
void draw(){

void mousePressed(){
   startX = mouseX;
   startY = mouseY; 

void keyPressed(){
   float w = mouseX - startX;
  if(w < 0) {
     startX = mouseX;
     w = abs(w);
  float h = mouseY - startY;
  if(h < 0) {
     startY = mouseY;
     h = abs(h);

  int realX = round(startX * 7.008);
  int realY = round(startY * 7.008);
  int realW = round(w * 7.008);
  int realH = round(h * 7.008);
  int finalW = 1000;
  if(realW == 0) realW = 1; //catch case if area not really drawn
  float ratio = (float)finalW /(float) realW;
  int finalH = round(ratio * realH);

   println("convert orig/"+file+".png -crop "+realW+"x"+realH+"+"+realX+"+"+realY+
           " -resize "+finalW+"x"+finalH+" out/"+file+"-"+".png");

The flush(); at the end didn't do what I wanted, which was to ensure Processing's output buffer showed the line immediately, but it worked well enough and wasn't worth fixing.

The ImageMagick command it output was:
convert origfile.png -crop WIDTHxHEIGHTxTOPxLEFT -resize WIDTHxHEIGHT outputfile.png

Another ImageMagick I had to lookup today is

convert file1.png file2.png file3.png +append outputfile.png
"+append" is pretty handy for making an "image sequence". I haven't experimented with what happens if the images are different heights, but it met my goal of making a simple "filmstrip" from the pages. (I'm going to use that as a simple thumbnail view.)

No comments:

Post a Comment