Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6207139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:34:36+00:00 2026-05-24T05:34:36+00:00

If I have a PNG image opened as a BufferedImage, is it possible to

  • 0

If I have a PNG image opened as a BufferedImage, is it possible to reduce the palette in the PNG image so that there is less colour (less bits per pixel / colour depth)?

For example, if you look at Colour depth in Wikipedia, I would like to use 16 colours in my PNG image (3rd image down the right hand side).

If it’s not possible with Java 2D, is there a library out there that will allow me to do this effectively?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:34:37+00:00Added an answer on May 24, 2026 at 5:34 am

    I think Martijn Courteaux was right:

    comparison

    Here is example implementation:

    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.awt.image.IndexColorModel;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    public class ImagingTest2 {
        public static void main(String[] args) throws IOException {
            BufferedImage src = ImageIO.read(new File("in.png")); // 71 kb
    
            // here goes custom palette
            IndexColorModel cm = new IndexColorModel(
                    3, // 3 bits can store up to 8 colors
                    6, // here I use only 6
                    //          RED  GREEN1 GREEN2  BLUE  WHITE BLACK              
                    new byte[]{-100,     0,     0,    0,    -1,     0},
                    new byte[]{   0,  -100,    60,    0,    -1,     0},
                    new byte[]{   0,     0,     0, -100,    -1,     0});
    
            // draw source image on new one, with custom palette
            BufferedImage img = new BufferedImage(
                    src.getWidth(), src.getHeight(), // match source
                    BufferedImage.TYPE_BYTE_INDEXED, // required to work
                    cm); // custom color model (i.e. palette)
            Graphics2D g2 = img.createGraphics();
            g2.drawImage(src, 0, 0, null);
            g2.dispose();
    
            // output
            ImageIO.write(img, "png", new File("out.png"));   // 2,5 kb
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PNG image that has an unsupported bitmap graphics context pixel format.
I have an image (PNG file) that has an alpha channel that is set
I have a PHP class that creates a PNG image on the fly and
I have a PHP script that can encode a PNG image to a Base64
I have a slow connection that I need to send a PNG image over
I have two png image files that I would like my android app to
I have a png image on Library that i have declared it via Properties
Here's a question: let's say we have a png image 200x200 that is transparent
I have created a png image in photoshop with transparencies that I have loaded
I have a PNG image that uses transparency (it is actually a circle with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.