2

Okay, so I am attempting to compress an image file as much as possible for storage (NOTE, before being able to view the file I would un-compress it, the compressed file does not need to be usable, it just needs to be able to be un-compressed) I would like to do this in python, and I have the code as follows:

import bz2
from shutil import copyfileobj

x = 0


while True:
    while x == 0:
        with open("START.JPG", 'rb') as input:
            with bz2.BZ2File("END.JPG", 'wb', compresslevel=9) as output:
                copyfileobj(input, output)

                print ("STAGE ONE COMPLETE")
                x = 1

    while x == 1:
        with open("END.JPG", 'rb') as input:
            with bz2.BZ2File("FINAL.JPG", 'wb', compresslevel=9) as output1:
                copyfileobj(input, output1)

                print ("STAGE TWO COMPLETE")
                x = 2

So far it does compress the image down, but only by about 1/3 of a MB, I thought by compressing the compressed file I could possibly compress it farther, however when I do this the FINAL.JPG file is larger than my first compressed file. So my question is: What is the best way to compress a file (I know different file types need slightly different compression, and my end goal is to be compressing MP3 audio files), and why is it that my compressed file becomes larger when I compress it a second time?, Also how small could I make one of these files before it becomes un-able to un-compress?

Thank you in advance for all the help I'm sure I will get!

1
  • Just a thought from my reading on compression, wouldn't it be most efficient to use memory chunking as well? I have never worked with it but I'm guessing it's something I should be familiar with. Commented Jun 2, 2013 at 18:43

1 Answer 1

5

JPEG image files and MP3 audio files are already compressed. Aside from some metadata (such as EXIF tags on JPEG images) that is stored in an uncompressed format, a compression algorithm will make little to no headway on these types of files.

Keep in mind that compression algorithms are not magic — to generalize wildly, they work by finding patterns in files and encoding those patterns in a more efficient way. If a file is already compressed, it will not contain any patterns that a compression algorithm can work with, so it will be unable to generate a smaller output file.

Sign up to request clarification or add additional context in comments.

5 Comments

I was wondering about that. Does this mean there is no way to store MP3 files more efficiently, or at least not enough to make a big difference? Also is there any other form of audio file that could be compressed farther than MP3 or is MP3 the lowest form of compression without damaging the file permanently? P.S. Thank you for all your help!
Not enough to be significant. The bulk of the data in an MP3 file is already Huffman coded.
I compressed Amarok.mp3 (an album that is one, 60 minute-long song) with xz, which compresses harder than bz2, but the result was only a savings of 0.5%. If you could compress an already-compressed file and be productive each time, eventually you would compress everything down to nothing. But if you start with an uncompressed wav file, that should compress with xz quite well - possibly smaller than the built-in mp3 compression.
Thank you this has been extremely educational, however I still have a few questions. Would a video file (say MP4, or .FLV be able to be compressed, and what would an ideal file type be to compress it to. Also just out of curiousness in general what would you personally say would be the smartest file to compress on a computer (or android phone) to save hard drive room? I know this is allot of questions and I think you immensely for all of your help and support!
Video files are also already compressed. If you want to try compressing something you're likely to have sitting around, try a BMP image file, or an EXE/DLL executable file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.