替换JPG图像文件中的图像,但保留元数据? [英] Replace the image in a JPG image file but keep the metadata?

查看:152
本文介绍了替换JPG图像文件中的图像,但保留元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以替换JPG的图像部分但保留嵌入的元数据?或者,有没有办法可靠地将所有元数据从一个JPG图像复制到另一个JPG图像?

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?

背景:我有一个存储JPG图像的ASP.NET Web应用程序。用户使用tinyMCE图像编辑器在浏览器中编辑图像(调整大小,裁剪等),然后浏览器将此修改后的图像发送到服务器。我需要用原始图像文件替换原始图像文件,同时保留原始元数据。

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.

我的第一种方法是复制System.Drawing.Imaging.PropertyItems并复制一些使用InPlaceBitmapMetadataWriter的项目,但是这种方法很多人都错过了很多属性。我需要更可靠的东西,如果我能够替换可能是票的图像部分,我就会想到它。但谷歌搜索没有透露任何内容。

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.

推荐答案

你可以用ImageMagick做到这一点 - 这是一种方式。

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

加载原始图像使用您要保留的元数据,然后加载图像并将其复合到原始文件的顶部并保存,ImageMagick将保留原始图像的元数据。让我们举个例子。

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.

这是原始图片,让我们用 jhead

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 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

...突然,我刚刚在Mac上制作的灰色矩形是在去年圣诞节那天拍摄的,与湖区的原版完全相同: - )

... 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

这篇关于替换JPG图像文件中的图像,但保留元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