Possible to add jpeg_add_quant_table to the JPEG encoder ?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Possible to add jpeg_add_quant_table to the JPEG encoder ?



In my quest to better image quality, I was wondering if it could be possible to add to the JPEG encoder the possibility to pass it the quantization tables instead of using the default tables?

In libjpeg there's a call for that:
jpeg_add_quant_table (j_compress_ptr cinfo, int which_tbl, const unsigned int *basic_table, int scale_factor, boolean force_baseline)

Presently I'm using cjpeg to save the file because it give me this option but I would prefer if it be integrated directly in PIL. Here's how I save it with cjpeg:

proc = subprocess.Popen('cjpeg -sample 1x1 -qtables my_qtables.qtb -outfile %s' % imgpath, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = proc.communicate('P6\n%s %s\n255\n%s' % (im.size[0], im.size[1], im.tostring()))

The motivation to add that is to be able to specify the tables to improve the output quality because the base tables (use in libjpeg) are far from ideal. With other tables you can achieve prettier result for the same file size. Another motivation could be to experiment with different tables for different purposes.

I could try to do a patch but my C++ is far from good! For the format of the tables (int *basic_table) on the python side, I think it could be an array object or more simpler a string with number separated by any spaces (it's the format use in cjpeg), this way it could be easy to embed tables in code with triple quoted string or read it from a file. You could also copy directly the output of djpeg - v -v. But any format will do the job because it will be easy to write some transform functions from one format to another.

What do you think ?


Image-SIG maillist  -  [hidden email]