what is the best way to deal with a image for compression by Arithmetic coding?

Viewed 272

I have been working on a project in node js for compression of .txt file. So far i have been succeeded in that goal by using Arithmetic coding for encoding and decoding of data but now i want use that code for compression of images but i can't get the idea how to do that. My main problem is how should i read data from image to compress. I did not have slightest idea how to do that with Buffer , stream or even with Blob (by sometime searching on interest theses were the possible way to do that.).

I knew that dealing with images in Python is quite easy so i though to make script to read images with python. For just now i am not integrating Python and Node js that will be later step.

I had small experience with some python libraries like open-csv and Pillow (PIL). So far i have used cv2.imread() function to read an image and store it as binary in .txt file. as for now i have not integrated it with Node js.

from cv2 import cv2

img1 = cv2.imread('cat.png',0)

with open("cat.txt", 'wb') as out:
    out.write(img1.tobytes())

Now i use that cat.txt file and read that data by fs in node js and than compress and decompress it by Arithmetic coding.

Now to main problem :

When i read image in python and store it in cat.txt it's size is quite bigger than cat.png file. Even such that after compressing that cat.txt file the resultant compressed_cat.txt is bigger than that of cat.png.

cat.png = 242kb, cat.txt(before compression) = 971kb, compressed_cat.txt(after compression) = 251kb

I think i am reading or storing the file incorrectly. I don't know how to overcome that problem.

Some additional help needed

  • Is there any way to process image in node js like in python so i don't need to integrate it later. I have tried Multer and Jimp but i could not find anything in it like python open-csv
  • And i don't know how to store the data after compression because mostly it is stored in either .zip or .rar. I have never stored/read data in/form that type of format.
2 Answers

I don't have a complete answer, but I think the following information would help -

The image formats like .png, .jpg, etc already represent image in a compressed format. Some are not lossy, but most commonly used for storage are lossy to minimize size. So, honestly, I doubt any text compression algorithms could beat already existing image compression formats.

One thing which I think is wrong with your approach is reading the image using open-cv. It gives you a multi-dimentional array representation of the image if I am not mistaken. Great if you want to work with image, but I think you just want to read the bytes from image file directly, so you can convert it back to the original image file. Now, how you work with bytes, or underlying bits is upto you. I hope this answers part of your question.

I was looking in the wrong direction for the solution. But @Harsh comment helped I think you just want to read the bytes from image file directly, so you can convert it back to the original image file. I made 2 big changes

First the way to read the image.

I don't need python to read/write the image as it easily can be done in node-js.

const fs = require("fs");

let data = fs.readFileSync('Cat.png', 'binary');

fs.writeFileSync('Cat_Copy.png', data, 'binary');

Second Selecting the Right file to Compress

Mostly .jpeg, .jpgand .png are already compressed so normal compression techniques does not work on them. But .tiff files are not compressed and at least my algorithm works on this format. Still it is not very efficient.

Third Selecting the Right format to save the compress file

Well that was not a big problem as we can save it without any extension what does really matters is the encoding in which it is saved. Well in js ucs2 takes less space on disk than utf8. I am saving it as filename.ac as it represents Arithmetic Coding.

Related