Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to keep Imager's image files small ?

by palkia (Monk)
on Jul 27, 2012 at 18:46 UTC ( [id://984066]=perlquestion: print w/replies, xml ) Need Help??

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

Hello
I would like to keep my images as small as possible,
preferably by lowest color def possible but without going all grey.
My thoughts were to make it a simple 16 colors (not 16 bits) images,
but I don't see how this can be done even after going over several Imager cpan pages.

Suggestions ?
Thx
  • Comment on How to keep Imager's image files small ?

Replies are listed 'Best First'.
Re: How to keep Imager's image files small ?
by tonyc (Friar) on Jul 28, 2012 at 00:19 UTC

    This is fairly simple:

    1. call to_paletted() with the quantization options you want, in your case max_colors => 16
    2. call write()

    For example

    my $pal = $source->to_paletted ( max_colors => 16, translate => "errdiff", ) or die $source->errstr; $pal->write(file => "foo.png") or die $pal->errstr;

    That said, unless your images start with a small number of colours (non-photographic, no blends, little anti-aliasing) the results are going to be fairly ugly. You should test with some representative source images.

Re: How to keep Imager's image files small ?
by BrowserUk (Patriarch) on Jul 27, 2012 at 19:20 UTC
    My thoughts were to make it a simple 16 colors (not 16 bits) images

    In general, there are no such things as 16-color images.

    The closest you'll get is palettised images which use 8 bits per index (3-bits each for red & green and 2-bits for blue) giving a 256 colors in total.

    Of course, you do not need to use all 256 palette entries. If you only use 16 colors, then the size of the palette stored in the image file will (sometimes) be reduced(*), but the image will still require 1-byte per pixel to store the indexes, so the gain of going below 256 colors is minimal(**).

    (*From approximated 4*256=1024 bytes for 256 entries; to 4*16=64 bytes for 16 entries. Not all image tools do this.)

    (**There is the possibility that using a palettised .png that only uses 16 colors might compress more easily thereby further reducing the size of the image file.)


    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?

      It seems that Imager supports dithering. At least Imager::ImageTypes mentions "Floyd Steinberg error diffusion", which seems to suggest that it can be used to further reduce the number of used colours at the cost of image resolution.

        I think you'll find that once you write the images to disk in any of the popular (gif/jpg/png) formats, they'll be stored as 8-bit indexed palletised images.

        The tiff file format might theoretically support 4-bit indexes; but I've never come across one; nor any tool that would write 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?

Re: How to keep Imager's image files small ?
by BrowserUk (Patriarch) on Jul 27, 2012 at 23:12 UTC

    Looking into it a little further, it seems that Imager can write Windows .BMP files with 4-bits per pel.

    However, as it doesn't support compressing those images, the file sizes will likely be much larger than the same image written as 8-bit palettised in .png, .gif or .jpg format


    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?

      Imager doesn't compress BMP files at all, though it can read BI_RLE4 and BI_RLE8 compressed images.

      It never seemed worth the effort of implementation, with so many formats with useful compression algorithms.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-06-23 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.