dwgoon / jpegio

A python package for accessing the internal variables of JPEG file format such as DCT coefficients and quantization tables
Apache License 2.0
73 stars 18 forks source link

JPEG writing with coefficients and quantization matrix #13

Closed ehgusehgus closed 4 years ago

ehgusehgus commented 4 years ago

Is there a function that save jpeg file with the DCT coefficients and quantization matrix directly?(not with the pixel values)

Thank you for reading

dwgoon commented 4 years ago

You can use write method as follows

import jpegio as jio

jpeg = jio.read("image.jpg")
# Modifying jpeg.coef_arrays...
# Modifying jpeg.quant_tables...
jio.write(jpeg, "image_modified.jpg")
ehgusehgus commented 4 years ago

jpegio.write() function seems to cause error.

Traceback (most recent call last): File "", line 1, in File "C:\Users\ehgus\Anaconda3\lib\site-packages\jpegio-0.2.1-py3.6-win-amd64.egg\jpegio\io.py", line 20, in write obj.write(fpath) File "jpegio\decompressedjpeg.pyx", line 195, in jpegio.decompressedjpeg.DecompressedJpeg.write File "jpegio\decompressedjpeg.pyx", line 196, in jpegio.decompressedjpeg.DecompressedJpeg.write File "jpegio\decompressedjpeg.pyx", line 200, in jpegio.decompressedjpeg.DecompressedJpeg._write_markers TypeError: object of type 'NoneType' has no len()

And also something like

jpeg = jio.read("image.jpg") jpeg.write("image_modified.jpg")

does not saving the modified coefficients. It saves with original one.

dwgoon commented 4 years ago

Refer to the code snippet again, and correct your code as follows, please.

jio.write(jpeg, "image_modified.jpg")
ehgusehgus commented 4 years ago

Yes. I've tried like this.

import jpegio
c = jpegio.read('input.jpg')
jpegio.write(c,'image_modified.jpg')

But it produces error like below.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ehgus\Anaconda3\lib\site-packages\jpegio-0.2.1-py3.6-win-amd64.egg\jpegio\io.py", line 20, in write
    obj.write(fpath)
  File "jpegio\decompressedjpeg.pyx", line 195, in jpegio.decompressedjpeg.DecompressedJpeg.write
  File "jpegio\decompressedjpeg.pyx", line 196, in jpegio.decompressedjpeg.DecompressedJpeg.write
  File "jpegio\decompressedjpeg.pyx", line 200, in jpegio.decompressedjpeg.DecompressedJpeg._write_markers
TypeError: object of type 'NoneType' has no len()

I think this error is caused by creating empty new object in the writing function and try to save that object.

And if I try the writing like

jpeg = jpegio.read("image.jpg")
jpeg.write("image_modified.jpg")

It does not save the modified coefficient but save the original one

dwgoon commented 4 years ago

Thanks for giving me the hint. I've fixed jpegio.write following your comment. Test the revised code, please.