Skip to content

Instantly share code, notes, and snippets.

@ddmitov
Created March 20, 2016 08:22
Show Gist options
  • Save ddmitov/8cf37f52da794cade9e4 to your computer and use it in GitHub Desktop.
Save ddmitov/8cf37f52da794cade9e4 to your computer and use it in GitHub Desktop.
Simple resizer for multiple JPG images based on the 'convert' binary from ImageMagick. Full path to a directory containing JPG files is the first and only command line argument needed. Original files are not overwritten! Resized images are saved in a subfolder.
#!/usr/bin/perl -w
use strict;
use warnings;
my $FOLDER_TO_OPEN = $ARGV[0];
opendir (my $directory_handle, $FOLDER_TO_OPEN) or die $!;
my $output_directory_name = "converted-images";
my $output_directory_full_path = $FOLDER_TO_OPEN."/".$output_directory_name;
unless (-e $output_directory_full_path or mkdir $output_directory_full_path) {
die "Unable to create $output_directory_full_path\n";
}
while (my $file = readdir ($directory_handle)) {
# Only files are selected:
next unless (-f "$FOLDER_TO_OPEN/$file");
# Regular expression is used to find files ending in .jpg:
next unless ($file =~ m/\.jpg$/);
my $filepath_to_read = $FOLDER_TO_OPEN."/".$file;
my $filepath_to_write = $output_directory_full_path."/".$file;
print "Resizing $file ...\n";
my $result = `/usr/bin/convert $filepath_to_read -resize 20% $filepath_to_write`;
}
closedir ($directory_handle);
print "Resizing successfully completed!\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment