Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to improve image resolution...Continued

by chakkaln (Sexton)
on Aug 06, 2005 at 05:02 UTC ( [id://481438]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All, I am using Perl GD to generate some images. I am not quite happy with the image resolution and the color quality. Is there as way to improve one of it or both. Apologise for a basic question (new to this field). Can any one give any suggestions please. Thanks in advance Nagesh

Update

Thanks for all the messages and to jhourcle for a lenghty explanation. Sorry for not responding back due to the time difference. The essence of the code is to generate little triangles and rectangles on a given line dynamically based on the input file and hence I am not using any photos or images with gradient (hence I feel jpeg is not an option for me). These triangle are 3 pixel in the base and height. The problem I am having is that the slope of the triangle is not smooth (can this problem be related to my screen resolution? but did not have this problem before). Also the overall image does not appear to be sharp. The code runs into several hundered lines and I will put the relavant code. When I try to use true color, I get a black background. When I remove the truecolor parameter, I get a image with a white back ground (how is this influencing the image background). If there is a way to post the image, please let me know and I will do it as I am not sure if that the best quality one can get. Also note that using gif, png did not change the final image resolution. my GD package does not support tif. I feel the problem is related to inability to use true color as I tried to specify to use truecolor instead of palette based color but not successful.
use GD; $im = new GD::Image(1000,100,1); code to generate polygons (triangle) and rectangles print $im->png(0);
Thanks Nagesh

Replies are listed 'Best First'.
Re: How to improve image resolution
by BrowserUk (Patriarch) on Aug 06, 2005 at 05:42 UTC

    How are you generating your images? What format are you writing your images to disk in? What compression level (if any) are you specifying when writing them?

    What, if anything, you can do to improve the appearance of your images depends very much on what you are already doing.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      Thanks for the posting. Sorry for not including these details before. I am basically creating an image using the image() and printing it in gif format. The images does nothave smooth margins and also the colors donot appear as true color. I am not aware of any compression. Thanks
      $im = new GD::Image(1000,100); blah...blah print $im->gif;

        Try adding the truecolor parameter when you create your images:

        $im = new GD::Image(1000,100, 1); ## Note the extra parameter!

        From the GD docs:

        The optional third argument, $truecolor, tells new() to create a truecolor GD::Image object. Truecolor images have 24 bits of color data (eight bits each in the red, green and blue channels respectively), allowing for precise photograph-quality color usage. If not specified, the image will use an 8-bit palette for compatibility with older versions of libgd.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: How to improve image resolution
by neniro (Priest) on Aug 06, 2005 at 07:46 UTC
    Remember that the .gif-format only allows a 256 color-palette, so it's ugly to save 24-bit images in it (extremly with a fixed palette). Try .png24 or .tif, these formats use a non-lossy compression - so the results should be much better.

      You can save a 24 bit image as a GIF without any loss of quality, provided that your 24 bit image only uses 254 colors other than black or white, and the program doing the conversion supports adaptive palettes, rather than using the 'web' palette, or the system palette.

      The problem with GIF is if there are too many colors, or the colors aren't defined in the color palette, the graphics program must either select the 'closest' color (for which every program handles this differently -- some just use the closest in terms of an RGB color cube, while others have algorythms to deal with a typical human's ability to differentiate color), or it must 'dither' the image, which involves lots of little dots of different color to make it look like the color from a distance, like the impressionist styles of Monet or van Gogh painting.

      An overly restrictive color palette actually hurts the compression of GIF images, as it tracks horizontal bands of the same color -- so dithering breaks up continuous bands of color.

      Because of this, GIF does well with computer generated graphics, with solid colors, no gradients, and little or none anti-aliasing. It does not handle photo realistic images, which JPEG is better for. (and yet -- JPEG has the exact opposite problem -- it doesn't handle solid colors well, especially if there are sharp changes between colors)

      GIF's only loss comes from the palette selection -- the compression is LZW, the same as used in TIFF.

      So, what's the point of all this? Well, it's that every image format has their own advantages -- TIFF is a great format for interchange between graphics programs, but it's not good for compatability on webpages. You just have to be aware of the advantages/disadvantages of each format, and select what works best in a particular situation.

      Update: as BrowserUK pointed out, I didn't complain about PNG. There's a few different versions, and varying support for them. There's good support for PNG in web browsers, but you still may have to deal with the occassional older browser depending on your audience. (but it does hold the advantage in that field as it's a w3c recommendation). It also holds the advantage of being designed after the others, so it built on the strengths of each one, and mitigated the problems. The only thing that I'd say gives it a significant disadvantage over any other format is its non-lossy compression -- as such, a JPEG tends to be smaller for photo-realistic images (specifically because it trashed some of the data) I'd say the problems with PNG tend to be more with improperly using it, than the format on its own.

        You don't mention png. It seems to address the problems with both gif (limited palette) and jpg (lossy compression).

        I wonder if I missing something?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
        jhourcle++ (great explanation)

        If I want to put graphics onto the web I shrink/convert them to .gif or .png - as long as they're work-in-progress I want them in the best possible quality and un- or lossless-compressed (as .tif).

Re: How to improve image resolution
by ChemBoy (Priest) on Aug 06, 2005 at 05:53 UTC

    A slightly more narrow approach to BrowserUk's questions above: are you saving anything as a JPEG? If so, you almost certainly shouldn't be—GD is a terrific tool for 8-bit graphic manipulation, but 8-bit JPEGs are not generally especially pleasant to look upon, and things that should be 8-bit GIFs or PNGs, when saved as JPEG, look fuzzy and are larger than the GIF or PNG would be.



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

      Thanks for the posting. I am using gif format to create the image
Re: How to improve image resolution
by bradcathey (Prior) on Aug 06, 2005 at 12:06 UTC

    As a designer I am pretty particular about my images, and I have never been happy with the optimization and resulting quality of GD images, gif or jpg. There is no comparison between those images I optimize in Photoshop and those produced by GD.

    Maximizing the color settings helps, but does not seem to have an effect on the overall resolution or quality of the image. It seems there must be other mitigating factors in how its "engine" does the work.

    I have never tried Image::Magick, but I get the impression from what I have read that it is better. I have also gotten the feeling that Implementing it is difficult it, but I have no experience.

    Anyway, you're not alone. My 2¢.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-16 04:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found