Search code examples
arraysimagecolorshexmit-scratch

Image to hex triplet(Any language)


Welcome,
this is not that tough please help
I use a platform (Scratch). I need to print an image using a pen
(You may not understand what I mean in the above if you don't know the platform BUT)
What I need is to convert the image (below), to hex triplet array.

Image to be converted

(Note I don't need the above image to be converted I need to know how to convert any image)

Hex triplet array sample

note: this one(above) is only a sample.

For better Understanding of hex array and image



I am familiar with java ,python,javascript.... most popular language
Give me how to convert image to hex triplet array using any language or tool

if you know the platform (scratch) you may get a hint here



Solution

  •     Path path = Paths.get("... .png");
    
        System.out.println("Image: " + path);
        try {
            BufferedImage image = ImageIO.read(path.toFile());
    
            int w = image.getWidth();
            int h = image.getHeight();
            for (int x = 0; x < w; ++x) {
                for (int y = 0; y < h; ++y) {
                    int rgb = image.getRGB(x, y);
                    System.out.printf("%06X%n", rgb & 0xFF_FF_FF);
                }
                System.out.println();
            }
            System.out.println("Done");
    

    getRGB delivers the color in ARGB format.