简体   繁体   中英

Perl - Using Image::Magick

I am trying to use ImageMagick to manipulate .jpg files. I believe I have successfully installed it because I can convert a .jpg image to a .png image on the command prompt by the following command:

convert   image.jpg    image.png

However, when I attempt to load the module in a script and execute the script I get a “Can't locate imagemagick.pm in @INC (you may need to install the imagemagick module)” error.

How do I load the Image::Magick module into the script?

#!/usr/bin/perl

use Image::Magick;   #load the ImageMagick module
use strict;
use warnings;

my $directory = 'path-to-images';

opendir my $dh2, $directory or die "Could not open file";

while (my $image = readdir($dh2)){
        my $convertedFileName = $image;  #assign current image to a variable
        $convertedFileName =~ s/.jpg/-convert.jpg/; #change name of file
        print "$convertedFileName\n";
        $convertedFileName = Image::Magick->new; #make ImageMagick object


           $convertedFileName->Quantize(colorspace=>'gray'); #alter the image
            }

    closedir $dh2;

The most likely explanation is that the error message is telling you that the Image::Magick module just isn't installed on your system. What makes you think that it is installed? Have you used it in other programs? Do other people who use the system make use of it?

The Image::Magick module isn't part of the standard Perl installation. It needs to be installed separately. It looks like you're using Linux and your shebang line points to what looks like the system Perl, so the quickest approach will be to install the version of Image::Magick that has been pre-packaged for your distribution and which will be available from your standard package repositories.

On a RedHat-like system (RHEL, Centos, Fedora, etc) type:

sudo yum install ImageMagick-perl

(You might need dnf instead of yum on newer versions of Fedora.)

On a Debian-like system (Debian, Ubuntu, etc) type:

sudo apt-get install libimage-magick-perl

There's also at least one problem with your code. After creating the Image::Magick object ( Image::Magick->new() ) and before doing any work on it ( $convertedFileName->Quantize(colorspace=>'gray') ) you need to read the existing file into the object. Your code should probably look something like this:

while (my $image = readdir($dh2)){
    my $convertedFileName = $image;  #assign current image to a variable
    $convertedFileName =~ s/.jpg/-convert.jpg/; #change name of file
    print "$convertedFileName\n";

    # Note: I'm using a new variable here
    $image = Image::Magick->new; #make ImageMagick object

    $image->Read("$directory/$convertedFileName");
    $image->Quantize(colorspace=>'gray'); #alter the image
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM