微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在JPG / PNG图像中写入/编辑元数据

如何解决在JPG / PNG图像中写入/编辑元数据

通常,我从PyExifTool巫婆那里读取图像中的元数据非常有用:

import exiftool 
exiftool.executable = "exiftool.exe" 
img_path = 'test.JPG' 
with exiftool.ExifTool() as et:
    Metadata = et.get_Metadata(img_path)                # read Metadata

要编辑图像(调整大小,裁剪等),请使用枕头或OpenCV库。 示例:

from PIL import Image
img_path = 'test.JPG'
img = Image.open(img_path)
img = img.resize((100,100),Image.BILINEAR) # edit the image
exif = img.info['exif']
img.save('output.jpg',exif=exif)

但是,并非所有元数据都在exif TAG上。 例如:

 'Composite:Aperture': 5.6,'Composite:CircleOfConfusion': 0.0110169622305844,'Composite:FOV': 73.7398575770811,'Composite:FocalLength35efl': 24,'Composite:GPSAltitude': 455.764,'Composite:GPSLatitude': 41.3867478333333,'Composite:GPSLongitude': -70.5641583055556,'Composite:GPSPosition': '41.3867478333333 -70.5641583055556','Composite:Hyperfocaldistance': 1.25520730117252,'Composite:ImageSize': '5472 3648','Composite:LightValue': 13.2927817492278,'Composite:Megapixels': 19.961856,'Composite:ScaleFactor35efl': 2.72727272727273,'Composite:ShutterSpeed': 0.003125,'EXIF:ApertureValue': 5.59834346238078,'EXIF:ColorSpace': 1,'EXIF:ComponentsConfiguration': '0 3 2 1','EXIF:CompressedBitsPerPixel': 3.368460728,'EXIF:Compression': 6,'EXIF:Contrast': 0,'EXIF:CreateDate': '2020:04:11 09:42:31','EXIF:Customrendered': 0,'EXIF:DateTimeOriginal': '2020:04:11 09:42:31','EXIF:DigitalZoomratio': 'undef','EXIF:ExifImageHeight': 3648,'EXIF:ExifImageWidth': 5472,'EXIF:ExifVersion': '0230','EXIF:ExposureCompensation': -0.34375,'EXIF:ExposureIndex': 'undef','EXIF:ExposureMode': 0,'EXIF:ExposureProgram': 2,'EXIF:ExposureTime': 0.003125,'EXIF:FNumber': 5.6,'EXIF:FileSource': 3,'EXIF:Flash': 32,'EXIF:FlashpixVersion': '0010','EXIF:FocalLength': 8.8,'EXIF:FocalLengthIn35mmFormat': 24,'EXIF:GPSAltitude': 455.764,'EXIF:GPSAltitudeRef': 0,'EXIF:GPSLatitude': 41.3867478333333,'EXIF:GPSLatitudeRef': 'N','EXIF:GPSLongitude': 70.5641583055556,'EXIF:GPSLongitudeRef': 'W','EXIF:GPsversionID': '2 3 0 0','EXIF:GainControl': 0,'EXIF:ISO': 100,'EXIF:ImageDescription': 'DCIM\\PAnorAMA\\101_0586\\PAN','EXIF:InteropIndex': 'R98','EXIF:InteropVersion': '0100','EXIF:LightSource': 1,'EXIF:Make': 'DJI','EXIF:MaxApertureValue': 2.79917173119039,'EXIF:MeteringMode': 2,'EXIF:Model': 'FC6310S','EXIF:ModifyDate': '2020:04:11 09:42:31','EXIF:Orientation': 1,'EXIF:ResolutionUnit': 2,'EXIF:Saturation': 0,'EXIF:SceneCaptureType': 0,'EXIF:SceneType': 1,'EXIF:SerialNumber': '46c76034607b1c0616d67f3643c34a5b','EXIF:Sharpness': 0,'EXIF:ShutterSpeedValue': '0.00312701097912618','EXIF:Software': 'v01.08.1719','EXIF:Subjectdistance': 0,'EXIF:SubjectdistanceRange': 0,'EXIF:ThumbnailImage': '(Binary data 10230 bytes,use -b option to extract)','EXIF:ThumbnailLength': 10230,'EXIF:ThumbnailOffset': 10240,'EXIF:WhiteBalance': 0,'EXIF:xpcomment': 'Type=N,Mode=P,DE=None','EXIF:XPKeywords': 'v01.08.1719;1.3.0;v1.0.0','EXIF:XResolution': 72,'EXIF:ycbcrPositioning': 1,'EXIF:YResolution': 72,'ExifTool:ExifToolVersion': 12.05,'ExifTool:Warning': '[minor] Possibly incorrect maker notes offsets (fix by '
                     '1783?)','File:BitsPerSample': 8,'File:ColorComponents': 3,'File:Directory': '.','File:EncodingProcess': 0,'File:ExifByteOrder': 'II','File:FileAccessDate': '2020:09:12 11:46:45+01:00','File:FileCreateDate': '2020:09:12 11:46:45+01:00','File:FileModifyDate': '2020:04:21 09:54:42+01:00','File:FileName': 'PANO0001.JPG','File:FilePermissions': 666,'File:FileSize': 8689815,'File:FileType': 'JPEG','File:FileTypeExtension': 'JPG','File:ImageHeight': 3648,'File:ImageWidth': 5472,'File:MIMEType': 'image/jpeg','File:ycbcrsubsampling': '2 1','MPF:Dependentimage1EntryNumber': 0,'MPF:Dependentimage2EntryNumber': 0,'MPF:ImageUIDList': '(Binary data 66 bytes,'MPF:MPFVersion': '0010','MPF:MPImageFlags': 8,'MPF:MPImageFormat': 0,'MPF:MPImageLength': 255918,'MPF:MPImageStart': 8433897,'MPF:MPImageType': 65537,'MPF:NumberOfImages': 2,'MPF:PreviewImage': '(Binary data 255918 bytes,'MPF:TotalFrames': 1,'MakerNotes:CameraPitch': -90,'MakerNotes:CameraRoll': 0,'MakerNotes:CameraYaw': -114.099998474121,'MakerNotes:Make': 'DJI','MakerNotes:Pitch': -10.5,'MakerNotes:Roll': 5.19999980926514,'MakerNotes:SpeedX': 0,'MakerNotes:SpeedY': 0,'MakerNotes:SpeedZ': 0,'MakerNotes:Yaw': -114.699996948242,'SourceFile': 'PANO0001.JPG','XMP:About': 'DJI Meta Data','XMP:Absolutealtitude': '+455.76','XMP:AlreadyApplied': False,'XMP:CalibratedFocalLength': 3666.666504,'XMP:CalibratedOpticalCenterX': 2736.0,'XMP:CalibratedOpticalCenterY': 1824.0,'XMP:CamReverse': 0,'XMP:CreateDate': '2020:04:11','XMP:FlightPitchDegree': -10.5,'XMP:FlightRollDegree': '+5.20','XMP:FlightXSpeed': '+0.00','XMP:FlightYSpeed': '+0.00','XMP:FlightYawDegree': -114.7,'XMP:FlightZSpeed': '+0.00','XMP:Format': 'image/jpg','XMP:GPSLatitude': 41.38674784,'XMP:GPSLongtitude': -70.56415831,'XMP:GimbalPitchDegree': -90.0,'XMP:GimbalReverse': 0,'XMP:GimbalRollDegree': '+0.00','XMP:GimbalYawDegree': -114.1,'XMP:HasCrop': False,'XMP:HasSettings': False,'XMP:Make': 'DJI','XMP:Model': 'FC6310S','XMP:ModifyDate': '2020:04:11','XMP:RelativeAltitude': '+69.60','XMP:RtkFlag': 0,'XMP:SelfData': 'Undefined','XMP:Version': 7.0

任何建议:

  1. 如何编辑图像并保持相同的元数据?
  2. 如何从原始图像中编辑特定的标签

解决方法

关于2)-只需为此使用PIL.Image


TAG_ID = 271

if hasattr(image,'_getexif'):  # only present in JPEGs
    exif = image._getexif()  # returns None if no EXIF data
    if exif is not None:
        try:
            orientation = exif[TAG_ID]
        except KeyError:
            pass
        else:
            exif[TAG_ID] = 'Canon'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?