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

后端 未结 2 674
孤独总比滥情好
孤独总比滥情好 2021-01-19 17:24

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 anothe

2条回答
  •  梦谈多话
    2021-01-19 18:23

    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
    

    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:

    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
    

提交回复
热议问题