0

I'm editing a JPEG image (blurring faces) and want to modify as little data as possible, namely I'm trying to keep the metadata as intact as possible. However, all the libraries for reading and writing image files I've tried so far leave some of the metadata behind.

Here's what I'm currently doing in Python (with Pillow):

import PIL.Image
import numpy as np

image = PIL.Image.open(input_fpath)

blurred_data = blur_image(np.array(image_data))
image.frombytes(blurred_data.tobytes())

image.save(output_fpath, quality="keep", sampling="keep", qtables="keep", **image.info)

Which loses at least the comment and JFIF blocks. I'm thinking that maybe I need to interact directly with libjpeg but maybe there's some tool or library that I'm missing.

carandraug
  • 12,938
  • 1
  • 26
  • 38
  • 1
    This is an easy way https://stackoverflow.com/a/34352134/2836621 – Mark Setchell Mar 16 '22 at 11:17
  • @MarkSetchell that looks like a neat trick but has the issue that it opens and resaves the image data leading to another round of introducing visual artifacts – carandraug Mar 16 '22 at 12:20
  • I don't understand why you create a new image from the Numpy-blurred stuff in the second-to-last line if you want to retain the metadata? Why don't you `paste` the Numpy-blurred stuff back on top of your original image? – Mark Setchell Mar 16 '22 at 12:29
  • @MarkSetchell I don't create a new image, I use `frombytes` in the existing `Image` instance to retain the metadata, quality, quantization tables, etc. – carandraug Mar 16 '22 at 12:54
  • @MarkSetchell the answer you linked previously uses graphicsmagick `convert` to create a new image and uses `jhead` to inspect the header. But reading `jhead` documentation it seems that it can be used to transplant the metadata (or at least part of it). I need to do more testing but thank you for the lead. – carandraug Mar 16 '22 at 13:05
  • You might find `exiftool` better for that kind of thing https://stackoverflow.com/a/70529583/2836621 and https://stackoverflow.com/a/59246026/2836621 and https://stackoverflow.com/a/70708133/2836621 – Mark Setchell Mar 16 '22 at 13:42

0 Answers0