Coverage for /usr/lib/python3/dist-packages/PIL/TiffTags.py: 89%
47 statements
« prev ^ index » next coverage.py v7.4.4, created at 2025-06-14 15:25 +0200
« prev ^ index » next coverage.py v7.4.4, created at 2025-06-14 15:25 +0200
1#
2# The Python Imaging Library.
3# $Id$
4#
5# TIFF tags
6#
7# This module provides clear-text names for various well-known
8# TIFF tags. the TIFF codec works just fine without it.
9#
10# Copyright (c) Secret Labs AB 1999.
11#
12# See the README file for information on usage and redistribution.
13#
15##
16# This module provides constants and clear-text names for various
17# well-known TIFF tags.
18##
19from __future__ import annotations
21from collections import namedtuple
24class TagInfo(namedtuple("_TagInfo", "value name type length enum")):
25 __slots__ = []
27 def __new__(cls, value=None, name="unknown", type=None, length=None, enum=None):
28 return super().__new__(cls, value, name, type, length, enum or {})
30 def cvt_enum(self, value):
31 # Using get will call hash(value), which can be expensive
32 # for some types (e.g. Fraction). Since self.enum is rarely
33 # used, it's usually better to test it first.
34 return self.enum.get(value, value) if self.enum else value
37def lookup(tag, group=None):
38 """
39 :param tag: Integer tag number
40 :param group: Which :py:data:`~PIL.TiffTags.TAGS_V2_GROUPS` to look in
42 .. versionadded:: 8.3.0
44 :returns: Taginfo namedtuple, From the ``TAGS_V2`` info if possible,
45 otherwise just populating the value and name from ``TAGS``.
46 If the tag is not recognized, "unknown" is returned for the name
48 """
50 if group is not None:
51 info = TAGS_V2_GROUPS[group].get(tag) if group in TAGS_V2_GROUPS else None
52 else:
53 info = TAGS_V2.get(tag)
54 return info or TagInfo(tag, TAGS.get(tag, "unknown"))
57##
58# Map tag numbers to tag info.
59#
60# id: (Name, Type, Length[, enum_values])
61#
62# The length here differs from the length in the tiff spec. For
63# numbers, the tiff spec is for the number of fields returned. We
64# agree here. For string-like types, the tiff spec uses the length of
65# field in bytes. In Pillow, we are using the number of expected
66# fields, in general 1 for string-like types.
69BYTE = 1
70ASCII = 2
71SHORT = 3
72LONG = 4
73RATIONAL = 5
74SIGNED_BYTE = 6
75UNDEFINED = 7
76SIGNED_SHORT = 8
77SIGNED_LONG = 9
78SIGNED_RATIONAL = 10
79FLOAT = 11
80DOUBLE = 12
81IFD = 13
82LONG8 = 16
84TAGS_V2 = {
85 254: ("NewSubfileType", LONG, 1),
86 255: ("SubfileType", SHORT, 1),
87 256: ("ImageWidth", LONG, 1),
88 257: ("ImageLength", LONG, 1),
89 258: ("BitsPerSample", SHORT, 0),
90 259: (
91 "Compression",
92 SHORT,
93 1,
94 {
95 "Uncompressed": 1,
96 "CCITT 1d": 2,
97 "Group 3 Fax": 3,
98 "Group 4 Fax": 4,
99 "LZW": 5,
100 "JPEG": 6,
101 "PackBits": 32773,
102 },
103 ),
104 262: (
105 "PhotometricInterpretation",
106 SHORT,
107 1,
108 {
109 "WhiteIsZero": 0,
110 "BlackIsZero": 1,
111 "RGB": 2,
112 "RGB Palette": 3,
113 "Transparency Mask": 4,
114 "CMYK": 5,
115 "YCbCr": 6,
116 "CieLAB": 8,
117 "CFA": 32803, # TIFF/EP, Adobe DNG
118 "LinearRaw": 32892, # Adobe DNG
119 },
120 ),
121 263: ("Threshholding", SHORT, 1),
122 264: ("CellWidth", SHORT, 1),
123 265: ("CellLength", SHORT, 1),
124 266: ("FillOrder", SHORT, 1),
125 269: ("DocumentName", ASCII, 1),
126 270: ("ImageDescription", ASCII, 1),
127 271: ("Make", ASCII, 1),
128 272: ("Model", ASCII, 1),
129 273: ("StripOffsets", LONG, 0),
130 274: ("Orientation", SHORT, 1),
131 277: ("SamplesPerPixel", SHORT, 1),
132 278: ("RowsPerStrip", LONG, 1),
133 279: ("StripByteCounts", LONG, 0),
134 280: ("MinSampleValue", SHORT, 0),
135 281: ("MaxSampleValue", SHORT, 0),
136 282: ("XResolution", RATIONAL, 1),
137 283: ("YResolution", RATIONAL, 1),
138 284: ("PlanarConfiguration", SHORT, 1, {"Contiguous": 1, "Separate": 2}),
139 285: ("PageName", ASCII, 1),
140 286: ("XPosition", RATIONAL, 1),
141 287: ("YPosition", RATIONAL, 1),
142 288: ("FreeOffsets", LONG, 1),
143 289: ("FreeByteCounts", LONG, 1),
144 290: ("GrayResponseUnit", SHORT, 1),
145 291: ("GrayResponseCurve", SHORT, 0),
146 292: ("T4Options", LONG, 1),
147 293: ("T6Options", LONG, 1),
148 296: ("ResolutionUnit", SHORT, 1, {"none": 1, "inch": 2, "cm": 3}),
149 297: ("PageNumber", SHORT, 2),
150 301: ("TransferFunction", SHORT, 0),
151 305: ("Software", ASCII, 1),
152 306: ("DateTime", ASCII, 1),
153 315: ("Artist", ASCII, 1),
154 316: ("HostComputer", ASCII, 1),
155 317: ("Predictor", SHORT, 1, {"none": 1, "Horizontal Differencing": 2}),
156 318: ("WhitePoint", RATIONAL, 2),
157 319: ("PrimaryChromaticities", RATIONAL, 6),
158 320: ("ColorMap", SHORT, 0),
159 321: ("HalftoneHints", SHORT, 2),
160 322: ("TileWidth", LONG, 1),
161 323: ("TileLength", LONG, 1),
162 324: ("TileOffsets", LONG, 0),
163 325: ("TileByteCounts", LONG, 0),
164 330: ("SubIFDs", LONG, 0),
165 332: ("InkSet", SHORT, 1),
166 333: ("InkNames", ASCII, 1),
167 334: ("NumberOfInks", SHORT, 1),
168 336: ("DotRange", SHORT, 0),
169 337: ("TargetPrinter", ASCII, 1),
170 338: ("ExtraSamples", SHORT, 0),
171 339: ("SampleFormat", SHORT, 0),
172 340: ("SMinSampleValue", DOUBLE, 0),
173 341: ("SMaxSampleValue", DOUBLE, 0),
174 342: ("TransferRange", SHORT, 6),
175 347: ("JPEGTables", UNDEFINED, 1),
176 # obsolete JPEG tags
177 512: ("JPEGProc", SHORT, 1),
178 513: ("JPEGInterchangeFormat", LONG, 1),
179 514: ("JPEGInterchangeFormatLength", LONG, 1),
180 515: ("JPEGRestartInterval", SHORT, 1),
181 517: ("JPEGLosslessPredictors", SHORT, 0),
182 518: ("JPEGPointTransforms", SHORT, 0),
183 519: ("JPEGQTables", LONG, 0),
184 520: ("JPEGDCTables", LONG, 0),
185 521: ("JPEGACTables", LONG, 0),
186 529: ("YCbCrCoefficients", RATIONAL, 3),
187 530: ("YCbCrSubSampling", SHORT, 2),
188 531: ("YCbCrPositioning", SHORT, 1),
189 532: ("ReferenceBlackWhite", RATIONAL, 6),
190 700: ("XMP", BYTE, 0),
191 33432: ("Copyright", ASCII, 1),
192 33723: ("IptcNaaInfo", UNDEFINED, 1),
193 34377: ("PhotoshopInfo", BYTE, 0),
194 # FIXME add more tags here
195 34665: ("ExifIFD", LONG, 1),
196 34675: ("ICCProfile", UNDEFINED, 1),
197 34853: ("GPSInfoIFD", LONG, 1),
198 36864: ("ExifVersion", UNDEFINED, 1),
199 37724: ("ImageSourceData", UNDEFINED, 1),
200 40965: ("InteroperabilityIFD", LONG, 1),
201 41730: ("CFAPattern", UNDEFINED, 1),
202 # MPInfo
203 45056: ("MPFVersion", UNDEFINED, 1),
204 45057: ("NumberOfImages", LONG, 1),
205 45058: ("MPEntry", UNDEFINED, 1),
206 45059: ("ImageUIDList", UNDEFINED, 0), # UNDONE, check
207 45060: ("TotalFrames", LONG, 1),
208 45313: ("MPIndividualNum", LONG, 1),
209 45569: ("PanOrientation", LONG, 1),
210 45570: ("PanOverlap_H", RATIONAL, 1),
211 45571: ("PanOverlap_V", RATIONAL, 1),
212 45572: ("BaseViewpointNum", LONG, 1),
213 45573: ("ConvergenceAngle", SIGNED_RATIONAL, 1),
214 45574: ("BaselineLength", RATIONAL, 1),
215 45575: ("VerticalDivergence", SIGNED_RATIONAL, 1),
216 45576: ("AxisDistance_X", SIGNED_RATIONAL, 1),
217 45577: ("AxisDistance_Y", SIGNED_RATIONAL, 1),
218 45578: ("AxisDistance_Z", SIGNED_RATIONAL, 1),
219 45579: ("YawAngle", SIGNED_RATIONAL, 1),
220 45580: ("PitchAngle", SIGNED_RATIONAL, 1),
221 45581: ("RollAngle", SIGNED_RATIONAL, 1),
222 40960: ("FlashPixVersion", UNDEFINED, 1),
223 50741: ("MakerNoteSafety", SHORT, 1, {"Unsafe": 0, "Safe": 1}),
224 50780: ("BestQualityScale", RATIONAL, 1),
225 50838: ("ImageJMetaDataByteCounts", LONG, 0), # Can be more than one
226 50839: ("ImageJMetaData", UNDEFINED, 1), # see Issue #2006
227}
228TAGS_V2_GROUPS = {
229 # ExifIFD
230 34665: {
231 36864: ("ExifVersion", UNDEFINED, 1),
232 40960: ("FlashPixVersion", UNDEFINED, 1),
233 40965: ("InteroperabilityIFD", LONG, 1),
234 41730: ("CFAPattern", UNDEFINED, 1),
235 },
236 # GPSInfoIFD
237 34853: {
238 0: ("GPSVersionID", BYTE, 4),
239 1: ("GPSLatitudeRef", ASCII, 2),
240 2: ("GPSLatitude", RATIONAL, 3),
241 3: ("GPSLongitudeRef", ASCII, 2),
242 4: ("GPSLongitude", RATIONAL, 3),
243 5: ("GPSAltitudeRef", BYTE, 1),
244 6: ("GPSAltitude", RATIONAL, 1),
245 7: ("GPSTimeStamp", RATIONAL, 3),
246 8: ("GPSSatellites", ASCII, 0),
247 9: ("GPSStatus", ASCII, 2),
248 10: ("GPSMeasureMode", ASCII, 2),
249 11: ("GPSDOP", RATIONAL, 1),
250 12: ("GPSSpeedRef", ASCII, 2),
251 13: ("GPSSpeed", RATIONAL, 1),
252 14: ("GPSTrackRef", ASCII, 2),
253 15: ("GPSTrack", RATIONAL, 1),
254 16: ("GPSImgDirectionRef", ASCII, 2),
255 17: ("GPSImgDirection", RATIONAL, 1),
256 18: ("GPSMapDatum", ASCII, 0),
257 19: ("GPSDestLatitudeRef", ASCII, 2),
258 20: ("GPSDestLatitude", RATIONAL, 3),
259 21: ("GPSDestLongitudeRef", ASCII, 2),
260 22: ("GPSDestLongitude", RATIONAL, 3),
261 23: ("GPSDestBearingRef", ASCII, 2),
262 24: ("GPSDestBearing", RATIONAL, 1),
263 25: ("GPSDestDistanceRef", ASCII, 2),
264 26: ("GPSDestDistance", RATIONAL, 1),
265 27: ("GPSProcessingMethod", UNDEFINED, 0),
266 28: ("GPSAreaInformation", UNDEFINED, 0),
267 29: ("GPSDateStamp", ASCII, 11),
268 30: ("GPSDifferential", SHORT, 1),
269 },
270 # InteroperabilityIFD
271 40965: {1: ("InteropIndex", ASCII, 1), 2: ("InteropVersion", UNDEFINED, 1)},
272}
274# Legacy Tags structure
275# these tags aren't included above, but were in the previous versions
276TAGS = {
277 347: "JPEGTables",
278 700: "XMP",
279 # Additional Exif Info
280 32932: "Wang Annotation",
281 33434: "ExposureTime",
282 33437: "FNumber",
283 33445: "MD FileTag",
284 33446: "MD ScalePixel",
285 33447: "MD ColorTable",
286 33448: "MD LabName",
287 33449: "MD SampleInfo",
288 33450: "MD PrepDate",
289 33451: "MD PrepTime",
290 33452: "MD FileUnits",
291 33550: "ModelPixelScaleTag",
292 33723: "IptcNaaInfo",
293 33918: "INGR Packet Data Tag",
294 33919: "INGR Flag Registers",
295 33920: "IrasB Transformation Matrix",
296 33922: "ModelTiepointTag",
297 34264: "ModelTransformationTag",
298 34377: "PhotoshopInfo",
299 34735: "GeoKeyDirectoryTag",
300 34736: "GeoDoubleParamsTag",
301 34737: "GeoAsciiParamsTag",
302 34850: "ExposureProgram",
303 34852: "SpectralSensitivity",
304 34855: "ISOSpeedRatings",
305 34856: "OECF",
306 34864: "SensitivityType",
307 34865: "StandardOutputSensitivity",
308 34866: "RecommendedExposureIndex",
309 34867: "ISOSpeed",
310 34868: "ISOSpeedLatitudeyyy",
311 34869: "ISOSpeedLatitudezzz",
312 34908: "HylaFAX FaxRecvParams",
313 34909: "HylaFAX FaxSubAddress",
314 34910: "HylaFAX FaxRecvTime",
315 36864: "ExifVersion",
316 36867: "DateTimeOriginal",
317 36868: "DateTimeDigitized",
318 37121: "ComponentsConfiguration",
319 37122: "CompressedBitsPerPixel",
320 37724: "ImageSourceData",
321 37377: "ShutterSpeedValue",
322 37378: "ApertureValue",
323 37379: "BrightnessValue",
324 37380: "ExposureBiasValue",
325 37381: "MaxApertureValue",
326 37382: "SubjectDistance",
327 37383: "MeteringMode",
328 37384: "LightSource",
329 37385: "Flash",
330 37386: "FocalLength",
331 37396: "SubjectArea",
332 37500: "MakerNote",
333 37510: "UserComment",
334 37520: "SubSec",
335 37521: "SubSecTimeOriginal",
336 37522: "SubsecTimeDigitized",
337 40960: "FlashPixVersion",
338 40961: "ColorSpace",
339 40962: "PixelXDimension",
340 40963: "PixelYDimension",
341 40964: "RelatedSoundFile",
342 40965: "InteroperabilityIFD",
343 41483: "FlashEnergy",
344 41484: "SpatialFrequencyResponse",
345 41486: "FocalPlaneXResolution",
346 41487: "FocalPlaneYResolution",
347 41488: "FocalPlaneResolutionUnit",
348 41492: "SubjectLocation",
349 41493: "ExposureIndex",
350 41495: "SensingMethod",
351 41728: "FileSource",
352 41729: "SceneType",
353 41730: "CFAPattern",
354 41985: "CustomRendered",
355 41986: "ExposureMode",
356 41987: "WhiteBalance",
357 41988: "DigitalZoomRatio",
358 41989: "FocalLengthIn35mmFilm",
359 41990: "SceneCaptureType",
360 41991: "GainControl",
361 41992: "Contrast",
362 41993: "Saturation",
363 41994: "Sharpness",
364 41995: "DeviceSettingDescription",
365 41996: "SubjectDistanceRange",
366 42016: "ImageUniqueID",
367 42032: "CameraOwnerName",
368 42033: "BodySerialNumber",
369 42034: "LensSpecification",
370 42035: "LensMake",
371 42036: "LensModel",
372 42037: "LensSerialNumber",
373 42112: "GDAL_METADATA",
374 42113: "GDAL_NODATA",
375 42240: "Gamma",
376 50215: "Oce Scanjob Description",
377 50216: "Oce Application Selector",
378 50217: "Oce Identification Number",
379 50218: "Oce ImageLogic Characteristics",
380 # Adobe DNG
381 50706: "DNGVersion",
382 50707: "DNGBackwardVersion",
383 50708: "UniqueCameraModel",
384 50709: "LocalizedCameraModel",
385 50710: "CFAPlaneColor",
386 50711: "CFALayout",
387 50712: "LinearizationTable",
388 50713: "BlackLevelRepeatDim",
389 50714: "BlackLevel",
390 50715: "BlackLevelDeltaH",
391 50716: "BlackLevelDeltaV",
392 50717: "WhiteLevel",
393 50718: "DefaultScale",
394 50719: "DefaultCropOrigin",
395 50720: "DefaultCropSize",
396 50721: "ColorMatrix1",
397 50722: "ColorMatrix2",
398 50723: "CameraCalibration1",
399 50724: "CameraCalibration2",
400 50725: "ReductionMatrix1",
401 50726: "ReductionMatrix2",
402 50727: "AnalogBalance",
403 50728: "AsShotNeutral",
404 50729: "AsShotWhiteXY",
405 50730: "BaselineExposure",
406 50731: "BaselineNoise",
407 50732: "BaselineSharpness",
408 50733: "BayerGreenSplit",
409 50734: "LinearResponseLimit",
410 50735: "CameraSerialNumber",
411 50736: "LensInfo",
412 50737: "ChromaBlurRadius",
413 50738: "AntiAliasStrength",
414 50740: "DNGPrivateData",
415 50778: "CalibrationIlluminant1",
416 50779: "CalibrationIlluminant2",
417 50784: "Alias Layer Metadata",
418}
421def _populate():
422 for k, v in TAGS_V2.items():
423 # Populate legacy structure.
424 TAGS[k] = v[0]
425 if len(v) == 4:
426 for sk, sv in v[3].items():
427 TAGS[(k, sv)] = sk
429 TAGS_V2[k] = TagInfo(k, *v)
431 for tags in TAGS_V2_GROUPS.values():
432 for k, v in tags.items():
433 tags[k] = TagInfo(k, *v)
436_populate()
437##
438# Map type numbers to type names -- defined in ImageFileDirectory.
440TYPES = {}
442#
443# These tags are handled by default in libtiff, without
444# adding to the custom dictionary. From tif_dir.c, searching for
445# case TIFFTAG in the _TIFFVSetField function:
446# Line: item.
447# 148: case TIFFTAG_SUBFILETYPE:
448# 151: case TIFFTAG_IMAGEWIDTH:
449# 154: case TIFFTAG_IMAGELENGTH:
450# 157: case TIFFTAG_BITSPERSAMPLE:
451# 181: case TIFFTAG_COMPRESSION:
452# 202: case TIFFTAG_PHOTOMETRIC:
453# 205: case TIFFTAG_THRESHHOLDING:
454# 208: case TIFFTAG_FILLORDER:
455# 214: case TIFFTAG_ORIENTATION:
456# 221: case TIFFTAG_SAMPLESPERPIXEL:
457# 228: case TIFFTAG_ROWSPERSTRIP:
458# 238: case TIFFTAG_MINSAMPLEVALUE:
459# 241: case TIFFTAG_MAXSAMPLEVALUE:
460# 244: case TIFFTAG_SMINSAMPLEVALUE:
461# 247: case TIFFTAG_SMAXSAMPLEVALUE:
462# 250: case TIFFTAG_XRESOLUTION:
463# 256: case TIFFTAG_YRESOLUTION:
464# 262: case TIFFTAG_PLANARCONFIG:
465# 268: case TIFFTAG_XPOSITION:
466# 271: case TIFFTAG_YPOSITION:
467# 274: case TIFFTAG_RESOLUTIONUNIT:
468# 280: case TIFFTAG_PAGENUMBER:
469# 284: case TIFFTAG_HALFTONEHINTS:
470# 288: case TIFFTAG_COLORMAP:
471# 294: case TIFFTAG_EXTRASAMPLES:
472# 298: case TIFFTAG_MATTEING:
473# 305: case TIFFTAG_TILEWIDTH:
474# 316: case TIFFTAG_TILELENGTH:
475# 327: case TIFFTAG_TILEDEPTH:
476# 333: case TIFFTAG_DATATYPE:
477# 344: case TIFFTAG_SAMPLEFORMAT:
478# 361: case TIFFTAG_IMAGEDEPTH:
479# 364: case TIFFTAG_SUBIFD:
480# 376: case TIFFTAG_YCBCRPOSITIONING:
481# 379: case TIFFTAG_YCBCRSUBSAMPLING:
482# 383: case TIFFTAG_TRANSFERFUNCTION:
483# 389: case TIFFTAG_REFERENCEBLACKWHITE:
484# 393: case TIFFTAG_INKNAMES:
486# Following pseudo-tags are also handled by default in libtiff:
487# TIFFTAG_JPEGQUALITY 65537
489# some of these are not in our TAGS_V2 dict and were included from tiff.h
491# This list also exists in encode.c
492LIBTIFF_CORE = {
493 255,
494 256,
495 257,
496 258,
497 259,
498 262,
499 263,
500 266,
501 274,
502 277,
503 278,
504 280,
505 281,
506 340,
507 341,
508 282,
509 283,
510 284,
511 286,
512 287,
513 296,
514 297,
515 321,
516 320,
517 338,
518 32995,
519 322,
520 323,
521 32998,
522 32996,
523 339,
524 32997,
525 330,
526 531,
527 530,
528 301,
529 532,
530 333,
531 # as above
532 269, # this has been in our tests forever, and works
533 65537,
534}
536LIBTIFF_CORE.remove(255) # We don't have support for subfiletypes
537LIBTIFF_CORE.remove(322) # We don't have support for writing tiled images with libtiff
538LIBTIFF_CORE.remove(323) # Tiled images
539LIBTIFF_CORE.remove(333) # Ink Names either
541# Note to advanced users: There may be combinations of these
542# parameters and values that when added properly, will work and
543# produce valid tiff images that may work in your application.
544# It is safe to add and remove tags from this set from Pillow's point
545# of view so long as you test against libtiff.