简体   繁体   中英

How can I access and change the tag description property of a JPEG image in the Windows 7 file explorer through the use of Python?

I am having a difficult time understanding how I can change the Tags of a JPEG file through Python. The "Tags" I'm referring to are those under the description properties of any arbitrary file, where comments, subjects, and ratings would also be found. I am uncertain whether or not there is a .os function similar to the filename rename function which can do this, or if this kind of operation of changing the tags would fall under the category of JPEG Metadata/EXIFDATA manipulation.

Say, for example, I want to add 3 tags to a JPEG image through Python, those being the tags BLUE, SQUARE, and 9. Manually I would go to the file, access the description properties either by right-clicking the file to view its tags, or clicking the bottom bar menu, and click tags to then input BLUE, SQUARE, and 9 into the text box, with a semicolon after each individual tag. With Python, I would need a script that could inject those individual tags into the description properties of the JPEG to make the process less painful if I were to do this iteratively for multiple JPEG files.

I doubt this would require any heavy-handed knowledge of programing with Python, but at the same time the specificity of my issue seems to elude an easy resolution. I can't seem to find a semi-clear or straightforward answer, as I'm a bit of a greenhorn when it comes to programing with python in general.

Any help would be very, very useful. I am using Python 3.5.2. by the way.

So, here is my revised answer. So, all info in a jpg is metadata stored with an "id" number so for instance 0th is the discription area you are looking for heres a table.

0th:
283 = (720000, 10000)
296 = 2
34665 = 11444
306 = b'2011:08:22 21:39:05'
270 = b''
271 = b'OLYMPUS IMAGING CORP.'
272 = b'E-P1'
305 = b'Adobe Photoshop CS5 Windows'
274 = 1
33432 = b'Robin Parmar'
282 = (720000, 10000)
315 = b'Robin Parmar'

source http://www.theatreofnoise.com/2016/10/how-to-retrieve-photo-metadata-in-python.html

So to change like the subject all you need to do is...

pip install piexif

This works with 2.7 & 3

import piexif

def insert_sample():
    zeroth_ifd = {270: "test"}
    exif_bytes = piexif.dump({"0th":zeroth_ifd})
    piexif.insert(exif_bytes,'img.jpg')

insert_sample() 

Hope this helps :D Heres a list of all the ids

<< Inspect piexif >>

