4

Is it possible to replace the image portion of a JPG but keep the embedded metadata? Alternatively, is there a way to reliably copy all metadata from one JPG image to another?

Background: I have an ASP.NET web application that stores JPG images. Users use the tinyMCE image editor to edit the image in the browser (resize, crop, etc), then the browser sends this modified image up to the server. I need to replace the original image file with the edited one while preserving the original metadata.

My first approach was to copy the System.Drawing.Imaging.PropertyItems and also copy some items using InPlaceBitmapMetadataWriter, but many properties are getting missed with this approach. I need something more reliable and it occurred to me if I could just replace the image portion that might be the ticket. But googling hasn't revealed anything.

This is an open source product (Gallery Server) so any libraries have to be compatible with this license.

Roger
  • 2,118
  • 1
  • 20
  • 25

3 Answers3

4

You can do this with ImageMagick - here is one way.

Load the original image with the metadata you want to preserve, then load the "fake" image and composite that over the top of the original and save, and ImageMagick will preserve the original image's metadata. Let's do an example.

Here is an original image, let's check the metadata with jhead

jhead original.jpg

File name    : original.jpg
File size    : 2080473 bytes
File date    : 2015:12:18 09:05:53
Camera make  : Apple
Camera model : iPhone 4S
Date/Time    : 2014:12:25 15:02:27
Resolution   : 3264 x 2448
Flash used   : No
Focal length :  4.3mm  (35mm equivalent: 35mm)
Exposure time: 0.0083 s  (1/120)
Aperture     : f/2.4
ISO equiv.   : 50
Whitebalance : Auto
Metering Mode: pattern
Exposure     : program (auto)
GPS Latitude : N 54d 26m 14.84s
GPS Longitude: W  3d  5m 49.91s
GPS Altitude :  226.00m
JPEG Quality : 95

enter image description here

Now, we run the process I suggested, compositing a grey (fake) image of the same size over the top:

convert original.jpg fake.jpg -composite new.jpg

and we get this:

enter image description here

And if we check the metadata of the new image:

jhead new.jpg

File name    : new.jpg
File size    : 43408 bytes
File date    : 2015:12:18 09:08:30
Camera make  : Apple
Camera model : iPhone 4S
Date/Time    : 2014:12:25 15:02:27
Resolution   : 3264 x 2448
Color/bw     : Black and white
Flash used   : No
Focal length :  4.3mm  (35mm equivalent: 35mm)
Exposure time: 0.0083 s  (1/120)
Aperture     : f/2.4
ISO equiv.   : 50
Whitebalance : Auto
Metering Mode: pattern
Exposure     : program (auto)
GPS Latitude : N 54d 26m 14.84s
GPS Longitude: W  3d  5m 49.91s
GPS Altitude :  226.00m
JPEG Quality : 96

... and suddenly the grey rectangle I just made two minutes ago on my Mac was taken on Christmas Day last year at exactly the same spot as the original in the Lake District :-)

If your fake image and original differ in size, you can do this to force them to a common size before compositing (so that the fake completely covers the original) like this:

convert original.jpg fake.jpg -resize 3264x2446! -composite new.jpg
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks - I confirmed this works well when the images are the same size, but as you implied it gets more complicated when they vary. For example, when the edited image is smaller than the original, I have to crop after the composite operation: convert.exe original.jpg edited.jpg -composite - | convert - -crop 320x305+0+0 out.jpg. There is also the issue of some original metadata no longer accurately describing the edited image (width, height). But I can work with it. Cheers! – Roger Dec 18 '15 at 16:05
  • Don't forget you can use parentheses to only apply resizing to one or the other of the images... `convert image1.jpg \( image2.jpg -resize 1000x2000 \) -composite result.jpg` – Mark Setchell Dec 18 '15 at 19:51
2

For copying metadata between images. You can use exiftool, which is designed for metadata manipulation.

exiftool -TagsFromFile  sourceImg.jpg target.jpg

-TagsFromFile means you are copying metadata.

add -overwrite_original before -TagsFromFile if you dont want exiftool to generate backup files.

If you want to copy selected tags or perform recursion, see this answer.

fall
  • 984
  • 11
  • 33
-1

I think that ImageMagick can help you.

I saw that ImageMagick example where you can see how to extract all metadata from a jpg file.

enter image description here

Now if you have the metadata of the original file, you can create a new file with that metadata. I found a example of how to add metadata with ImageMagick

I hope this can help you

Community
  • 1
  • 1
ganchito55
  • 3,559
  • 4
  • 25
  • 46
  • If ImageMagick would work that would be great because it's already integrated into my software. But I can't find any evidence it has this capability. Can you be more specific? – Roger Dec 17 '15 at 23:27
  • @Roger sorry, I was from my phone and I wrote a short answer :S – ganchito55 Dec 18 '15 at 00:01