Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Pure Perl - Crop and Resize Jpeg Files

by langsor (Novice)
on Mar 15, 2006 at 10:21 UTC ( [id://536800]=perlquestion: print w/replies, xml ) Need Help??

langsor has asked for the wisdom of the Perl Monks concerning the following question:

Hello Worthy Monks,

I've returned - once again seeking your sage advice.

Let me preface this post by stating that I am NOT a deeply sophisticated Perl programmer. I'm a designer who has sunk my teeth into JavaScript, DOM, and Action Script and has been using Perl for several years mostly only to accomplish what I cannot do on the client browser. I know nothing about any other programming or scripting languages, but with detailed help am willing to try.

My situation:

I am searching for a method to mangle (transform) Jpeg photo files. I need to Crop (grab a selection out of the middle), Scale-Resize to a defined height and width, and define compression-optimization. GetHeight, GetWidth would be a nice feature but not really needed. I would really like it if this were done using 'cross-platform, stand-alone, pure-perl' code. This is for a web-app and not to deliver thumbs on the fly.

This seems like a highly useful (in demand) utility for image galleries, online catalogs, etc. but I can't find anything that does the job in a Keep-It-Simple manner. Also, I only need this for Jpeg format (.png would be a nice extra), in an attempt to limit the need for outside C/C++ files.

So far:

I've looked into ImageMagic-PerlMagic, Imager, and GD… They all seem to have a rather formidable installation learning curve (for my simple ways of doing things) and the limitation of requiring C/C++ dependencies that I find beyond my grasp to understand.

My limitations:

I'm developing on a Windows machine (indigoStar.com's IndigoPerl - 5.8) but my target environment is a Unix server that gives me limited access for installing custom software and no super-user privileges. That is one reason I would like a drop-and-go perl script that I can use from my less-than root directory. That, and I have trouble installing some modules on IndigoPerl and changing paths to include Win32 binary or DLL files and anticipate even more trouble doing so on this server.

My questions:

  • Can this seemingly simple thing be done using pure-perl? I know Perl favors text crunching rather than binary crunching.
  • Do I need to hire someone to do this?
  • Would I be better off looking at an approach using PHP, Ruby or Python?
  • Am I missing something obvious?

Any help or advice getting me looking and thinking in the right direction would be deeply appreciated.

Thank you for any help and insights you can provide.

Replies are listed 'Best First'.
Re: Pure Perl - Crop and Resize Jpeg Files
by adrianh (Chancellor) on Mar 15, 2006 at 10:36 UTC
    Can this seemingly simple thing be done using pure-perl? I know Perl favors text crunching rather than binary crunching.

    Well there is nothing technically preventing somebody writing a pure perl program to do this. However AFAIK nobody has done. So - effectively your answer here is "no".

    Do I need to hire someone to do this?

    Possibly. Depending how much time you want to invest/waste you might want to poke around doing the installation yourself ;-)

    Would I be better off looking at an approach using PHP, Ruby or Python?

    I don't know enough about the built in functionality of PHP to comment. Both Python and Ruby give you the same sort of problems Perl would.

    Am I missing something obvious?

    I don't think so.

Re: Pure Perl - Crop and Resize Jpeg Files
by Cody Pendant (Prior) on Mar 15, 2006 at 10:52 UTC
    There are good reasons why this kind of thing is done with ImageMagick etc. It would probably be very slow even if you could write it in pure perl.

    The best thing for you is to bit the bullet and work on that installation thing. You'll learn a lot by doing it. But then on the other hand:

    I'm developing on a Windows machine (indigoStar.com's IndigoPerl - 5.8) but my target environment is a Unix server that gives me limited access for installing custom software and no super-user privileges.

    A specific Unix server? Why not develop on that? And it's either got ImageMagick or GD or whatever, or it hasn't. So, do you know?



    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
Re: Pure Perl - Crop and Resize Jpeg Files
by shotgunefx (Parson) on Mar 15, 2006 at 11:03 UTC
    Could it be done? Yes. But I don't think you understand what you're asking. Making a pure perl (or any other language for that matter) imaging library is a large undertaking.

    Even resizing images is a complex subject and there are numerous ways of doing it, some better suited for the task at hand than others. Plus perl would be slow (maybe PDL could help here)

    I'd be suprised if PHP,Ruby or Python weren't using GD or Image::Magick as well underneath the hood.

    It would probably take you ten times as long to create such a thing, than learn the ins and outs of installing and interfacing third party software. Plus once you've overcome that problem, you'll be able to utilize dozens if not hundreds of third party libraries for any number of tasks.

    -Lee
    "To be civilized is to deny one's nature."
      I'd be suprised if PHP,Ruby or Python weren't using GD or Image::Magick as well underneath the hood.

      Actually they are, but maybe the modules are already available on the target server for one of these (well probably for PHP indeed), that would ease work quite a bit for the OP.

        That's a good point. Though I would think most hosts would have magick or GD on it.

        As anecdote, don't know wether it's still the case, but Yahoo! Store used GD from within the LISP software.

        -Lee
        "To be civilized is to deny one's nature."
