TiffTags.py 14 KB

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