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

I use GD and its submodules for creating images with perl. Mostly it is scientific data analysis which requires lot of comparisons. The images are created with bitdepth 8 which I think is the reason for low quality(I am not sure though). Merely resizing the image from 500x500 to say 2000x2000 pixels does help somewhat, but I m not pleased with the way since it still shows bit depth as 8 in properties. Is there a way to tell GD to create better (publication quality, 300 dpi) images? It is a pain to analyse data and prepare figures with GD and then resort to redoing the work for publication. Imager module says it can create better resolution images, but can it be used with GD as the charting modules are basically easier to use in GD and I havent tried Imager. An alternative would be to resort to SVG charting modules but I really dont want to change unless absolutely necessary, because I have already written large pipelines with GD. Wisdom of PerlMonks will be very helpful and I m counting on it.

Replies are listed 'Best First'.
Re: How to create images with 300dpi?
by BrowserUk (Patriarch) on May 26, 2012 at 05:34 UTC
    1. First, you do not create images at any particular number of dots per inch, Dpi is a measure of the output (printer) or display (screen) resolution, on which an image is viewed.

      If you create an image 500x500 and then view it on a 1280x1024 15" screen, then the image will be 5 3/16 inches per side which equates to 96dpi.

      However, if you display that same image on a smartphone with a 800x480 4.3 inch screen, then (assuming that the browser doesn't do any scaling) the image will be 2.1 inches per side; which translates to 237 dpi.

      Same image, different dpi depending upon what it is displayed or printed on.

      In order to view a 500x500 image at 300 dpi, it would have to be displayed or printed on a device capable of 300 pixels per inch -- eg. a printer -- and would show up as 1.67 inches per side.

    2. Second, GD is just as capable of creating high quality images as any other drawing package -- but you do have to use it properly.

      The first problem appears to be that you are creating 8-bit color images instead of 24-bit color images. That's your fault not the modules.

      If you do my $gd = GD::Image->new( 500, 500 ); then you'll get a palettised 8-bit color image.

      If you do my $gd = GD::Image->new( 500, 500, 1 ); then you'll get a 24-bit (true)color image.

      You'll probably need to make some other changes to your current code to achieve best results, but as you haven't posted it, we can't advise you further.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Thanks, but I have tried this already and didn't work. I should have given more info- I am using Venn::Chart for creating Venn diagrams. This module uses GD to draw the charts.

      #!/usr/bin/perl use warnings; use Carp; use strict; use Venn::Chart; (A) # Create the Venn::Chart constructor my $venn_chart = Venn::Chart->new( 500, 500 ) or die("error : $!"); + (B) my $venn_chart = Venn::Chart->new( 500, 500,1 ) or die("error : $! +");

      I have tried both ways- A and B. Could it be that Venn::Chart is using default 8-bit image settings while calling GD? Diving into Chart.pm shows up --

      sub new { my ( $self, $width, $height ) = @_; $self = ref($self) || $self; my $this = {}; bless $this, $self; ----- ----- }

      This tells me that it is taking only width, height as parameters. There are other options also but I am not sure which one to play with as I am not into OOP. Looks like it is time I learn object orientation too.

        The problem here is not with GD, but that Venn::Chart is designed to only produce 8-bit images. It would require careful modification to change that.

        Personally, I would write my own subroutine to generate a Venn Diagram. Drawing 2 or 3 overlapping circles is not difficult.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

      In order to view a 500x500 image at 300 dpi, it would have to be displayed or printed on a device capable of 300 pixels per inch -- eg. a printer -- and would show up as 1.67 inches per side.

      Or you could scale the image on a wrong-DPI device so that it is of the correct physical dimensions when shown. (Depending on the device's capabilities, you may lose detail.) Nevertheless, your underlying point was that DPI on image files or display devices is largely a tacked-on number most programs don't care one bit about.

        Or you could scale the image on a wrong-DPI device so that it is of the correct physical dimensions when shown. (Depending on the device's capabilities, you may lose detail.)

        Yes. By embedding the dpi at which an image was recorded or constructed within the image file, it allows for the possibility to scale that image up or down to correct the size when displayed on mismatched devices. But, as you say, detaisl can get lost or become blocky as a result.

        The problem is, it encourages the thinking "if I make my images at high dpi, they'll look good whatever device they are shown on", but it doesn't work that way.

        If the image is recorded at 300 dpi, and then displayed on a 96 dpi device, it'll either be 3 times too big, or all the fine detail vanishes. And in the bargin, (ignoring compression), you've transmitted 9 times more data than will be used in the final display, for not just no benefit, but negative benefit.

        Far better to record images at a lower dpi, that more closely matches the likely display media and offer a click to zoom facility that links to a larger image for those that need one.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?