Re: Pure Perl - Crop and Resize Jpeg Files
by zentara (Archbishop) on Mar 15, 2006 at 12:32 UTC
    This seems like a highly useful (in demand) utility for image galleries, online catalogs, etc.

    I was watching a show last night on Edison and his inventions. One of the early lessons he learned, was never build anything that won't sell. Your pure Perl version of this cropping app, will be so slow, that nobody will use it, since most people can install and use ImageMagick, GD, or Imager ( or it is preinstalled for them). If this was used in online web apps, the first thing that would happen, is you would get alot of cgi carshes due to timeouts( as your Perl script uses 12 seconds to do what the c-based module does in .3 seconds).

    It will take alot more effort to make your app(bug free), than it would to learn how to install and use the existing modules. It reminds me of myself, when I was a purely functional programmer, and would spend hours trying to avoid using objects, because I didn't want to learn how they worked.


    I'm not really a human, but I play one on earth. flash japh
Re: Pure Perl - Crop and Resize Jpeg Files
by salva (Canon) on Mar 15, 2006 at 16:23 UTC
    instead of using Image::Magick you can just install ImagiMagick for windows and use the binary utilities provided on that package to manipulate the images calling them from your Perl programs with system().
Re: Pure Perl - Crop and Resize Jpeg Files
by langsor (Novice) on Mar 15, 2006 at 17:11 UTC

    Thank you all for your replies!

    I understand now that what I was missing is how slow it would be to execute such an operation using pure perl. I did understand that it would be a very large undertaking to build something like that, but not why it had not been done.

    I have spent some time already on trying to install and configure the modules I mentioned. I guess I'll go back to that and make one of them work.

    A specific Unix server? Why not develop on that? And it's either got ImageMagick or GD or whatever, or it hasn't. So, do you know? - Cody Pendant

    Cody, I don't have access to it yet, it's for a client site so I don't have all the details yet, but that is a good point. I'll find out and figure out my resources then work on the rest of it until I have access to develop that one chunk on the Unix Server.

    Thanks again, your feedback put me back on track ( of 'back to the drawing board' with what I've got ) instead of trying to find the pie in the sky.

    Peace

      I've not been succesful in installing Perlmagick myself, last time I tried. So, skip that. Just install ImageMagick, which is a binary install for Windows and quite easy to install on Linux (it may already be on the system, if it has PHP as well), and just use the command line tools. In my experience, that works quite well.
Re: Pure Perl - Crop and Resize Jpeg Files
by cormanaz (Deacon) on Mar 16, 2006 at 01:09 UTC
    You should use GD for this. What you're talking about is simple using that library:
    use GD; $image = GD::Image->new("myimage.jpg"); ($width,$height) = $image->getBounds() $newimage = new GD::Image(100,100); $newimage = copyResized($image,0,0, 0,0,100,100,$width,$height); open(IMG, "new.jpg") or die "Can't open output: $!"; binmode IMG; print IMG $newimage->jpeg; close IMG;
    Good luck...

    Steve

      What you're talking about is simple using that library:

      With a bit of fiddling to correct the syntax errors, this code produces a distorted image - not a cropped image.

        This version should do better (update: made more idiomatic/terse, it should really do aspect preservation to make a better image instead of translating an unknown aspect to a square)–

        use strictures; use GD; use Path::Tiny; GD::Image->trueColor(1); my $original_image = GD::Image->new( shift || die "Give an image!\n" ) +; my $new_image = GD::Image->new(100, 100); $new_image->copyResampled( $original_image, 0, 0, 0, 0, $new_image->getBounds, $original_image->getBounds ); path("new.jpg")->spew_raw($new_image->jpeg);

        The changes that make it “right” are GD::Image->trueColor(1) and $newimage->copyResampled instead of copyResized.

Re: Pure Perl - Crop and Resize Jpeg Files
by leocharre (Priest) on Mar 15, 2006 at 19:30 UTC

    Alright.
    I've done this a million times. I've coded thumbnailers and auto image thingie doers a bunch. It can be a pain.

    Using imagemagick is not so hard.

    image magick and the gd library are probably already in that box you are developing for/in/to. Do you have a shell account ? try this sh1t3:

    convert -blur 8 /this/is/my/image.jpg /this/is/my/ouput_image2.jpg

    Did it work? Muhahhahahhahah! H311 Y3ah! Ah.. open source.. waddyagonna do... let it give you a lap dance for free- she's sooo good...

    So, anyway.. convert is actually imagemagick. Imagemagick has all these diff interfaces to it, perlmagick is one of them, there's stuff for C, for python, etc. Interfaces. What we used up there was a command line interface, convert is mad good, you have got to look at what it can do.. it's INSANE.. if you are doing anything with images on a linux box, don't be shooting yourself in the foot and think that this stuff is *too complex* and yadda yadda.. It's not complex. It has *options*! To let you do what you want it to do! The options make sense. (Think about it, you are letting code mess with an image, this is not your regular search and replace regex). Later on when you use something lighter you'll be wishing the options were there.

    Don't be lazy, here- have some coffee- on me. Drink up. If my stupid 455 could, you can do it too!

    I'm going to put a 800 line little tiny app I wrote called indexgallery, it lets you maintain an image gallery- this is not release worthy, this is just a script!, but it works, and it's cool.- but before I do let me focus on what may most interes you, the sub that makes a thumbnail with image magick:

    Edit: g0n - readmore tags

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://536800]
Approved by hawtin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-19 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found