Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 156 lines (123 sloc) 4.239 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#!/usr/bin/env perl

use DateTime;
use File::Basename;
use Image::Magick;
use Log::Message::Simple;
use strict;

# debug flag
my $debug = 1;

my $in_name = shift;
debug("Input name: $in_name", $debug);

my $out_dir = shift // '.';
$out_dir =~ s|/$||;
debug("Output directory: $out_dir", $debug);

my $bg_filename = 'background.png';

my ($crop_w, $crop_h) = (386, 340);
my ($translate_x, $translate_y) = (62, 62);
my $crop_geometry = geometry($crop_w, $crop_h, $translate_x, $translate_y);

my ($output_w, $output_h) = (671, 489);
my $output_geometry = geometry($output_w, $output_h);

my $overlay_width = 512;
my $ratio = 438/495; # found by stretching it over google maps in image editor
my $overlay_geometry = geometry($overlay_width, $ratio * $overlay_width);

my $overlay_trans_x = 255;
my $overlay_trans_y = 315;
my $overlay_trans_geometry = geometry(undef, undef, $overlay_trans_x, $overlay_trans_y);

my ($out_w, $out_h) = (800, 600);
my ($out_off_x, $out_off_y) = (173 ,235);
my $out_crop_geometry = geometry($out_w, $out_h, $out_off_x, $out_off_y);

my ($label_x, $label_y) = (445, 550);
my $label_geometry = geometry(undef, undef, $label_x, $label_y);
my $label_size = 18;

my $background;
$background = Image::Magick->new;
$background->Read($bg_filename);

if($in_name =~ m/-/) {
  msg("Processing file names listed on stdin", $debug);
  while(<STDIN>) {
    chomp;
    process_image($_);
  }
} elsif (-f $in_name) {
  process_image($in_name);
} elsif (-d $in_name) {
  $in_name =~ s|/$||;
  msg("Processing all .png files in $in_name", $debug);
  my $dir;
  opendir($dir, $in_name) or die "can't opendir $in_name: $!";
  while (defined(my $file = readdir($dir))) {
    my $file_name = "$in_name/$file";
    if($file_name =~ m/.png$/) {
      process_image($file_name);
    }
  }
  closedir($dir);
}
 

sub process_image {
  my $in_path = shift;
  msg("Processing file $in_path", $debug);
  my $bixi_data = Image::Magick->new;
  my $filename = basename($in_path);
  my $out_path = "$out_dir/$filename";

  my $time_stamp = basename($in_path, '.png');

  my $dt_label = Image::Magick->new();
  $dt_label->SetAttribute(fill => 'red', background => '#0000000000000000'
    , font => '/Library/Fonts/Andale Mono.ttf', pointsize => $label_size);
  my $label = 'label:' . dt_str($time_stamp);
  $dt_label->Read($label);

  my $err;

  # read in the bixi bike data
  $err = $bixi_data->Read($in_path);
  die $err if $err;

  #crop out axes
  $err = $bixi_data->Crop(geometry => $crop_geometry);
  die $err if $err;

  # adjust color and opacity
  my ($w, $h) = $bixi_data->Get('columns', 'rows');
  my $overlay = Image::Magick->new(size => geometry($w, $h));
  $err = $overlay->Read('canvas:red');
  die $err if $err;

  $err = $bixi_data->Negate();
  die $err if $err;
  $err = $overlay->Composite(image => $bixi_data, compose => 'CopyOpacity');
  die $err if $err;

  # create background map image with correct dimensions to overlay onto
  my $bg = Image::Magick->new;
  $err = $bg->Read($bg_filename);
  die $err if $err;

  # resize and overlay data onto map image
  $err = $overlay->Resize(geometry => $overlay_geometry);
  die $err if $err;
  $err = $bg->Composite(image => $overlay, geometry => $overlay_trans_geometry);
  die $err if $err;

  # crop to output dimensions
  $bg->Crop(geometry => $out_crop_geometry);

  # add label
  $bg->Composite(image => $dt_label, geometry => $label_geometry);

  $err = $bg->Write($out_path);
  die $err if $err;

  # clear the image objects out; unsure if this is necessary
  @$bg = ();
  @$overlay = ();
  @$bixi_data = ();
}

sub geometry {
  my $x = shift;
  my $y = shift;
  my $x_off = shift;
  my $y_off = shift;

  my $dims = $x . 'x' . $y if defined $x && defined $y;
  my $offsets = sprintf "%+d%+d", $x_off, $y_off if defined $x_off && defined $y_off;

  return $dims . $offsets;
}

sub dt_from_unix {
  my $unix_time = shift;
  $unix_time /= 1000 if $unix_time > 2_000_000_000; # dates are circa 2012 in s or ms from epoch
  my $dt = DateTime->from_epoch( epoch => $unix_time );
  $dt->set_time_zone( 'America/Montreal' );
}

sub dt_str {
  my $unix_time = shift;
  my $dt = dt_from_unix($unix_time);
  return sprintf "%s\n%02d:%02d", $dt->ymd(), $dt->hour, $dt->minute;
}
Something went wrong with that request. Please try again.