0

I try to save a matrix of rgb pixel in file with C# and read it in python. If my image is 243*507, then the len of the file must be (height * width * rgb) then 243*507*3 (I save each rgb value as a byte in file). When I read this file with C#, it have the good len (369603), it work perfectly. But with python, I get 369570 or with another file, a bit more or a bit less. It always missing bytes in the python string when I read the file. For get the image file, download frame.txt

Code in python:

f = open(path, "r")
if f.mode != 'r':
    print_cerror("Can't read file")
contents = f.read()
f.close()
print(len(contents) # Not the good len

Code in C#:

if (File.Exists(file_path))
{
    string text = System.IO.File.ReadAllText(file_path);
    Debug.Log("READ LEN = " + text.Length); // Perfect 369603 !
}

EDIT: When I tried to use "rb" the size of the file is 477537 instead of 369603. I think that it have a header or other informations. How can I get only the body of the file ? I tried with seek(0, 1, 2) but it only skip 0, 1, 2 first bytes...

OK PROBLEM SOLVED: My bad, I wrote bytes as text with C# then the file wasn't good x) Sorry

  • Read it with `"rb"` mode. Now you're reading it as a text, not bytes, and probably "empty" bytes get dropped. – h4z3 Nov 28 '19 at 12:07
  • Or, because Python3 uses Unicode, bytes get interpreted together as a single character? – h4z3 Nov 28 '19 at 12:09
  • 2
    Don't open files in text mode. Python translates line separator sequences (`\r\n` -> `\n`), and in Python 2 on Windows, [an EOF byte stops reading early](https://stackoverflow.com/questions/20695336/how-to-process-huge-text-files-that-contain-eof-ctrl-z-characters-using-python). In Python 3, the default codec used can differ, but some codecs do let you open a binary image 'as text', you just get gibberish text and if this is a multi-byte codec the result will also be shorter. Images are binary data, read them as such. – Martijn Pieters Nov 28 '19 at 12:17
  • @h4z3 When I tried to use "rb" the size of the file is 477537 instead of 369603. I think that it have a header or other informations. How can I get only the body of the file ? I tried with seek(0, 1, 2) but it only skip 0, 1, 2 first bytes... – Timothée Dautrême Nov 28 '19 at 13:00
  • @MartijnPieters Okay but I tried to delete EOF but when I read as "rb", the file size is 477537 instead of 369603 – Timothée Dautrême Nov 28 '19 at 13:03
  • @TimothéeDautrême you seem to be reading the file as text in C# too. Are you certain that that doesn’t also involve transformations? – Martijn Pieters Nov 29 '19 at 00:12

1 Answers1

0

If you want the size of a file in bytes you can use

import os
print(os.stat(path).st_size)
Floppy52
  • 184
  • 9