How can i write to file in binary code at Wolfram mathematica? - wolfram-mathematica

I trying to write to file in binary code.
In mathematica i have some sting that i encoded with huffman code , i need to write them to binary file and see what is the file size.
what is the best way to do it?
thanks.

You should be able to use this procedure for your purpose. Here some binary data is created in file and written out in binary format to streamoutput.gif:
file = ExportString[Graphics[Disk[]], "GIF"];
stream = OpenWrite["streamoutput.gif", BinaryFormat -> True];
BinaryWrite[stream, file];
Close[stream];
Import["streamoutput.gif"]
source:
Converting graphics with ExportString

Related

EOFError when converting gensim word2vec to binary format

I have a pretrained embeddings with word2vec format in txt. I loaded it and then saved it to .bin. But I cannot load this embeddings as an EOFError: unexpected end of input; is count incorrect or file otherwise damaged?
My original code is:
model = KeyedVectors.load_word2vec_format(wordfile)
model.save_word2vec_format("file.bin",binary=True,write_header=True)
bin_model = KeyedVectors.load_word2vec_format("file.bin",binary=True)
And I can load this file.bin with a limit arguement: KeyedVectors.load_word2vec_format("file.bin",binary=True, limit=10000).
Is there some other process needed when I save embeddings?
There's a good chance that your .bin file has an incorrect leading-count, or the file has been otherwise been damaged/truncated – because that error means the file declared in its header (1st line) a larger number of word-vectors than were found during attempted-load.
So, if you downloaded it or copied it from somewhere, check the original source, to make sure you've got the full file.
Is there a reason you're performing this conversion? The formats are essentialy equivalent, and result in the exact same object-in-Python after loading.
If there's any tiny on-disk size savings in binary-format, you could probably save more by GZIPping the file (which the .load_word2vec_format() will also happily decompress, if it sees a trailing .gz on the filename).

X937 file decoding in golang?

I am trying to open and parse an x937 file - which I BELIEVE is usually encoded in EBCDIC 0037.
I am using the following library to decode the main bytes of the file :
"github.com/gdumoulin/goebcdic"
and the code I am using is as follows, for now.
// Bytes in file.
b, _ := ioutil.ReadFile("testingFile.x937")
fmt.Println(string(goebcdic.ASCIItoEBCDICofBytes(b)))
But if I dump the output of my file, I still don't seem to get anything that matches what I would have thought I would be looking for.
Any ideas on how I can work with this?

Save a double matrix as an image file in MATLAB

I have a matrix sig_matrix of data type double, the values are like:
3,0450 3,0450 3,0450
2,6200 2,6050 2,5900
2,5250 2,5200 2,5150
2,3800 2,3800 2,3650
2,6050 2,6650 2,7350
I need to save as an image file, but the problem is: image types like jpg, png only accepts integer data (uint8, uint16), but I really need to save the data as double.
I tried to save as a TIF file using the code below:
t = Tiff('test.tif', 'w8');
setTag(t,'Photometric',Tiff.Photometric.RGB);
setTag(t,'ImageLength',length(sig_matrix));
setTag(t,'ImageWidth',length(sig_matrix));
setTag(t,'BitsPerSample',64);
setTag(t,'SamplesPerPixel',3);
setTag(t,'SampleFormat', 3);
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
setTag(t,'Compression',Tiff.Compression.None);
write(t,repmat(sig_matrix, [1 1 3]));
close(t);
It worked, but then I tried to convert this file to yuv (using ffmpeg for example) but I got an error (Invalid TIFF header to be more specific).
So, I need to save a double matrix as a image file and then convert to yuv.
Anyone could help me please?
Obs.: I can't use mat2gray or something like that because I need that specific double data to convert to yuv

Cutting data appended to a .jpg file and save as mpg file

The background of my problem is that I want to extract the video data of Motion Photos (taken by my Samsung S7). Manually it is easy but time consuming. Just open the .jpg file in a HexEditor and extract all data after the line "MotionPhoto_Data". The first part is the image and the second part is the video.
My current code is
im = 'test.jpg'
with open(im, 'rb') as fin:
data = fin.read()
data_latin = data.decode('latin1')
fin.close()
position = data_latin.find('MotionPhoto_Data')
data_pic = data[:position]
data_mpg = data[position:]
My problem now is that I can´t figure out how to save these strings in a way that data_pic is saved as a working jpg and data_mpg as a working video.
I tried
with open('test_pic.jpg', 'a') as fin:
fin.write(str(data_pic))
fin.close()
But this didn´t worked. I think there is a basic issue on how I try to handle/save my data but I can´t figure out how to fix this.
I assume you use python 3 as it is tagged that way.
You should not decode with 'data.decode('latin1'). It is binary data.
data = fin.read()
Then later write it also as binary data:
with open('test_pic.jpg', 'ab') as fout:
fout.write(data_pic)
fout.close()

Converting WAV to GSM using pysox

I'm experimenting with pysox and trying to simply convert a WAV file to GSM.
I'm currently using the following approach which works just fine:
infile = pysox.CSoxStream("input_file.wav")
outfile = pysox.CSoxStream('output_file.gsm','w',infile.get_signal())
chain = pysox.CEffectsChain(infile, outfile)
chain.flow_effects()
outfile.close()
I wonder if there's a better/builtin way without using effects (as i'm not applying any effects) .
thanks in advance
I found that i actually must use libsox effect as i'm changing the rate :
chain.add_effect(pysox.CEffect("rate", ["8k"]))
Without adding this line the output appears in slow motion (since my original file can have different rate)

Resources