简体   繁体   中英

Hexadecimal number string to colour pallette

I have a massive string of hexadecimal numbers. I want to make an image out of this string where each pixel's colour value is defined by (the first, second, third ...) 6-letter strings from this massive string. I'm hoping the image will look something like this: http://i.stack.imgur.com/8jKGx.jpg . Although there will be many more pixels.

I'm a noob when it comes to coding so any help/pointers would be massively appreciated! I've done some tests by entering the hexadecimal values manually into illustrator but it takes far too long.

You should loop through the string, counting by 6, to pull out each individual color. I recommend using python, because you can also use a library to create the image once you have all the values.

Take a look at this question for tips on looping: Iterate over a string 2 (or n) characters at a time in Python

This should work. This will create a BufferedImage with a defined height and width from the string hexString. You can then draw it to a JPanel or save it to a file.

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bi.getGraphics();
for(int i = 0; i < hexString.length(); i += 6){
    int x = i % width;
    int y = (int) Math.floor(i / width);
    Color hexColor = new Color(Integer.parseInt(hexString.substring(i, i + 6), 16);
    g.drawRect(x, y, 1, 1);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM