Skip to content

Instantly share code, notes, and snippets.

@esnosy
Created October 4, 2023 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esnosy/0abdffda371689c563a72f08bd6f895c to your computer and use it in GitHub Desktop.
Save esnosy/0abdffda371689c563a72f08bd6f895c to your computer and use it in GitHub Desktop.
Write Eigen Matrix as PGM image
constexpr size_t N = 900;
Eigen::Matrix<uint8_t, -1, -1> output(N, N);
output.fill(255);
std::ofstream ofs("output.pgm", std::ofstream::binary);
ofs << "P5\n"
<< N << ' ' << N << "\n255\n";
// Otherwise the image will be flipped due to how PGM file format is layed out
output.rowwise().reverseInPlace();
ofs.write(reinterpret_cast<char *>(output.data()), N * N * sizeof(uint8_t));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment