Roger
Roger

Reputation: 2326

Replace the image in a JPG image file but keep the metadata?

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.

Upvotes: 4

Views: 3771

Answers (3)

fall
fall

Reputation: 1202

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.

Upvotes: 2

Mark Setchell
Mark Setchell

Reputation: 207698

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

Upvotes: 4

ganchito55
ganchito55

Reputation: 3607

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

Upvotes: -1

Related Questions