-3

I want to convert a JPEG file into its binary equivalent and then convert it back to its JPEG form. i.e Convert a JPEG file into 1's and 0's and output this into a text file and then take this text file and convert it back to the original image (Just to check if there are no errors in conversion)

I have tried doing this with binascii module in python, but there seems to be a problem of encoding that i cannot comprehend.

It would be really great if someone could help me out with this!

P.S: A solution in Java would be even more appreciated

Meet
  • 35
  • 1
  • 2
  • 3
  • Are you saying you want the text file to actually contain "0" and "1" characters? Or do you mean something else? – Dawood ibn Kareem Nov 02 '13 at 04:37
  • So do you want Java *or* Python? And why the Huffman Encoding tag? – user2864740 Nov 02 '13 at 04:37
  • 3
    What do you actually want this for? Technically, a JPEG file is binary already. – Christian Ternus Nov 02 '13 at 04:39
  • 2
    @DavidWallace yes, the text file should contain actual 0 and 1 characters as a representation of the binary code of JPEG – Meet Nov 02 '13 at 04:49
  • 2
    @ChristianTernus yes, i do understand that. i just want that binary to be represented a "1" and "0" characters – Meet Nov 02 '13 at 04:50
  • @user2864740 a solution in Java would be great! from what i know Huffman Coding is used for lossles data compression which i think could come in handy in this question. – Meet Nov 02 '13 at 04:53
  • Please provide an example of what you mean by "binary equivalent". Provide a sample image (something small, like 4x4 pixels) and show us what the "binary equivalent" of it is. – mpenkov Nov 02 '13 at 05:36
  • This is a ridiculous "on hold" reason. Following my initial question, OP has made it abundantly clear what they are asking. If it were unclear, he/she wouldn't have received two good answers. The only reason I can imagine for this question to be put "on hold" would be OP's failure to demonstrate his/her own effort to solve the problem. The reason that I am highlighting this is because there's no way for OP to tell what changes would have to be made to the question to get it reopened; which means that "on hold" effectively means "destroyed"; and that is unfair. – Dawood ibn Kareem Nov 02 '13 at 21:55

2 Answers2

6

Well, you'll be sorry ;-), but here's a Python solution:

def dont_ask(inpath, outpath):
    byte2str = ["{:08b}".format(i) for i in range(256)]
    with open(inpath, "rb") as fin:
        with open(outpath, "w") as fout:
            data = fin.read(1024)  # doesn't much matter
            while data:
                for b in map(ord, data):
                    fout.write(byte2str[b])
                data = fin.read(1024)

dont_ask("path_to_some_jpg", "path_to_some_ouput_file")

Of course this will convert any file to a file 8 times larger composed of "1" and "0" characters.

BTW, I'm not writing the other half - but not because it's hard ;-)

Tim Peters
  • 67,464
  • 13
  • 126
  • 132
3

Java solution to convert any file (not just JPG) to binary:

    File input= new File("path to input");
    File output = new File("path to output");

    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(input));
         BufferedWriter bw = new BufferedWriter(new FileWriter(output))) {
        int read;
        while ((read=bis.read()) != -1) {
              String text = Integer.toString(read,2);
              while (text.length() < 8) {
                    text="0"+text;
              }
              bw.write(text);
        }            
    } catch (IOException e) {
            System.err.println(e);
    }
HQCasanova
  • 1,158
  • 1
  • 10
  • 15
vandale
  • 3,600
  • 3
  • 22
  • 39