How do I import an "idx3-ubyte" file?

I am wondering how to import the files found on this website into Processing:

They’re supposed to contain thousands of 28x28 byte arrangements, each with its own “grayscale” value. In essence, they’re images. But the files don’t have a .dat extension, an extension I’ve learned to use thanks to the saveBytes() and loadBytes() reference tutorials on the Processing website.

I don’t know what an .idx3-ubyte extension is and how to import it into an arbitrary Processing array correctly, and the Java guides I found online didn’t help either, as the usual Processing code that I’m familiar with looks nothing like those chunks of code.

I also don’t know how the bytes are structured inside the file, although I would assume the first 784 (28*28) bytes would correspond to the first supposed image, going left to right, downwards, and the next 784 would correspond to the second image, and so on.

Even figuring out how to convert this into a .dat file would help, as I am able to import those into and out of Processing easily.

(Question copied from:

1 Like

Hi @Tsskyx, I am honestly not routined in this field, but have you seen ? If not, it seems like it might be able to help you.

1 Like

Okay. Turns out the answer was very simple, Processing is already perfectly capable of reading that file, I didn’t even know that! I just saw that everyone was having issues with it and didn’t understand the file extension myself. :smiley:

Here’s the code I quickly put together:

byte[] trainSet, labelSet;
int cellSize = 15;
int count = 0;

void settings() {
  size(28*cellSize, 28*cellSize);

void setup() {
  trainSet = loadBytes("train-images.idx3-ubyte");
  labelSet = loadBytes("train-labels.idx1-ubyte");
  textAlign(CENTER, CENTER);

void draw() {
  for (int i = 0; i < 28; i++) {
    for (int j = 0; j < 28; j++) {
      fill(int(trainSet[784*count+28*i+j+16])); //the first 16 bits are meta data
      rect(cellSize*j, cellSize*i, cellSize, cellSize);
  fill(255, 0, 0);
  text(int(labelSet[count+8]), 30, 30); //the first 8 bits are meta data

void mousePressed() {

Side note, these files are training and testing examples for a simple digit-recognizing neural network, and I’m so glad that the standard importing method still works for these files, else I would probably have to generate those testing examples myself, which would get unwieldy very quickly. :confused:

1 Like