11 = ProcessingSoftware
18246 = Rating
18249 = RatingPercent
254 = NewSubfileType
255 = SubfileType
256 = ImageWidth
257 = ImageLength
258 = BitsPerSample
259 = Compression
262 = PhotometricInterpretation
263 = Threshholding
264 = CellWidth
265 = CellLength
266 = FillOrder
269 = DocumentName
270 = ImageDescription
271 = Make
272 = Model
273 = StripOffsets
274 = Orientation
277 = SamplesPerPixel
278 = RowsPerStrip
279 = StripByteCounts
282 = XResolution
283 = YResolution
284 = PlanarConfiguration
290 = GrayResponseUnit
291 = GrayResponseCurve
292 = T4Options
293 = T6Options
296 = ResolutionUnit
301 = TransferFunction
305 = Software
306 = DateTime
315 = Artist
316 = HostComputer
317 = Predictor
318 = WhitePoint
319 = PrimaryChromaticities
320 = ColorMap
321 = HalftoneHints
322 = TileWidth
323 = TileLength
324 = TileOffsets
325 = TileByteCounts
32781 = ImageID
330 = SubIFDs
332 = InkSet
333 = InkNames
334 = NumberOfInks
33421 = CFARepeatPatternDim
33422 = CFAPattern
33423 = BatteryLevel
33432 = Copyright
33434 = ExposureTime
336 = DotRange
337 = TargetPrinter
338 = ExtraSamples
339 = SampleFormat
340 = SMinSampleValue
341 = SMaxSampleValue
342 = TransferRange
343 = ClipPath
34377 = ImageResources
344 = XClipPathUnits
345 = YClipPathUnits
346 = Indexed
34665 = ExifTag
34675 = InterColorProfile
347 = JPEGTables
34853 = GPSTag
34857 = Interlace
34858 = TimeZoneOffset
34859 = SelfTimerMode
351 = OPIProxy
37387 = FlashEnergy
37388 = SpatialFrequencyResponse
37389 = Noise
37390 = FocalPlaneXResolution
37391 = FocalPlaneYResolution
37392 = FocalPlaneResolutionUnit
37393 = ImageNumber
37394 = SecurityClassification
37395 = ImageHistory
37397 = ExposureIndex
37398 = TIFFEPStandardID
37399 = SensingMethod
40091 = XPTitle
40092 = XPComment
40093 = XPAuthor
40094 = XPKeywords
40095 = XPSubject
50341 = PrintImageMatching
50706 = DNGVersion
50707 = DNGBackwardVersion
50708 = UniqueCameraModel
50709 = LocalizedCameraModel
50710 = CFAPlaneColor
50711 = CFALayout
50712 = LinearizationTable
50713 = BlackLevelRepeatDim
50714 = BlackLevel
50715 = BlackLevelDeltaH
50716 = BlackLevelDeltaV
50717 = WhiteLevel
50718 = DefaultScale
50719 = DefaultCropOrigin
50720 = DefaultCropSize
50721 = ColorMatrix1
50722 = ColorMatrix2
50723 = CameraCalibration1
50724 = CameraCalibration2
50725 = ReductionMatrix1
50726 = ReductionMatrix2
50727 = AnalogBalance
50728 = AsShotNeutral
50729 = AsShotWhiteXY
50730 = BaselineExposure
50731 = BaselineNoise
50732 = BaselineSharpness
50733 = BayerGreenSplit
50734 = LinearResponseLimit
50735 = CameraSerialNumber
50736 = LensInfo
50737 = ChromaBlurRadius
50738 = AntiAliasStrength
50739 = ShadowScale
50740 = DNGPrivateData
50741 = MakerNoteSafety
50778 = CalibrationIlluminant1
50779 = CalibrationIlluminant2
50780 = BestQualityScale
50781 = RawDataUniqueID
50827 = OriginalRawFileName
50828 = OriginalRawFileData
50829 = ActiveArea
50830 = MaskedAreas
50831 = AsShotICCProfile
50832 = AsShotPreProfileMatrix
50833 = CurrentICCProfile
50834 = CurrentPreProfileMatrix
50879 = ColorimetricReference
50931 = CameraCalibrationSignature
50932 = ProfileCalibrationSignature
50934 = AsShotProfileName
50935 = NoiseReductionApplied
50936 = ProfileName
50937 = ProfileHueSatMapDims
50938 = ProfileHueSatMapData1
50939 = ProfileHueSatMapData2
50940 = ProfileToneCurve
50941 = ProfileEmbedPolicy
50942 = ProfileCopyright
50964 = ForwardMatrix1
50965 = ForwardMatrix2
50966 = PreviewApplicationName
50967 = PreviewApplicationVersion
50968 = PreviewSettingsName
50969 = PreviewSettingsDigest
50970 = PreviewColorSpace
50971 = PreviewDateTime
50972 = RawImageDigest
50973 = OriginalRawFileDigest
50974 = SubTileBlockSize
50975 = RowInterleaveFactor
50981 = ProfileLookTableDims
50982 = ProfileLookTableData
51008 = OpcodeList1
51009 = OpcodeList2
51022 = OpcodeList3
51041 = NoiseProfile
512 = JPEGProc
513 = JPEGInterchangeFormat
514 = JPEGInterchangeFormatLength
515 = JPEGRestartInterval
517 = JPEGLosslessPredictors
518 = JPEGPointTransforms
519 = JPEGQTables
520 = JPEGDCTables
521 = JPEGACTables
529 = YCbCrCoefficients
530 = YCbCrSubSampling
531 = YCbCrPositioning
532 = ReferenceBlackWhite
60606 = ZZZTestSlong1
60607 = ZZZTestSlong2
700 = XMLPacket

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM