TiffImagePlugin.py 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # TIFF file handling
  6. #
  7. # TIFF is a flexible, if somewhat aged, image file format originally
  8. # defined by Aldus. Although TIFF supports a wide variety of pixel
  9. # layouts and compression methods, the name doesn't really stand for
  10. # "thousands of incompatible file formats," it just feels that way.
  11. #
  12. # To read TIFF data from a stream, the stream must be seekable. For
  13. # progressive decoding, make sure to use TIFF files where the tag
  14. # directory is placed first in the file.
  15. #
  16. # History:
  17. # 1995-09-01 fl Created
  18. # 1996-05-04 fl Handle JPEGTABLES tag
  19. # 1996-05-18 fl Fixed COLORMAP support
  20. # 1997-01-05 fl Fixed PREDICTOR support
  21. # 1997-08-27 fl Added support for rational tags (from Perry Stoll)
  22. # 1998-01-10 fl Fixed seek/tell (from Jan Blom)
  23. # 1998-07-15 fl Use private names for internal variables
  24. # 1999-06-13 fl Rewritten for PIL 1.0 (1.0)
  25. # 2000-10-11 fl Additional fixes for Python 2.0 (1.1)
  26. # 2001-04-17 fl Fixed rewind support (seek to frame 0) (1.2)
  27. # 2001-05-12 fl Added write support for more tags (from Greg Couch) (1.3)
  28. # 2001-12-18 fl Added workaround for broken Matrox library
  29. # 2002-01-18 fl Don't mess up if photometric tag is missing (D. Alan Stewart)
  30. # 2003-05-19 fl Check FILLORDER tag
  31. # 2003-09-26 fl Added RGBa support
  32. # 2004-02-24 fl Added DPI support; fixed rational write support
  33. # 2005-02-07 fl Added workaround for broken Corel Draw 10 files
  34. # 2006-01-09 fl Added support for float/double tags (from Russell Nelson)
  35. #
  36. # Copyright (c) 1997-2006 by Secret Labs AB. All rights reserved.
  37. # Copyright (c) 1995-1997 by Fredrik Lundh
  38. #
  39. # See the README file for information on usage and redistribution.
  40. #
  41. import io
  42. import itertools
  43. import logging
  44. import math
  45. import os
  46. import struct
  47. import warnings
  48. from collections.abc import MutableMapping
  49. from fractions import Fraction
  50. from numbers import Number, Rational
  51. from . import ExifTags, Image, ImageFile, ImageOps, ImagePalette, TiffTags
  52. from ._binary import i16be as i16
  53. from ._binary import i32be as i32
  54. from ._binary import o8
  55. from .TiffTags import TYPES
  56. logger = logging.getLogger(__name__)
  57. # Set these to true to force use of libtiff for reading or writing.
  58. READ_LIBTIFF = False
  59. WRITE_LIBTIFF = False
  60. IFD_LEGACY_API = True
  61. STRIP_SIZE = 65536
  62. II = b"II" # little-endian (Intel style)
  63. MM = b"MM" # big-endian (Motorola style)
  64. #
  65. # --------------------------------------------------------------------
  66. # Read TIFF files
  67. # a few tag names, just to make the code below a bit more readable
  68. IMAGEWIDTH = 256
  69. IMAGELENGTH = 257
  70. BITSPERSAMPLE = 258
  71. COMPRESSION = 259
  72. PHOTOMETRIC_INTERPRETATION = 262
  73. FILLORDER = 266
  74. IMAGEDESCRIPTION = 270
  75. STRIPOFFSETS = 273
  76. SAMPLESPERPIXEL = 277
  77. ROWSPERSTRIP = 278
  78. STRIPBYTECOUNTS = 279
  79. X_RESOLUTION = 282
  80. Y_RESOLUTION = 283
  81. PLANAR_CONFIGURATION = 284
  82. RESOLUTION_UNIT = 296
  83. TRANSFERFUNCTION = 301
  84. SOFTWARE = 305
  85. DATE_TIME = 306
  86. ARTIST = 315
  87. PREDICTOR = 317
  88. COLORMAP = 320
  89. TILEWIDTH = 322
  90. TILELENGTH = 323
  91. TILEOFFSETS = 324
  92. TILEBYTECOUNTS = 325
  93. SUBIFD = 330
  94. EXTRASAMPLES = 338
  95. SAMPLEFORMAT = 339
  96. JPEGTABLES = 347
  97. YCBCRSUBSAMPLING = 530
  98. REFERENCEBLACKWHITE = 532
  99. COPYRIGHT = 33432
  100. IPTC_NAA_CHUNK = 33723 # newsphoto properties
  101. PHOTOSHOP_CHUNK = 34377 # photoshop properties
  102. ICCPROFILE = 34675
  103. EXIFIFD = 34665
  104. XMP = 700
  105. JPEGQUALITY = 65537 # pseudo-tag by libtiff
  106. # https://github.com/imagej/ImageJA/blob/master/src/main/java/ij/io/TiffDecoder.java
  107. IMAGEJ_META_DATA_BYTE_COUNTS = 50838
  108. IMAGEJ_META_DATA = 50839
  109. COMPRESSION_INFO = {
  110. # Compression => pil compression name
  111. 1: "raw",
  112. 2: "tiff_ccitt",
  113. 3: "group3",
  114. 4: "group4",
  115. 5: "tiff_lzw",
  116. 6: "tiff_jpeg", # obsolete
  117. 7: "jpeg",
  118. 8: "tiff_adobe_deflate",
  119. 32771: "tiff_raw_16", # 16-bit padding
  120. 32773: "packbits",
  121. 32809: "tiff_thunderscan",
  122. 32946: "tiff_deflate",
  123. 34676: "tiff_sgilog",
  124. 34677: "tiff_sgilog24",
  125. 34925: "lzma",
  126. 50000: "zstd",
  127. 50001: "webp",
  128. }
  129. COMPRESSION_INFO_REV = {v: k for k, v in COMPRESSION_INFO.items()}
  130. OPEN_INFO = {
  131. # (ByteOrder, PhotoInterpretation, SampleFormat, FillOrder, BitsPerSample,
  132. # ExtraSamples) => mode, rawmode
  133. (II, 0, (1,), 1, (1,), ()): ("1", "1;I"),
  134. (MM, 0, (1,), 1, (1,), ()): ("1", "1;I"),
  135. (II, 0, (1,), 2, (1,), ()): ("1", "1;IR"),
  136. (MM, 0, (1,), 2, (1,), ()): ("1", "1;IR"),
  137. (II, 1, (1,), 1, (1,), ()): ("1", "1"),
  138. (MM, 1, (1,), 1, (1,), ()): ("1", "1"),
  139. (II, 1, (1,), 2, (1,), ()): ("1", "1;R"),
  140. (MM, 1, (1,), 2, (1,), ()): ("1", "1;R"),
  141. (II, 0, (1,), 1, (2,), ()): ("L", "L;2I"),
  142. (MM, 0, (1,), 1, (2,), ()): ("L", "L;2I"),
  143. (II, 0, (1,), 2, (2,), ()): ("L", "L;2IR"),
  144. (MM, 0, (1,), 2, (2,), ()): ("L", "L;2IR"),
  145. (II, 1, (1,), 1, (2,), ()): ("L", "L;2"),
  146. (MM, 1, (1,), 1, (2,), ()): ("L", "L;2"),
  147. (II, 1, (1,), 2, (2,), ()): ("L", "L;2R"),
  148. (MM, 1, (1,), 2, (2,), ()): ("L", "L;2R"),
  149. (II, 0, (1,), 1, (4,), ()): ("L", "L;4I"),
  150. (MM, 0, (1,), 1, (4,), ()): ("L", "L;4I"),
  151. (II, 0, (1,), 2, (4,), ()): ("L", "L;4IR"),
  152. (MM, 0, (1,), 2, (4,), ()): ("L", "L;4IR"),
  153. (II, 1, (1,), 1, (4,), ()): ("L", "L;4"),
  154. (MM, 1, (1,), 1, (4,), ()): ("L", "L;4"),
  155. (II, 1, (1,), 2, (4,), ()): ("L", "L;4R"),
  156. (MM, 1, (1,), 2, (4,), ()): ("L", "L;4R"),
  157. (II, 0, (1,), 1, (8,), ()): ("L", "L;I"),
  158. (MM, 0, (1,), 1, (8,), ()): ("L", "L;I"),
  159. (II, 0, (1,), 2, (8,), ()): ("L", "L;IR"),
  160. (MM, 0, (1,), 2, (8,), ()): ("L", "L;IR"),
  161. (II, 1, (1,), 1, (8,), ()): ("L", "L"),
  162. (MM, 1, (1,), 1, (8,), ()): ("L", "L"),
  163. (II, 1, (2,), 1, (8,), ()): ("L", "L"),
  164. (MM, 1, (2,), 1, (8,), ()): ("L", "L"),
  165. (II, 1, (1,), 2, (8,), ()): ("L", "L;R"),
  166. (MM, 1, (1,), 2, (8,), ()): ("L", "L;R"),
  167. (II, 1, (1,), 1, (12,), ()): ("I;16", "I;12"),
  168. (II, 0, (1,), 1, (16,), ()): ("I;16", "I;16"),
  169. (II, 1, (1,), 1, (16,), ()): ("I;16", "I;16"),
  170. (MM, 1, (1,), 1, (16,), ()): ("I;16B", "I;16B"),
  171. (II, 1, (1,), 2, (16,), ()): ("I;16", "I;16R"),
  172. (II, 1, (2,), 1, (16,), ()): ("I", "I;16S"),
  173. (MM, 1, (2,), 1, (16,), ()): ("I", "I;16BS"),
  174. (II, 0, (3,), 1, (32,), ()): ("F", "F;32F"),
  175. (MM, 0, (3,), 1, (32,), ()): ("F", "F;32BF"),
  176. (II, 1, (1,), 1, (32,), ()): ("I", "I;32N"),
  177. (II, 1, (2,), 1, (32,), ()): ("I", "I;32S"),
  178. (MM, 1, (2,), 1, (32,), ()): ("I", "I;32BS"),
  179. (II, 1, (3,), 1, (32,), ()): ("F", "F;32F"),
  180. (MM, 1, (3,), 1, (32,), ()): ("F", "F;32BF"),
  181. (II, 1, (1,), 1, (8, 8), (2,)): ("LA", "LA"),
  182. (MM, 1, (1,), 1, (8, 8), (2,)): ("LA", "LA"),
  183. (II, 2, (1,), 1, (8, 8, 8), ()): ("RGB", "RGB"),
  184. (MM, 2, (1,), 1, (8, 8, 8), ()): ("RGB", "RGB"),
  185. (II, 2, (1,), 2, (8, 8, 8), ()): ("RGB", "RGB;R"),
  186. (MM, 2, (1,), 2, (8, 8, 8), ()): ("RGB", "RGB;R"),
  187. (II, 2, (1,), 1, (8, 8, 8, 8), ()): ("RGBA", "RGBA"), # missing ExtraSamples
  188. (MM, 2, (1,), 1, (8, 8, 8, 8), ()): ("RGBA", "RGBA"), # missing ExtraSamples
  189. (II, 2, (1,), 1, (8, 8, 8, 8), (0,)): ("RGBX", "RGBX"),
  190. (MM, 2, (1,), 1, (8, 8, 8, 8), (0,)): ("RGBX", "RGBX"),
  191. (II, 2, (1,), 1, (8, 8, 8, 8, 8), (0, 0)): ("RGBX", "RGBXX"),
  192. (MM, 2, (1,), 1, (8, 8, 8, 8, 8), (0, 0)): ("RGBX", "RGBXX"),
  193. (II, 2, (1,), 1, (8, 8, 8, 8, 8, 8), (0, 0, 0)): ("RGBX", "RGBXXX"),
  194. (MM, 2, (1,), 1, (8, 8, 8, 8, 8, 8), (0, 0, 0)): ("RGBX", "RGBXXX"),
  195. (II, 2, (1,), 1, (8, 8, 8, 8), (1,)): ("RGBA", "RGBa"),
  196. (MM, 2, (1,), 1, (8, 8, 8, 8), (1,)): ("RGBA", "RGBa"),
  197. (II, 2, (1,), 1, (8, 8, 8, 8, 8), (1, 0)): ("RGBA", "RGBaX"),
  198. (MM, 2, (1,), 1, (8, 8, 8, 8, 8), (1, 0)): ("RGBA", "RGBaX"),
  199. (II, 2, (1,), 1, (8, 8, 8, 8, 8, 8), (1, 0, 0)): ("RGBA", "RGBaXX"),
  200. (MM, 2, (1,), 1, (8, 8, 8, 8, 8, 8), (1, 0, 0)): ("RGBA", "RGBaXX"),
  201. (II, 2, (1,), 1, (8, 8, 8, 8), (2,)): ("RGBA", "RGBA"),
  202. (MM, 2, (1,), 1, (8, 8, 8, 8), (2,)): ("RGBA", "RGBA"),
  203. (II, 2, (1,), 1, (8, 8, 8, 8, 8), (2, 0)): ("RGBA", "RGBAX"),
  204. (MM, 2, (1,), 1, (8, 8, 8, 8, 8), (2, 0)): ("RGBA", "RGBAX"),
  205. (II, 2, (1,), 1, (8, 8, 8, 8, 8, 8), (2, 0, 0)): ("RGBA", "RGBAXX"),
  206. (MM, 2, (1,), 1, (8, 8, 8, 8, 8, 8), (2, 0, 0)): ("RGBA", "RGBAXX"),
  207. (II, 2, (1,), 1, (8, 8, 8, 8), (999,)): ("RGBA", "RGBA"), # Corel Draw 10
  208. (MM, 2, (1,), 1, (8, 8, 8, 8), (999,)): ("RGBA", "RGBA"), # Corel Draw 10
  209. (II, 2, (1,), 1, (16, 16, 16), ()): ("RGB", "RGB;16L"),
  210. (MM, 2, (1,), 1, (16, 16, 16), ()): ("RGB", "RGB;16B"),
  211. (II, 2, (1,), 1, (16, 16, 16, 16), ()): ("RGBA", "RGBA;16L"),
  212. (MM, 2, (1,), 1, (16, 16, 16, 16), ()): ("RGBA", "RGBA;16B"),
  213. (II, 2, (1,), 1, (16, 16, 16, 16), (0,)): ("RGBX", "RGBX;16L"),
  214. (MM, 2, (1,), 1, (16, 16, 16, 16), (0,)): ("RGBX", "RGBX;16B"),
  215. (II, 2, (1,), 1, (16, 16, 16, 16), (1,)): ("RGBA", "RGBa;16L"),
  216. (MM, 2, (1,), 1, (16, 16, 16, 16), (1,)): ("RGBA", "RGBa;16B"),
  217. (II, 2, (1,), 1, (16, 16, 16, 16), (2,)): ("RGBA", "RGBA;16L"),
  218. (MM, 2, (1,), 1, (16, 16, 16, 16), (2,)): ("RGBA", "RGBA;16B"),
  219. (II, 3, (1,), 1, (1,), ()): ("P", "P;1"),
  220. (MM, 3, (1,), 1, (1,), ()): ("P", "P;1"),
  221. (II, 3, (1,), 2, (1,), ()): ("P", "P;1R"),
  222. (MM, 3, (1,), 2, (1,), ()): ("P", "P;1R"),
  223. (II, 3, (1,), 1, (2,), ()): ("P", "P;2"),
  224. (MM, 3, (1,), 1, (2,), ()): ("P", "P;2"),
  225. (II, 3, (1,), 2, (2,), ()): ("P", "P;2R"),
  226. (MM, 3, (1,), 2, (2,), ()): ("P", "P;2R"),
  227. (II, 3, (1,), 1, (4,), ()): ("P", "P;4"),
  228. (MM, 3, (1,), 1, (4,), ()): ("P", "P;4"),
  229. (II, 3, (1,), 2, (4,), ()): ("P", "P;4R"),
  230. (MM, 3, (1,), 2, (4,), ()): ("P", "P;4R"),
  231. (II, 3, (1,), 1, (8,), ()): ("P", "P"),
  232. (MM, 3, (1,), 1, (8,), ()): ("P", "P"),
  233. (II, 3, (1,), 1, (8, 8), (2,)): ("PA", "PA"),
  234. (MM, 3, (1,), 1, (8, 8), (2,)): ("PA", "PA"),
  235. (II, 3, (1,), 2, (8,), ()): ("P", "P;R"),
  236. (MM, 3, (1,), 2, (8,), ()): ("P", "P;R"),
  237. (II, 5, (1,), 1, (8, 8, 8, 8), ()): ("CMYK", "CMYK"),
  238. (MM, 5, (1,), 1, (8, 8, 8, 8), ()): ("CMYK", "CMYK"),
  239. (II, 5, (1,), 1, (8, 8, 8, 8, 8), (0,)): ("CMYK", "CMYKX"),
  240. (MM, 5, (1,), 1, (8, 8, 8, 8, 8), (0,)): ("CMYK", "CMYKX"),
  241. (II, 5, (1,), 1, (8, 8, 8, 8, 8, 8), (0, 0)): ("CMYK", "CMYKXX"),
  242. (MM, 5, (1,), 1, (8, 8, 8, 8, 8, 8), (0, 0)): ("CMYK", "CMYKXX"),
  243. (II, 5, (1,), 1, (16, 16, 16, 16), ()): ("CMYK", "CMYK;16L"),
  244. (II, 6, (1,), 1, (8,), ()): ("L", "L"),
  245. (MM, 6, (1,), 1, (8,), ()): ("L", "L"),
  246. # JPEG compressed images handled by LibTiff and auto-converted to RGBX
  247. # Minimal Baseline TIFF requires YCbCr images to have 3 SamplesPerPixel
  248. (II, 6, (1,), 1, (8, 8, 8), ()): ("RGB", "RGBX"),
  249. (MM, 6, (1,), 1, (8, 8, 8), ()): ("RGB", "RGBX"),
  250. (II, 8, (1,), 1, (8, 8, 8), ()): ("LAB", "LAB"),
  251. (MM, 8, (1,), 1, (8, 8, 8), ()): ("LAB", "LAB"),
  252. }
  253. MAX_SAMPLESPERPIXEL = max(len(key_tp[4]) for key_tp in OPEN_INFO)
  254. PREFIXES = [
  255. b"MM\x00\x2A", # Valid TIFF header with big-endian byte order
  256. b"II\x2A\x00", # Valid TIFF header with little-endian byte order
  257. b"MM\x2A\x00", # Invalid TIFF header, assume big-endian
  258. b"II\x00\x2A", # Invalid TIFF header, assume little-endian
  259. b"MM\x00\x2B", # BigTIFF with big-endian byte order
  260. b"II\x2B\x00", # BigTIFF with little-endian byte order
  261. ]
  262. def _accept(prefix):
  263. return prefix[:4] in PREFIXES
  264. def _limit_rational(val, max_val):
  265. inv = abs(val) > 1
  266. n_d = IFDRational(1 / val if inv else val).limit_rational(max_val)
  267. return n_d[::-1] if inv else n_d
  268. def _limit_signed_rational(val, max_val, min_val):
  269. frac = Fraction(val)
  270. n_d = frac.numerator, frac.denominator
  271. if min(n_d) < min_val:
  272. n_d = _limit_rational(val, abs(min_val))
  273. if max(n_d) > max_val:
  274. val = Fraction(*n_d)
  275. n_d = _limit_rational(val, max_val)
  276. return n_d
  277. ##
  278. # Wrapper for TIFF IFDs.
  279. _load_dispatch = {}
  280. _write_dispatch = {}
  281. class IFDRational(Rational):
  282. """Implements a rational class where 0/0 is a legal value to match
  283. the in the wild use of exif rationals.
  284. e.g., DigitalZoomRatio - 0.00/0.00 indicates that no digital zoom was used
  285. """
  286. """ If the denominator is 0, store this as a float('nan'), otherwise store
  287. as a fractions.Fraction(). Delegate as appropriate
  288. """
  289. __slots__ = ("_numerator", "_denominator", "_val")
  290. def __init__(self, value, denominator=1):
  291. """
  292. :param value: either an integer numerator, a
  293. float/rational/other number, or an IFDRational
  294. :param denominator: Optional integer denominator
  295. """
  296. if isinstance(value, IFDRational):
  297. self._numerator = value.numerator
  298. self._denominator = value.denominator
  299. self._val = value._val
  300. return
  301. if isinstance(value, Fraction):
  302. self._numerator = value.numerator
  303. self._denominator = value.denominator
  304. else:
  305. self._numerator = value
  306. self._denominator = denominator
  307. if denominator == 0:
  308. self._val = float("nan")
  309. elif denominator == 1:
  310. self._val = Fraction(value)
  311. else:
  312. self._val = Fraction(value, denominator)
  313. @property
  314. def numerator(self):
  315. return self._numerator
  316. @property
  317. def denominator(self):
  318. return self._denominator
  319. def limit_rational(self, max_denominator):
  320. """
  321. :param max_denominator: Integer, the maximum denominator value
  322. :returns: Tuple of (numerator, denominator)
  323. """
  324. if self.denominator == 0:
  325. return self.numerator, self.denominator
  326. f = self._val.limit_denominator(max_denominator)
  327. return f.numerator, f.denominator
  328. def __repr__(self):
  329. return str(float(self._val))
  330. def __hash__(self):
  331. return self._val.__hash__()
  332. def __eq__(self, other):
  333. val = self._val
  334. if isinstance(other, IFDRational):
  335. other = other._val
  336. if isinstance(other, float):
  337. val = float(val)
  338. return val == other
  339. def __getstate__(self):
  340. return [self._val, self._numerator, self._denominator]
  341. def __setstate__(self, state):
  342. IFDRational.__init__(self, 0)
  343. _val, _numerator, _denominator = state
  344. self._val = _val
  345. self._numerator = _numerator
  346. self._denominator = _denominator
  347. def _delegate(op):
  348. def delegate(self, *args):
  349. return getattr(self._val, op)(*args)
  350. return delegate
  351. """ a = ['add','radd', 'sub', 'rsub', 'mul', 'rmul',
  352. 'truediv', 'rtruediv', 'floordiv', 'rfloordiv',
  353. 'mod','rmod', 'pow','rpow', 'pos', 'neg',
  354. 'abs', 'trunc', 'lt', 'gt', 'le', 'ge', 'bool',
  355. 'ceil', 'floor', 'round']
  356. print("\n".join("__%s__ = _delegate('__%s__')" % (s,s) for s in a))
  357. """
  358. __add__ = _delegate("__add__")
  359. __radd__ = _delegate("__radd__")
  360. __sub__ = _delegate("__sub__")
  361. __rsub__ = _delegate("__rsub__")
  362. __mul__ = _delegate("__mul__")
  363. __rmul__ = _delegate("__rmul__")
  364. __truediv__ = _delegate("__truediv__")
  365. __rtruediv__ = _delegate("__rtruediv__")
  366. __floordiv__ = _delegate("__floordiv__")
  367. __rfloordiv__ = _delegate("__rfloordiv__")
  368. __mod__ = _delegate("__mod__")
  369. __rmod__ = _delegate("__rmod__")
  370. __pow__ = _delegate("__pow__")
  371. __rpow__ = _delegate("__rpow__")
  372. __pos__ = _delegate("__pos__")
  373. __neg__ = _delegate("__neg__")
  374. __abs__ = _delegate("__abs__")
  375. __trunc__ = _delegate("__trunc__")
  376. __lt__ = _delegate("__lt__")
  377. __gt__ = _delegate("__gt__")
  378. __le__ = _delegate("__le__")
  379. __ge__ = _delegate("__ge__")
  380. __bool__ = _delegate("__bool__")
  381. __ceil__ = _delegate("__ceil__")
  382. __floor__ = _delegate("__floor__")
  383. __round__ = _delegate("__round__")
  384. # Python >= 3.11
  385. if hasattr(Fraction, "__int__"):
  386. __int__ = _delegate("__int__")
  387. class ImageFileDirectory_v2(MutableMapping):
  388. """This class represents a TIFF tag directory. To speed things up, we
  389. don't decode tags unless they're asked for.
  390. Exposes a dictionary interface of the tags in the directory::
  391. ifd = ImageFileDirectory_v2()
  392. ifd[key] = 'Some Data'
  393. ifd.tagtype[key] = TiffTags.ASCII
  394. print(ifd[key])
  395. 'Some Data'
  396. Individual values are returned as the strings or numbers, sequences are
  397. returned as tuples of the values.
  398. The tiff metadata type of each item is stored in a dictionary of
  399. tag types in
  400. :attr:`~PIL.TiffImagePlugin.ImageFileDirectory_v2.tagtype`. The types
  401. are read from a tiff file, guessed from the type added, or added
  402. manually.
  403. Data Structures:
  404. * ``self.tagtype = {}``
  405. * Key: numerical TIFF tag number
  406. * Value: integer corresponding to the data type from
  407. :py:data:`.TiffTags.TYPES`
  408. .. versionadded:: 3.0.0
  409. 'Internal' data structures:
  410. * ``self._tags_v2 = {}``
  411. * Key: numerical TIFF tag number
  412. * Value: decoded data, as tuple for multiple values
  413. * ``self._tagdata = {}``
  414. * Key: numerical TIFF tag number
  415. * Value: undecoded byte string from file
  416. * ``self._tags_v1 = {}``
  417. * Key: numerical TIFF tag number
  418. * Value: decoded data in the v1 format
  419. Tags will be found in the private attributes ``self._tagdata``, and in
  420. ``self._tags_v2`` once decoded.
  421. ``self.legacy_api`` is a value for internal use, and shouldn't be changed
  422. from outside code. In cooperation with
  423. :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v1`, if ``legacy_api``
  424. is true, then decoded tags will be populated into both ``_tags_v1`` and
  425. ``_tags_v2``. ``_tags_v2`` will be used if this IFD is used in the TIFF
  426. save routine. Tags should be read from ``_tags_v1`` if
  427. ``legacy_api == true``.
  428. """
  429. def __init__(self, ifh=b"II\052\0\0\0\0\0", prefix=None, group=None):
  430. """Initialize an ImageFileDirectory.
  431. To construct an ImageFileDirectory from a real file, pass the 8-byte
  432. magic header to the constructor. To only set the endianness, pass it
  433. as the 'prefix' keyword argument.
  434. :param ifh: One of the accepted magic headers (cf. PREFIXES); also sets
  435. endianness.
  436. :param prefix: Override the endianness of the file.
  437. """
  438. if not _accept(ifh):
  439. msg = f"not a TIFF file (header {repr(ifh)} not valid)"
  440. raise SyntaxError(msg)
  441. self._prefix = prefix if prefix is not None else ifh[:2]
  442. if self._prefix == MM:
  443. self._endian = ">"
  444. elif self._prefix == II:
  445. self._endian = "<"
  446. else:
  447. msg = "not a TIFF IFD"
  448. raise SyntaxError(msg)
  449. self._bigtiff = ifh[2] == 43
  450. self.group = group
  451. self.tagtype = {}
  452. """ Dictionary of tag types """
  453. self.reset()
  454. (self.next,) = (
  455. self._unpack("Q", ifh[8:]) if self._bigtiff else self._unpack("L", ifh[4:])
  456. )
  457. self._legacy_api = False
  458. prefix = property(lambda self: self._prefix)
  459. offset = property(lambda self: self._offset)
  460. legacy_api = property(lambda self: self._legacy_api)
  461. @legacy_api.setter
  462. def legacy_api(self, value):
  463. msg = "Not allowing setting of legacy api"
  464. raise Exception(msg)
  465. def reset(self):
  466. self._tags_v1 = {} # will remain empty if legacy_api is false
  467. self._tags_v2 = {} # main tag storage
  468. self._tagdata = {}
  469. self.tagtype = {} # added 2008-06-05 by Florian Hoech
  470. self._next = None
  471. self._offset = None
  472. def __str__(self):
  473. return str(dict(self))
  474. def named(self):
  475. """
  476. :returns: dict of name|key: value
  477. Returns the complete tag dictionary, with named tags where possible.
  478. """
  479. return {
  480. TiffTags.lookup(code, self.group).name: value
  481. for code, value in self.items()
  482. }
  483. def __len__(self):
  484. return len(set(self._tagdata) | set(self._tags_v2))
  485. def __getitem__(self, tag):
  486. if tag not in self._tags_v2: # unpack on the fly
  487. data = self._tagdata[tag]
  488. typ = self.tagtype[tag]
  489. size, handler = self._load_dispatch[typ]
  490. self[tag] = handler(self, data, self.legacy_api) # check type
  491. val = self._tags_v2[tag]
  492. if self.legacy_api and not isinstance(val, (tuple, bytes)):
  493. val = (val,)
  494. return val
  495. def __contains__(self, tag):
  496. return tag in self._tags_v2 or tag in self._tagdata
  497. def __setitem__(self, tag, value):
  498. self._setitem(tag, value, self.legacy_api)
  499. def _setitem(self, tag, value, legacy_api):
  500. basetypes = (Number, bytes, str)
  501. info = TiffTags.lookup(tag, self.group)
  502. values = [value] if isinstance(value, basetypes) else value
  503. if tag not in self.tagtype:
  504. if info.type:
  505. self.tagtype[tag] = info.type
  506. else:
  507. self.tagtype[tag] = TiffTags.UNDEFINED
  508. if all(isinstance(v, IFDRational) for v in values):
  509. self.tagtype[tag] = (
  510. TiffTags.RATIONAL
  511. if all(v >= 0 for v in values)
  512. else TiffTags.SIGNED_RATIONAL
  513. )
  514. elif all(isinstance(v, int) for v in values):
  515. if all(0 <= v < 2**16 for v in values):
  516. self.tagtype[tag] = TiffTags.SHORT
  517. elif all(-(2**15) < v < 2**15 for v in values):
  518. self.tagtype[tag] = TiffTags.SIGNED_SHORT
  519. else:
  520. self.tagtype[tag] = (
  521. TiffTags.LONG
  522. if all(v >= 0 for v in values)
  523. else TiffTags.SIGNED_LONG
  524. )
  525. elif all(isinstance(v, float) for v in values):
  526. self.tagtype[tag] = TiffTags.DOUBLE
  527. elif all(isinstance(v, str) for v in values):
  528. self.tagtype[tag] = TiffTags.ASCII
  529. elif all(isinstance(v, bytes) for v in values):
  530. self.tagtype[tag] = TiffTags.BYTE
  531. if self.tagtype[tag] == TiffTags.UNDEFINED:
  532. values = [
  533. v.encode("ascii", "replace") if isinstance(v, str) else v
  534. for v in values
  535. ]
  536. elif self.tagtype[tag] == TiffTags.RATIONAL:
  537. values = [float(v) if isinstance(v, int) else v for v in values]
  538. is_ifd = self.tagtype[tag] == TiffTags.LONG and isinstance(values, dict)
  539. if not is_ifd:
  540. values = tuple(info.cvt_enum(value) for value in values)
  541. dest = self._tags_v1 if legacy_api else self._tags_v2
  542. # Three branches:
  543. # Spec'd length == 1, Actual length 1, store as element
  544. # Spec'd length == 1, Actual > 1, Warn and truncate. Formerly barfed.
  545. # No Spec, Actual length 1, Formerly (<4.2) returned a 1 element tuple.
  546. # Don't mess with the legacy api, since it's frozen.
  547. if not is_ifd and (
  548. (info.length == 1)
  549. or self.tagtype[tag] == TiffTags.BYTE
  550. or (info.length is None and len(values) == 1 and not legacy_api)
  551. ):
  552. # Don't mess with the legacy api, since it's frozen.
  553. if legacy_api and self.tagtype[tag] in [
  554. TiffTags.RATIONAL,
  555. TiffTags.SIGNED_RATIONAL,
  556. ]: # rationals
  557. values = (values,)
  558. try:
  559. (dest[tag],) = values
  560. except ValueError:
  561. # We've got a builtin tag with 1 expected entry
  562. warnings.warn(
  563. f"Metadata Warning, tag {tag} had too many entries: "
  564. f"{len(values)}, expected 1"
  565. )
  566. dest[tag] = values[0]
  567. else:
  568. # Spec'd length > 1 or undefined
  569. # Unspec'd, and length > 1
  570. dest[tag] = values
  571. def __delitem__(self, tag):
  572. self._tags_v2.pop(tag, None)
  573. self._tags_v1.pop(tag, None)
  574. self._tagdata.pop(tag, None)
  575. def __iter__(self):
  576. return iter(set(self._tagdata) | set(self._tags_v2))
  577. def _unpack(self, fmt, data):
  578. return struct.unpack(self._endian + fmt, data)
  579. def _pack(self, fmt, *values):
  580. return struct.pack(self._endian + fmt, *values)
  581. def _register_loader(idx, size):
  582. def decorator(func):
  583. from .TiffTags import TYPES
  584. if func.__name__.startswith("load_"):
  585. TYPES[idx] = func.__name__[5:].replace("_", " ")
  586. _load_dispatch[idx] = size, func # noqa: F821
  587. return func
  588. return decorator
  589. def _register_writer(idx):
  590. def decorator(func):
  591. _write_dispatch[idx] = func # noqa: F821
  592. return func
  593. return decorator
  594. def _register_basic(idx_fmt_name):
  595. from .TiffTags import TYPES
  596. idx, fmt, name = idx_fmt_name
  597. TYPES[idx] = name
  598. size = struct.calcsize("=" + fmt)
  599. _load_dispatch[idx] = ( # noqa: F821
  600. size,
  601. lambda self, data, legacy_api=True: (
  602. self._unpack(f"{len(data) // size}{fmt}", data)
  603. ),
  604. )
  605. _write_dispatch[idx] = lambda self, *values: ( # noqa: F821
  606. b"".join(self._pack(fmt, value) for value in values)
  607. )
  608. list(
  609. map(
  610. _register_basic,
  611. [
  612. (TiffTags.SHORT, "H", "short"),
  613. (TiffTags.LONG, "L", "long"),
  614. (TiffTags.SIGNED_BYTE, "b", "signed byte"),
  615. (TiffTags.SIGNED_SHORT, "h", "signed short"),
  616. (TiffTags.SIGNED_LONG, "l", "signed long"),
  617. (TiffTags.FLOAT, "f", "float"),
  618. (TiffTags.DOUBLE, "d", "double"),
  619. (TiffTags.IFD, "L", "long"),
  620. (TiffTags.LONG8, "Q", "long8"),
  621. ],
  622. )
  623. )
  624. @_register_loader(1, 1) # Basic type, except for the legacy API.
  625. def load_byte(self, data, legacy_api=True):
  626. return data
  627. @_register_writer(1) # Basic type, except for the legacy API.
  628. def write_byte(self, data):
  629. if isinstance(data, IFDRational):
  630. data = int(data)
  631. if isinstance(data, int):
  632. data = bytes((data,))
  633. return data
  634. @_register_loader(2, 1)
  635. def load_string(self, data, legacy_api=True):
  636. if data.endswith(b"\0"):
  637. data = data[:-1]
  638. return data.decode("latin-1", "replace")
  639. @_register_writer(2)
  640. def write_string(self, value):
  641. # remerge of https://github.com/python-pillow/Pillow/pull/1416
  642. if isinstance(value, int):
  643. value = str(value)
  644. if not isinstance(value, bytes):
  645. value = value.encode("ascii", "replace")
  646. return value + b"\0"
  647. @_register_loader(5, 8)
  648. def load_rational(self, data, legacy_api=True):
  649. vals = self._unpack(f"{len(data) // 4}L", data)
  650. def combine(a, b):
  651. return (a, b) if legacy_api else IFDRational(a, b)
  652. return tuple(combine(num, denom) for num, denom in zip(vals[::2], vals[1::2]))
  653. @_register_writer(5)
  654. def write_rational(self, *values):
  655. return b"".join(
  656. self._pack("2L", *_limit_rational(frac, 2**32 - 1)) for frac in values
  657. )
  658. @_register_loader(7, 1)
  659. def load_undefined(self, data, legacy_api=True):
  660. return data
  661. @_register_writer(7)
  662. def write_undefined(self, value):
  663. if isinstance(value, int):
  664. value = str(value).encode("ascii", "replace")
  665. return value
  666. @_register_loader(10, 8)
  667. def load_signed_rational(self, data, legacy_api=True):
  668. vals = self._unpack(f"{len(data) // 4}l", data)
  669. def combine(a, b):
  670. return (a, b) if legacy_api else IFDRational(a, b)
  671. return tuple(combine(num, denom) for num, denom in zip(vals[::2], vals[1::2]))
  672. @_register_writer(10)
  673. def write_signed_rational(self, *values):
  674. return b"".join(
  675. self._pack("2l", *_limit_signed_rational(frac, 2**31 - 1, -(2**31)))
  676. for frac in values
  677. )
  678. def _ensure_read(self, fp, size):
  679. ret = fp.read(size)
  680. if len(ret) != size:
  681. msg = (
  682. "Corrupt EXIF data. "
  683. f"Expecting to read {size} bytes but only got {len(ret)}. "
  684. )
  685. raise OSError(msg)
  686. return ret
  687. def load(self, fp):
  688. self.reset()
  689. self._offset = fp.tell()
  690. try:
  691. tag_count = (
  692. self._unpack("Q", self._ensure_read(fp, 8))
  693. if self._bigtiff
  694. else self._unpack("H", self._ensure_read(fp, 2))
  695. )[0]
  696. for i in range(tag_count):
  697. tag, typ, count, data = (
  698. self._unpack("HHQ8s", self._ensure_read(fp, 20))
  699. if self._bigtiff
  700. else self._unpack("HHL4s", self._ensure_read(fp, 12))
  701. )
  702. tagname = TiffTags.lookup(tag, self.group).name
  703. typname = TYPES.get(typ, "unknown")
  704. msg = f"tag: {tagname} ({tag}) - type: {typname} ({typ})"
  705. try:
  706. unit_size, handler = self._load_dispatch[typ]
  707. except KeyError:
  708. logger.debug("%s - unsupported type %s", msg, typ)
  709. continue # ignore unsupported type
  710. size = count * unit_size
  711. if size > (8 if self._bigtiff else 4):
  712. here = fp.tell()
  713. (offset,) = self._unpack("Q" if self._bigtiff else "L", data)
  714. msg += f" Tag Location: {here} - Data Location: {offset}"
  715. fp.seek(offset)
  716. data = ImageFile._safe_read(fp, size)
  717. fp.seek(here)
  718. else:
  719. data = data[:size]
  720. if len(data) != size:
  721. warnings.warn(
  722. "Possibly corrupt EXIF data. "
  723. f"Expecting to read {size} bytes but only got {len(data)}."
  724. f" Skipping tag {tag}"
  725. )
  726. logger.debug(msg)
  727. continue
  728. if not data:
  729. logger.debug(msg)
  730. continue
  731. self._tagdata[tag] = data
  732. self.tagtype[tag] = typ
  733. msg += " - value: " + (
  734. "<table: %d bytes>" % size if size > 32 else repr(data)
  735. )
  736. logger.debug(msg)
  737. (self.next,) = (
  738. self._unpack("Q", self._ensure_read(fp, 8))
  739. if self._bigtiff
  740. else self._unpack("L", self._ensure_read(fp, 4))
  741. )
  742. except OSError as msg:
  743. warnings.warn(str(msg))
  744. return
  745. def tobytes(self, offset=0):
  746. # FIXME What about tagdata?
  747. result = self._pack("H", len(self._tags_v2))
  748. entries = []
  749. offset = offset + len(result) + len(self._tags_v2) * 12 + 4
  750. stripoffsets = None
  751. # pass 1: convert tags to binary format
  752. # always write tags in ascending order
  753. for tag, value in sorted(self._tags_v2.items()):
  754. if tag == STRIPOFFSETS:
  755. stripoffsets = len(entries)
  756. typ = self.tagtype.get(tag)
  757. logger.debug("Tag %s, Type: %s, Value: %s", tag, typ, repr(value))
  758. is_ifd = typ == TiffTags.LONG and isinstance(value, dict)
  759. if is_ifd:
  760. if self._endian == "<":
  761. ifh = b"II\x2A\x00\x08\x00\x00\x00"
  762. else:
  763. ifh = b"MM\x00\x2A\x00\x00\x00\x08"
  764. ifd = ImageFileDirectory_v2(ifh, group=tag)
  765. values = self._tags_v2[tag]
  766. for ifd_tag, ifd_value in values.items():
  767. ifd[ifd_tag] = ifd_value
  768. data = ifd.tobytes(offset)
  769. else:
  770. values = value if isinstance(value, tuple) else (value,)
  771. data = self._write_dispatch[typ](self, *values)
  772. tagname = TiffTags.lookup(tag, self.group).name
  773. typname = "ifd" if is_ifd else TYPES.get(typ, "unknown")
  774. msg = f"save: {tagname} ({tag}) - type: {typname} ({typ})"
  775. msg += " - value: " + (
  776. "<table: %d bytes>" % len(data) if len(data) >= 16 else str(values)
  777. )
  778. logger.debug(msg)
  779. # count is sum of lengths for string and arbitrary data
  780. if is_ifd:
  781. count = 1
  782. elif typ in [TiffTags.BYTE, TiffTags.ASCII, TiffTags.UNDEFINED]:
  783. count = len(data)
  784. else:
  785. count = len(values)
  786. # figure out if data fits into the entry
  787. if len(data) <= 4:
  788. entries.append((tag, typ, count, data.ljust(4, b"\0"), b""))
  789. else:
  790. entries.append((tag, typ, count, self._pack("L", offset), data))
  791. offset += (len(data) + 1) // 2 * 2 # pad to word
  792. # update strip offset data to point beyond auxiliary data
  793. if stripoffsets is not None:
  794. tag, typ, count, value, data = entries[stripoffsets]
  795. if data:
  796. msg = "multistrip support not yet implemented"
  797. raise NotImplementedError(msg)
  798. value = self._pack("L", self._unpack("L", value)[0] + offset)
  799. entries[stripoffsets] = tag, typ, count, value, data
  800. # pass 2: write entries to file
  801. for tag, typ, count, value, data in entries:
  802. logger.debug("%s %s %s %s %s", tag, typ, count, repr(value), repr(data))
  803. result += self._pack("HHL4s", tag, typ, count, value)
  804. # -- overwrite here for multi-page --
  805. result += b"\0\0\0\0" # end of entries
  806. # pass 3: write auxiliary data to file
  807. for tag, typ, count, value, data in entries:
  808. result += data
  809. if len(data) & 1:
  810. result += b"\0"
  811. return result
  812. def save(self, fp):
  813. if fp.tell() == 0: # skip TIFF header on subsequent pages
  814. # tiff header -- PIL always starts the first IFD at offset 8
  815. fp.write(self._prefix + self._pack("HL", 42, 8))
  816. offset = fp.tell()
  817. result = self.tobytes(offset)
  818. fp.write(result)
  819. return offset + len(result)
  820. ImageFileDirectory_v2._load_dispatch = _load_dispatch
  821. ImageFileDirectory_v2._write_dispatch = _write_dispatch
  822. for idx, name in TYPES.items():
  823. name = name.replace(" ", "_")
  824. setattr(ImageFileDirectory_v2, "load_" + name, _load_dispatch[idx][1])
  825. setattr(ImageFileDirectory_v2, "write_" + name, _write_dispatch[idx])
  826. del _load_dispatch, _write_dispatch, idx, name
  827. # Legacy ImageFileDirectory support.
  828. class ImageFileDirectory_v1(ImageFileDirectory_v2):
  829. """This class represents the **legacy** interface to a TIFF tag directory.
  830. Exposes a dictionary interface of the tags in the directory::
  831. ifd = ImageFileDirectory_v1()
  832. ifd[key] = 'Some Data'
  833. ifd.tagtype[key] = TiffTags.ASCII
  834. print(ifd[key])
  835. ('Some Data',)
  836. Also contains a dictionary of tag types as read from the tiff image file,
  837. :attr:`~PIL.TiffImagePlugin.ImageFileDirectory_v1.tagtype`.
  838. Values are returned as a tuple.
  839. .. deprecated:: 3.0.0
  840. """
  841. def __init__(self, *args, **kwargs):
  842. super().__init__(*args, **kwargs)
  843. self._legacy_api = True
  844. tags = property(lambda self: self._tags_v1)
  845. tagdata = property(lambda self: self._tagdata)
  846. # defined in ImageFileDirectory_v2
  847. tagtype: dict
  848. """Dictionary of tag types"""
  849. @classmethod
  850. def from_v2(cls, original):
  851. """Returns an
  852. :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v1`
  853. instance with the same data as is contained in the original
  854. :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v2`
  855. instance.
  856. :returns: :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v1`
  857. """
  858. ifd = cls(prefix=original.prefix)
  859. ifd._tagdata = original._tagdata
  860. ifd.tagtype = original.tagtype
  861. ifd.next = original.next # an indicator for multipage tiffs
  862. return ifd
  863. def to_v2(self):
  864. """Returns an
  865. :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v2`
  866. instance with the same data as is contained in the original
  867. :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v1`
  868. instance.
  869. :returns: :py:class:`~PIL.TiffImagePlugin.ImageFileDirectory_v2`
  870. """
  871. ifd = ImageFileDirectory_v2(prefix=self.prefix)
  872. ifd._tagdata = dict(self._tagdata)
  873. ifd.tagtype = dict(self.tagtype)
  874. ifd._tags_v2 = dict(self._tags_v2)
  875. return ifd
  876. def __contains__(self, tag):
  877. return tag in self._tags_v1 or tag in self._tagdata
  878. def __len__(self):
  879. return len(set(self._tagdata) | set(self._tags_v1))
  880. def __iter__(self):
  881. return iter(set(self._tagdata) | set(self._tags_v1))
  882. def __setitem__(self, tag, value):
  883. for legacy_api in (False, True):
  884. self._setitem(tag, value, legacy_api)
  885. def __getitem__(self, tag):
  886. if tag not in self._tags_v1: # unpack on the fly
  887. data = self._tagdata[tag]
  888. typ = self.tagtype[tag]
  889. size, handler = self._load_dispatch[typ]
  890. for legacy in (False, True):
  891. self._setitem(tag, handler(self, data, legacy), legacy)
  892. val = self._tags_v1[tag]
  893. if not isinstance(val, (tuple, bytes)):
  894. val = (val,)
  895. return val
  896. # undone -- switch this pointer when IFD_LEGACY_API == False
  897. ImageFileDirectory = ImageFileDirectory_v1
  898. ##
  899. # Image plugin for TIFF files.
  900. class TiffImageFile(ImageFile.ImageFile):
  901. format = "TIFF"
  902. format_description = "Adobe TIFF"
  903. _close_exclusive_fp_after_loading = False
  904. def __init__(self, fp=None, filename=None):
  905. self.tag_v2 = None
  906. """ Image file directory (tag dictionary) """
  907. self.tag = None
  908. """ Legacy tag entries """
  909. super().__init__(fp, filename)
  910. def _open(self):
  911. """Open the first image in a TIFF file"""
  912. # Header
  913. ifh = self.fp.read(8)
  914. if ifh[2] == 43:
  915. ifh += self.fp.read(8)
  916. self.tag_v2 = ImageFileDirectory_v2(ifh)
  917. # legacy IFD entries will be filled in later
  918. self.ifd = None
  919. # setup frame pointers
  920. self.__first = self.__next = self.tag_v2.next
  921. self.__frame = -1
  922. self._fp = self.fp
  923. self._frame_pos = []
  924. self._n_frames = None
  925. logger.debug("*** TiffImageFile._open ***")
  926. logger.debug("- __first: %s", self.__first)
  927. logger.debug("- ifh: %s", repr(ifh)) # Use repr to avoid str(bytes)
  928. # and load the first frame
  929. self._seek(0)
  930. @property
  931. def n_frames(self):
  932. if self._n_frames is None:
  933. current = self.tell()
  934. self._seek(len(self._frame_pos))
  935. while self._n_frames is None:
  936. self._seek(self.tell() + 1)
  937. self.seek(current)
  938. return self._n_frames
  939. def seek(self, frame):
  940. """Select a given frame as current image"""
  941. if not self._seek_check(frame):
  942. return
  943. self._seek(frame)
  944. # Create a new core image object on second and
  945. # subsequent frames in the image. Image may be
  946. # different size/mode.
  947. Image._decompression_bomb_check(self.size)
  948. self.im = Image.core.new(self.mode, self.size)
  949. def _seek(self, frame):
  950. self.fp = self._fp
  951. # reset buffered io handle in case fp
  952. # was passed to libtiff, invalidating the buffer
  953. self.fp.tell()
  954. while len(self._frame_pos) <= frame:
  955. if not self.__next:
  956. msg = "no more images in TIFF file"
  957. raise EOFError(msg)
  958. logger.debug(
  959. "Seeking to frame %s, on frame %s, __next %s, location: %s",
  960. frame,
  961. self.__frame,
  962. self.__next,
  963. self.fp.tell(),
  964. )
  965. self.fp.seek(self.__next)
  966. self._frame_pos.append(self.__next)
  967. logger.debug("Loading tags, location: %s", self.fp.tell())
  968. self.tag_v2.load(self.fp)
  969. if self.tag_v2.next in self._frame_pos:
  970. # This IFD has already been processed
  971. # Declare this to be the end of the image
  972. self.__next = 0
  973. else:
  974. self.__next = self.tag_v2.next
  975. if self.__next == 0:
  976. self._n_frames = frame + 1
  977. if len(self._frame_pos) == 1:
  978. self.is_animated = self.__next != 0
  979. self.__frame += 1
  980. self.fp.seek(self._frame_pos[frame])
  981. self.tag_v2.load(self.fp)
  982. self._reload_exif()
  983. # fill the legacy tag/ifd entries
  984. self.tag = self.ifd = ImageFileDirectory_v1.from_v2(self.tag_v2)
  985. self.__frame = frame
  986. self._setup()
  987. def tell(self):
  988. """Return the current frame number"""
  989. return self.__frame
  990. def getxmp(self):
  991. """
  992. Returns a dictionary containing the XMP tags.
  993. Requires defusedxml to be installed.
  994. :returns: XMP tags in a dictionary.
  995. """
  996. return self._getxmp(self.tag_v2[XMP]) if XMP in self.tag_v2 else {}
  997. def get_photoshop_blocks(self):
  998. """
  999. Returns a dictionary of Photoshop "Image Resource Blocks".
  1000. The keys are the image resource ID. For more information, see
  1001. https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_pgfId-1037727
  1002. :returns: Photoshop "Image Resource Blocks" in a dictionary.
  1003. """
  1004. blocks = {}
  1005. val = self.tag_v2.get(ExifTags.Base.ImageResources)
  1006. if val:
  1007. while val[:4] == b"8BIM":
  1008. id = i16(val[4:6])
  1009. n = math.ceil((val[6] + 1) / 2) * 2
  1010. size = i32(val[6 + n : 10 + n])
  1011. data = val[10 + n : 10 + n + size]
  1012. blocks[id] = {"data": data}
  1013. val = val[math.ceil((10 + n + size) / 2) * 2 :]
  1014. return blocks
  1015. def load(self):
  1016. if self.tile and self.use_load_libtiff:
  1017. return self._load_libtiff()
  1018. return super().load()
  1019. def load_end(self):
  1020. # allow closing if we're on the first frame, there's no next
  1021. # This is the ImageFile.load path only, libtiff specific below.
  1022. if not self.is_animated:
  1023. self._close_exclusive_fp_after_loading = True
  1024. # reset buffered io handle in case fp
  1025. # was passed to libtiff, invalidating the buffer
  1026. self.fp.tell()
  1027. # load IFD data from fp before it is closed
  1028. exif = self.getexif()
  1029. for key in TiffTags.TAGS_V2_GROUPS:
  1030. if key not in exif:
  1031. continue
  1032. exif.get_ifd(key)
  1033. ImageOps.exif_transpose(self, in_place=True)
  1034. if ExifTags.Base.Orientation in self.tag_v2:
  1035. del self.tag_v2[ExifTags.Base.Orientation]
  1036. def _load_libtiff(self):
  1037. """Overload method triggered when we detect a compressed tiff
  1038. Calls out to libtiff"""
  1039. Image.Image.load(self)
  1040. self.load_prepare()
  1041. if not len(self.tile) == 1:
  1042. msg = "Not exactly one tile"
  1043. raise OSError(msg)
  1044. # (self._compression, (extents tuple),
  1045. # 0, (rawmode, self._compression, fp))
  1046. extents = self.tile[0][1]
  1047. args = list(self.tile[0][3])
  1048. # To be nice on memory footprint, if there's a
  1049. # file descriptor, use that instead of reading
  1050. # into a string in python.
  1051. try:
  1052. fp = hasattr(self.fp, "fileno") and self.fp.fileno()
  1053. # flush the file descriptor, prevents error on pypy 2.4+
  1054. # should also eliminate the need for fp.tell
  1055. # in _seek
  1056. if hasattr(self.fp, "flush"):
  1057. self.fp.flush()
  1058. except OSError:
  1059. # io.BytesIO have a fileno, but returns an OSError if
  1060. # it doesn't use a file descriptor.
  1061. fp = False
  1062. if fp:
  1063. args[2] = fp
  1064. decoder = Image._getdecoder(
  1065. self.mode, "libtiff", tuple(args), self.decoderconfig
  1066. )
  1067. try:
  1068. decoder.setimage(self.im, extents)
  1069. except ValueError as e:
  1070. msg = "Couldn't set the image"
  1071. raise OSError(msg) from e
  1072. close_self_fp = self._exclusive_fp and not self.is_animated
  1073. if hasattr(self.fp, "getvalue"):
  1074. # We've got a stringio like thing passed in. Yay for all in memory.
  1075. # The decoder needs the entire file in one shot, so there's not
  1076. # a lot we can do here other than give it the entire file.
  1077. # unless we could do something like get the address of the
  1078. # underlying string for stringio.
  1079. #
  1080. # Rearranging for supporting byteio items, since they have a fileno
  1081. # that returns an OSError if there's no underlying fp. Easier to
  1082. # deal with here by reordering.
  1083. logger.debug("have getvalue. just sending in a string from getvalue")
  1084. n, err = decoder.decode(self.fp.getvalue())
  1085. elif fp:
  1086. # we've got a actual file on disk, pass in the fp.
  1087. logger.debug("have fileno, calling fileno version of the decoder.")
  1088. if not close_self_fp:
  1089. self.fp.seek(0)
  1090. # 4 bytes, otherwise the trace might error out
  1091. n, err = decoder.decode(b"fpfp")
  1092. else:
  1093. # we have something else.
  1094. logger.debug("don't have fileno or getvalue. just reading")
  1095. self.fp.seek(0)
  1096. # UNDONE -- so much for that buffer size thing.
  1097. n, err = decoder.decode(self.fp.read())
  1098. self.tile = []
  1099. self.readonly = 0
  1100. self.load_end()
  1101. if close_self_fp:
  1102. self.fp.close()
  1103. self.fp = None # might be shared
  1104. if err < 0:
  1105. raise OSError(err)
  1106. return Image.Image.load(self)
  1107. def _setup(self):
  1108. """Setup this image object based on current tags"""
  1109. if 0xBC01 in self.tag_v2:
  1110. msg = "Windows Media Photo files not yet supported"
  1111. raise OSError(msg)
  1112. # extract relevant tags
  1113. self._compression = COMPRESSION_INFO[self.tag_v2.get(COMPRESSION, 1)]
  1114. self._planar_configuration = self.tag_v2.get(PLANAR_CONFIGURATION, 1)
  1115. # photometric is a required tag, but not everyone is reading
  1116. # the specification
  1117. photo = self.tag_v2.get(PHOTOMETRIC_INTERPRETATION, 0)
  1118. # old style jpeg compression images most certainly are YCbCr
  1119. if self._compression == "tiff_jpeg":
  1120. photo = 6
  1121. fillorder = self.tag_v2.get(FILLORDER, 1)
  1122. logger.debug("*** Summary ***")
  1123. logger.debug("- compression: %s", self._compression)
  1124. logger.debug("- photometric_interpretation: %s", photo)
  1125. logger.debug("- planar_configuration: %s", self._planar_configuration)
  1126. logger.debug("- fill_order: %s", fillorder)
  1127. logger.debug("- YCbCr subsampling: %s", self.tag.get(YCBCRSUBSAMPLING))
  1128. # size
  1129. xsize = int(self.tag_v2.get(IMAGEWIDTH))
  1130. ysize = int(self.tag_v2.get(IMAGELENGTH))
  1131. self._size = xsize, ysize
  1132. logger.debug("- size: %s", self.size)
  1133. sample_format = self.tag_v2.get(SAMPLEFORMAT, (1,))
  1134. if len(sample_format) > 1 and max(sample_format) == min(sample_format) == 1:
  1135. # SAMPLEFORMAT is properly per band, so an RGB image will
  1136. # be (1,1,1). But, we don't support per band pixel types,
  1137. # and anything more than one band is a uint8. So, just
  1138. # take the first element. Revisit this if adding support
  1139. # for more exotic images.
  1140. sample_format = (1,)
  1141. bps_tuple = self.tag_v2.get(BITSPERSAMPLE, (1,))
  1142. extra_tuple = self.tag_v2.get(EXTRASAMPLES, ())
  1143. if photo in (2, 6, 8): # RGB, YCbCr, LAB
  1144. bps_count = 3
  1145. elif photo == 5: # CMYK
  1146. bps_count = 4
  1147. else:
  1148. bps_count = 1
  1149. bps_count += len(extra_tuple)
  1150. bps_actual_count = len(bps_tuple)
  1151. samples_per_pixel = self.tag_v2.get(
  1152. SAMPLESPERPIXEL,
  1153. 3 if self._compression == "tiff_jpeg" and photo in (2, 6) else 1,
  1154. )
  1155. if samples_per_pixel > MAX_SAMPLESPERPIXEL:
  1156. # DOS check, samples_per_pixel can be a Long, and we extend the tuple below
  1157. logger.error(
  1158. "More samples per pixel than can be decoded: %s", samples_per_pixel
  1159. )
  1160. msg = "Invalid value for samples per pixel"
  1161. raise SyntaxError(msg)
  1162. if samples_per_pixel < bps_actual_count:
  1163. # If a file has more values in bps_tuple than expected,
  1164. # remove the excess.
  1165. bps_tuple = bps_tuple[:samples_per_pixel]
  1166. elif samples_per_pixel > bps_actual_count and bps_actual_count == 1:
  1167. # If a file has only one value in bps_tuple, when it should have more,
  1168. # presume it is the same number of bits for all of the samples.
  1169. bps_tuple = bps_tuple * samples_per_pixel
  1170. if len(bps_tuple) != samples_per_pixel:
  1171. msg = "unknown data organization"
  1172. raise SyntaxError(msg)
  1173. # mode: check photometric interpretation and bits per pixel
  1174. key = (
  1175. self.tag_v2.prefix,
  1176. photo,
  1177. sample_format,
  1178. fillorder,
  1179. bps_tuple,
  1180. extra_tuple,
  1181. )
  1182. logger.debug("format key: %s", key)
  1183. try:
  1184. self._mode, rawmode = OPEN_INFO[key]
  1185. except KeyError as e:
  1186. logger.debug("- unsupported format")
  1187. msg = "unknown pixel mode"
  1188. raise SyntaxError(msg) from e
  1189. logger.debug("- raw mode: %s", rawmode)
  1190. logger.debug("- pil mode: %s", self.mode)
  1191. self.info["compression"] = self._compression
  1192. xres = self.tag_v2.get(X_RESOLUTION, 1)
  1193. yres = self.tag_v2.get(Y_RESOLUTION, 1)
  1194. if xres and yres:
  1195. resunit = self.tag_v2.get(RESOLUTION_UNIT)
  1196. if resunit == 2: # dots per inch
  1197. self.info["dpi"] = (xres, yres)
  1198. elif resunit == 3: # dots per centimeter. convert to dpi
  1199. self.info["dpi"] = (xres * 2.54, yres * 2.54)
  1200. elif resunit is None: # used to default to 1, but now 2)
  1201. self.info["dpi"] = (xres, yres)
  1202. # For backward compatibility,
  1203. # we also preserve the old behavior
  1204. self.info["resolution"] = xres, yres
  1205. else: # No absolute unit of measurement
  1206. self.info["resolution"] = xres, yres
  1207. # build tile descriptors
  1208. x = y = layer = 0
  1209. self.tile = []
  1210. self.use_load_libtiff = READ_LIBTIFF or self._compression != "raw"
  1211. if self.use_load_libtiff:
  1212. # Decoder expects entire file as one tile.
  1213. # There's a buffer size limit in load (64k)
  1214. # so large g4 images will fail if we use that
  1215. # function.
  1216. #
  1217. # Setup the one tile for the whole image, then
  1218. # use the _load_libtiff function.
  1219. # libtiff handles the fillmode for us, so 1;IR should
  1220. # actually be 1;I. Including the R double reverses the
  1221. # bits, so stripes of the image are reversed. See
  1222. # https://github.com/python-pillow/Pillow/issues/279
  1223. if fillorder == 2:
  1224. # Replace fillorder with fillorder=1
  1225. key = key[:3] + (1,) + key[4:]
  1226. logger.debug("format key: %s", key)
  1227. # this should always work, since all the
  1228. # fillorder==2 modes have a corresponding
  1229. # fillorder=1 mode
  1230. self._mode, rawmode = OPEN_INFO[key]
  1231. # libtiff always returns the bytes in native order.
  1232. # we're expecting image byte order. So, if the rawmode
  1233. # contains I;16, we need to convert from native to image
  1234. # byte order.
  1235. if rawmode == "I;16":
  1236. rawmode = "I;16N"
  1237. if ";16B" in rawmode:
  1238. rawmode = rawmode.replace(";16B", ";16N")
  1239. if ";16L" in rawmode:
  1240. rawmode = rawmode.replace(";16L", ";16N")
  1241. # YCbCr images with new jpeg compression with pixels in one plane
  1242. # unpacked straight into RGB values
  1243. if (
  1244. photo == 6
  1245. and self._compression == "jpeg"
  1246. and self._planar_configuration == 1
  1247. ):
  1248. rawmode = "RGB"
  1249. # Offset in the tile tuple is 0, we go from 0,0 to
  1250. # w,h, and we only do this once -- eds
  1251. a = (rawmode, self._compression, False, self.tag_v2.offset)
  1252. self.tile.append(("libtiff", (0, 0, xsize, ysize), 0, a))
  1253. elif STRIPOFFSETS in self.tag_v2 or TILEOFFSETS in self.tag_v2:
  1254. # striped image
  1255. if STRIPOFFSETS in self.tag_v2:
  1256. offsets = self.tag_v2[STRIPOFFSETS]
  1257. h = self.tag_v2.get(ROWSPERSTRIP, ysize)
  1258. w = self.size[0]
  1259. else:
  1260. # tiled image
  1261. offsets = self.tag_v2[TILEOFFSETS]
  1262. w = self.tag_v2.get(TILEWIDTH)
  1263. h = self.tag_v2.get(TILELENGTH)
  1264. for offset in offsets:
  1265. if x + w > xsize:
  1266. stride = w * sum(bps_tuple) / 8 # bytes per line
  1267. else:
  1268. stride = 0
  1269. tile_rawmode = rawmode
  1270. if self._planar_configuration == 2:
  1271. # each band on it's own layer
  1272. tile_rawmode = rawmode[layer]
  1273. # adjust stride width accordingly
  1274. stride /= bps_count
  1275. a = (tile_rawmode, int(stride), 1)
  1276. self.tile.append(
  1277. (
  1278. self._compression,
  1279. (x, y, min(x + w, xsize), min(y + h, ysize)),
  1280. offset,
  1281. a,
  1282. )
  1283. )
  1284. x = x + w
  1285. if x >= self.size[0]:
  1286. x, y = 0, y + h
  1287. if y >= self.size[1]:
  1288. x = y = 0
  1289. layer += 1
  1290. else:
  1291. logger.debug("- unsupported data organization")
  1292. msg = "unknown data organization"
  1293. raise SyntaxError(msg)
  1294. # Fix up info.
  1295. if ICCPROFILE in self.tag_v2:
  1296. self.info["icc_profile"] = self.tag_v2[ICCPROFILE]
  1297. # fixup palette descriptor
  1298. if self.mode in ["P", "PA"]:
  1299. palette = [o8(b // 256) for b in self.tag_v2[COLORMAP]]
  1300. self.palette = ImagePalette.raw("RGB;L", b"".join(palette))
  1301. #
  1302. # --------------------------------------------------------------------
  1303. # Write TIFF files
  1304. # little endian is default except for image modes with
  1305. # explicit big endian byte-order
  1306. SAVE_INFO = {
  1307. # mode => rawmode, byteorder, photometrics,
  1308. # sampleformat, bitspersample, extra
  1309. "1": ("1", II, 1, 1, (1,), None),
  1310. "L": ("L", II, 1, 1, (8,), None),
  1311. "LA": ("LA", II, 1, 1, (8, 8), 2),
  1312. "P": ("P", II, 3, 1, (8,), None),
  1313. "PA": ("PA", II, 3, 1, (8, 8), 2),
  1314. "I": ("I;32S", II, 1, 2, (32,), None),
  1315. "I;16": ("I;16", II, 1, 1, (16,), None),
  1316. "I;16S": ("I;16S", II, 1, 2, (16,), None),
  1317. "F": ("F;32F", II, 1, 3, (32,), None),
  1318. "RGB": ("RGB", II, 2, 1, (8, 8, 8), None),
  1319. "RGBX": ("RGBX", II, 2, 1, (8, 8, 8, 8), 0),
  1320. "RGBA": ("RGBA", II, 2, 1, (8, 8, 8, 8), 2),
  1321. "CMYK": ("CMYK", II, 5, 1, (8, 8, 8, 8), None),
  1322. "YCbCr": ("YCbCr", II, 6, 1, (8, 8, 8), None),
  1323. "LAB": ("LAB", II, 8, 1, (8, 8, 8), None),
  1324. "I;32BS": ("I;32BS", MM, 1, 2, (32,), None),
  1325. "I;16B": ("I;16B", MM, 1, 1, (16,), None),
  1326. "I;16BS": ("I;16BS", MM, 1, 2, (16,), None),
  1327. "F;32BF": ("F;32BF", MM, 1, 3, (32,), None),
  1328. }
  1329. def _save(im, fp, filename):
  1330. try:
  1331. rawmode, prefix, photo, format, bits, extra = SAVE_INFO[im.mode]
  1332. except KeyError as e:
  1333. msg = f"cannot write mode {im.mode} as TIFF"
  1334. raise OSError(msg) from e
  1335. ifd = ImageFileDirectory_v2(prefix=prefix)
  1336. encoderinfo = im.encoderinfo
  1337. encoderconfig = im.encoderconfig
  1338. try:
  1339. compression = encoderinfo["compression"]
  1340. except KeyError:
  1341. compression = im.info.get("compression")
  1342. if isinstance(compression, int):
  1343. # compression value may be from BMP. Ignore it
  1344. compression = None
  1345. if compression is None:
  1346. compression = "raw"
  1347. elif compression == "tiff_jpeg":
  1348. # OJPEG is obsolete, so use new-style JPEG compression instead
  1349. compression = "jpeg"
  1350. elif compression == "tiff_deflate":
  1351. compression = "tiff_adobe_deflate"
  1352. libtiff = WRITE_LIBTIFF or compression != "raw"
  1353. # required for color libtiff images
  1354. ifd[PLANAR_CONFIGURATION] = 1
  1355. ifd[IMAGEWIDTH] = im.size[0]
  1356. ifd[IMAGELENGTH] = im.size[1]
  1357. # write any arbitrary tags passed in as an ImageFileDirectory
  1358. if "tiffinfo" in encoderinfo:
  1359. info = encoderinfo["tiffinfo"]
  1360. elif "exif" in encoderinfo:
  1361. info = encoderinfo["exif"]
  1362. if isinstance(info, bytes):
  1363. exif = Image.Exif()
  1364. exif.load(info)
  1365. info = exif
  1366. else:
  1367. info = {}
  1368. logger.debug("Tiffinfo Keys: %s", list(info))
  1369. if isinstance(info, ImageFileDirectory_v1):
  1370. info = info.to_v2()
  1371. for key in info:
  1372. if isinstance(info, Image.Exif) and key in TiffTags.TAGS_V2_GROUPS:
  1373. ifd[key] = info.get_ifd(key)
  1374. else:
  1375. ifd[key] = info.get(key)
  1376. try:
  1377. ifd.tagtype[key] = info.tagtype[key]
  1378. except Exception:
  1379. pass # might not be an IFD. Might not have populated type
  1380. # additions written by Greg Couch, gregc@cgl.ucsf.edu
  1381. # inspired by image-sig posting from Kevin Cazabon, kcazabon@home.com
  1382. if hasattr(im, "tag_v2"):
  1383. # preserve tags from original TIFF image file
  1384. for key in (
  1385. RESOLUTION_UNIT,
  1386. X_RESOLUTION,
  1387. Y_RESOLUTION,
  1388. IPTC_NAA_CHUNK,
  1389. PHOTOSHOP_CHUNK,
  1390. XMP,
  1391. ):
  1392. if key in im.tag_v2:
  1393. ifd[key] = im.tag_v2[key]
  1394. ifd.tagtype[key] = im.tag_v2.tagtype[key]
  1395. # preserve ICC profile (should also work when saving other formats
  1396. # which support profiles as TIFF) -- 2008-06-06 Florian Hoech
  1397. icc = encoderinfo.get("icc_profile", im.info.get("icc_profile"))
  1398. if icc:
  1399. ifd[ICCPROFILE] = icc
  1400. for key, name in [
  1401. (IMAGEDESCRIPTION, "description"),
  1402. (X_RESOLUTION, "resolution"),
  1403. (Y_RESOLUTION, "resolution"),
  1404. (X_RESOLUTION, "x_resolution"),
  1405. (Y_RESOLUTION, "y_resolution"),
  1406. (RESOLUTION_UNIT, "resolution_unit"),
  1407. (SOFTWARE, "software"),
  1408. (DATE_TIME, "date_time"),
  1409. (ARTIST, "artist"),
  1410. (COPYRIGHT, "copyright"),
  1411. ]:
  1412. if name in encoderinfo:
  1413. ifd[key] = encoderinfo[name]
  1414. dpi = encoderinfo.get("dpi")
  1415. if dpi:
  1416. ifd[RESOLUTION_UNIT] = 2
  1417. ifd[X_RESOLUTION] = dpi[0]
  1418. ifd[Y_RESOLUTION] = dpi[1]
  1419. if bits != (1,):
  1420. ifd[BITSPERSAMPLE] = bits
  1421. if len(bits) != 1:
  1422. ifd[SAMPLESPERPIXEL] = len(bits)
  1423. if extra is not None:
  1424. ifd[EXTRASAMPLES] = extra
  1425. if format != 1:
  1426. ifd[SAMPLEFORMAT] = format
  1427. if PHOTOMETRIC_INTERPRETATION not in ifd:
  1428. ifd[PHOTOMETRIC_INTERPRETATION] = photo
  1429. elif im.mode in ("1", "L") and ifd[PHOTOMETRIC_INTERPRETATION] == 0:
  1430. if im.mode == "1":
  1431. inverted_im = im.copy()
  1432. px = inverted_im.load()
  1433. for y in range(inverted_im.height):
  1434. for x in range(inverted_im.width):
  1435. px[x, y] = 0 if px[x, y] == 255 else 255
  1436. im = inverted_im
  1437. else:
  1438. im = ImageOps.invert(im)
  1439. if im.mode in ["P", "PA"]:
  1440. lut = im.im.getpalette("RGB", "RGB;L")
  1441. colormap = []
  1442. colors = len(lut) // 3
  1443. for i in range(3):
  1444. colormap += [v * 256 for v in lut[colors * i : colors * (i + 1)]]
  1445. colormap += [0] * (256 - colors)
  1446. ifd[COLORMAP] = colormap
  1447. # data orientation
  1448. stride = len(bits) * ((im.size[0] * bits[0] + 7) // 8)
  1449. # aim for given strip size (64 KB by default) when using libtiff writer
  1450. if libtiff:
  1451. im_strip_size = encoderinfo.get("strip_size", STRIP_SIZE)
  1452. rows_per_strip = 1 if stride == 0 else min(im_strip_size // stride, im.size[1])
  1453. # JPEG encoder expects multiple of 8 rows
  1454. if compression == "jpeg":
  1455. rows_per_strip = min(((rows_per_strip + 7) // 8) * 8, im.size[1])
  1456. else:
  1457. rows_per_strip = im.size[1]
  1458. if rows_per_strip == 0:
  1459. rows_per_strip = 1
  1460. strip_byte_counts = 1 if stride == 0 else stride * rows_per_strip
  1461. strips_per_image = (im.size[1] + rows_per_strip - 1) // rows_per_strip
  1462. ifd[ROWSPERSTRIP] = rows_per_strip
  1463. if strip_byte_counts >= 2**16:
  1464. ifd.tagtype[STRIPBYTECOUNTS] = TiffTags.LONG
  1465. ifd[STRIPBYTECOUNTS] = (strip_byte_counts,) * (strips_per_image - 1) + (
  1466. stride * im.size[1] - strip_byte_counts * (strips_per_image - 1),
  1467. )
  1468. ifd[STRIPOFFSETS] = tuple(
  1469. range(0, strip_byte_counts * strips_per_image, strip_byte_counts)
  1470. ) # this is adjusted by IFD writer
  1471. # no compression by default:
  1472. ifd[COMPRESSION] = COMPRESSION_INFO_REV.get(compression, 1)
  1473. if im.mode == "YCbCr":
  1474. for tag, value in {
  1475. YCBCRSUBSAMPLING: (1, 1),
  1476. REFERENCEBLACKWHITE: (0, 255, 128, 255, 128, 255),
  1477. }.items():
  1478. ifd.setdefault(tag, value)
  1479. blocklist = [TILEWIDTH, TILELENGTH, TILEOFFSETS, TILEBYTECOUNTS]
  1480. if libtiff:
  1481. if "quality" in encoderinfo:
  1482. quality = encoderinfo["quality"]
  1483. if not isinstance(quality, int) or quality < 0 or quality > 100:
  1484. msg = "Invalid quality setting"
  1485. raise ValueError(msg)
  1486. if compression != "jpeg":
  1487. msg = "quality setting only supported for 'jpeg' compression"
  1488. raise ValueError(msg)
  1489. ifd[JPEGQUALITY] = quality
  1490. logger.debug("Saving using libtiff encoder")
  1491. logger.debug("Items: %s", sorted(ifd.items()))
  1492. _fp = 0
  1493. if hasattr(fp, "fileno"):
  1494. try:
  1495. fp.seek(0)
  1496. _fp = os.dup(fp.fileno())
  1497. except io.UnsupportedOperation:
  1498. pass
  1499. # optional types for non core tags
  1500. types = {}
  1501. # STRIPOFFSETS and STRIPBYTECOUNTS are added by the library
  1502. # based on the data in the strip.
  1503. # The other tags expect arrays with a certain length (fixed or depending on
  1504. # BITSPERSAMPLE, etc), passing arrays with a different length will result in
  1505. # segfaults. Block these tags until we add extra validation.
  1506. # SUBIFD may also cause a segfault.
  1507. blocklist += [
  1508. REFERENCEBLACKWHITE,
  1509. STRIPBYTECOUNTS,
  1510. STRIPOFFSETS,
  1511. TRANSFERFUNCTION,
  1512. SUBIFD,
  1513. ]
  1514. # bits per sample is a single short in the tiff directory, not a list.
  1515. atts = {BITSPERSAMPLE: bits[0]}
  1516. # Merge the ones that we have with (optional) more bits from
  1517. # the original file, e.g x,y resolution so that we can
  1518. # save(load('')) == original file.
  1519. legacy_ifd = {}
  1520. if hasattr(im, "tag"):
  1521. legacy_ifd = im.tag.to_v2()
  1522. # SAMPLEFORMAT is determined by the image format and should not be copied
  1523. # from legacy_ifd.
  1524. supplied_tags = {**getattr(im, "tag_v2", {}), **legacy_ifd}
  1525. if SAMPLEFORMAT in supplied_tags:
  1526. del supplied_tags[SAMPLEFORMAT]
  1527. for tag, value in itertools.chain(ifd.items(), supplied_tags.items()):
  1528. # Libtiff can only process certain core items without adding
  1529. # them to the custom dictionary.
  1530. # Custom items are supported for int, float, unicode, string and byte
  1531. # values. Other types and tuples require a tagtype.
  1532. if tag not in TiffTags.LIBTIFF_CORE:
  1533. if not getattr(Image.core, "libtiff_support_custom_tags", False):
  1534. continue
  1535. if tag in ifd.tagtype:
  1536. types[tag] = ifd.tagtype[tag]
  1537. elif not (isinstance(value, (int, float, str, bytes))):
  1538. continue
  1539. else:
  1540. type = TiffTags.lookup(tag).type
  1541. if type:
  1542. types[tag] = type
  1543. if tag not in atts and tag not in blocklist:
  1544. if isinstance(value, str):
  1545. atts[tag] = value.encode("ascii", "replace") + b"\0"
  1546. elif isinstance(value, IFDRational):
  1547. atts[tag] = float(value)
  1548. else:
  1549. atts[tag] = value
  1550. if SAMPLEFORMAT in atts and len(atts[SAMPLEFORMAT]) == 1:
  1551. atts[SAMPLEFORMAT] = atts[SAMPLEFORMAT][0]
  1552. logger.debug("Converted items: %s", sorted(atts.items()))
  1553. # libtiff always expects the bytes in native order.
  1554. # we're storing image byte order. So, if the rawmode
  1555. # contains I;16, we need to convert from native to image
  1556. # byte order.
  1557. if im.mode in ("I;16B", "I;16"):
  1558. rawmode = "I;16N"
  1559. # Pass tags as sorted list so that the tags are set in a fixed order.
  1560. # This is required by libtiff for some tags. For example, the JPEGQUALITY
  1561. # pseudo tag requires that the COMPRESS tag was already set.
  1562. tags = list(atts.items())
  1563. tags.sort()
  1564. a = (rawmode, compression, _fp, filename, tags, types)
  1565. e = Image._getencoder(im.mode, "libtiff", a, encoderconfig)
  1566. e.setimage(im.im, (0, 0) + im.size)
  1567. while True:
  1568. # undone, change to self.decodermaxblock:
  1569. errcode, data = e.encode(16 * 1024)[1:]
  1570. if not _fp:
  1571. fp.write(data)
  1572. if errcode:
  1573. break
  1574. if _fp:
  1575. try:
  1576. os.close(_fp)
  1577. except OSError:
  1578. pass
  1579. if errcode < 0:
  1580. msg = f"encoder error {errcode} when writing image file"
  1581. raise OSError(msg)
  1582. else:
  1583. for tag in blocklist:
  1584. del ifd[tag]
  1585. offset = ifd.save(fp)
  1586. ImageFile._save(
  1587. im, fp, [("raw", (0, 0) + im.size, offset, (rawmode, stride, 1))]
  1588. )
  1589. # -- helper for multi-page save --
  1590. if "_debug_multipage" in encoderinfo:
  1591. # just to access o32 and o16 (using correct byte order)
  1592. im._debug_multipage = ifd
  1593. class AppendingTiffWriter:
  1594. fieldSizes = [
  1595. 0, # None
  1596. 1, # byte
  1597. 1, # ascii
  1598. 2, # short
  1599. 4, # long
  1600. 8, # rational
  1601. 1, # sbyte
  1602. 1, # undefined
  1603. 2, # sshort
  1604. 4, # slong
  1605. 8, # srational
  1606. 4, # float
  1607. 8, # double
  1608. 4, # ifd
  1609. 2, # unicode
  1610. 4, # complex
  1611. 8, # long8
  1612. ]
  1613. # StripOffsets = 273
  1614. # FreeOffsets = 288
  1615. # TileOffsets = 324
  1616. # JPEGQTables = 519
  1617. # JPEGDCTables = 520
  1618. # JPEGACTables = 521
  1619. Tags = {273, 288, 324, 519, 520, 521}
  1620. def __init__(self, fn, new=False):
  1621. if hasattr(fn, "read"):
  1622. self.f = fn
  1623. self.close_fp = False
  1624. else:
  1625. self.name = fn
  1626. self.close_fp = True
  1627. try:
  1628. self.f = open(fn, "w+b" if new else "r+b")
  1629. except OSError:
  1630. self.f = open(fn, "w+b")
  1631. self.beginning = self.f.tell()
  1632. self.setup()
  1633. def setup(self):
  1634. # Reset everything.
  1635. self.f.seek(self.beginning, os.SEEK_SET)
  1636. self.whereToWriteNewIFDOffset = None
  1637. self.offsetOfNewPage = 0
  1638. self.IIMM = iimm = self.f.read(4)
  1639. if not iimm:
  1640. # empty file - first page
  1641. self.isFirst = True
  1642. return
  1643. self.isFirst = False
  1644. if iimm == b"II\x2a\x00":
  1645. self.setEndian("<")
  1646. elif iimm == b"MM\x00\x2a":
  1647. self.setEndian(">")
  1648. else:
  1649. msg = "Invalid TIFF file header"
  1650. raise RuntimeError(msg)
  1651. self.skipIFDs()
  1652. self.goToEnd()
  1653. def finalize(self):
  1654. if self.isFirst:
  1655. return
  1656. # fix offsets
  1657. self.f.seek(self.offsetOfNewPage)
  1658. iimm = self.f.read(4)
  1659. if not iimm:
  1660. # msg = "nothing written into new page"
  1661. # raise RuntimeError(msg)
  1662. # Make it easy to finish a frame without committing to a new one.
  1663. return
  1664. if iimm != self.IIMM:
  1665. msg = "IIMM of new page doesn't match IIMM of first page"
  1666. raise RuntimeError(msg)
  1667. ifd_offset = self.readLong()
  1668. ifd_offset += self.offsetOfNewPage
  1669. self.f.seek(self.whereToWriteNewIFDOffset)
  1670. self.writeLong(ifd_offset)
  1671. self.f.seek(ifd_offset)
  1672. self.fixIFD()
  1673. def newFrame(self):
  1674. # Call this to finish a frame.
  1675. self.finalize()
  1676. self.setup()
  1677. def __enter__(self):
  1678. return self
  1679. def __exit__(self, exc_type, exc_value, traceback):
  1680. if self.close_fp:
  1681. self.close()
  1682. return False
  1683. def tell(self):
  1684. return self.f.tell() - self.offsetOfNewPage
  1685. def seek(self, offset, whence=io.SEEK_SET):
  1686. if whence == os.SEEK_SET:
  1687. offset += self.offsetOfNewPage
  1688. self.f.seek(offset, whence)
  1689. return self.tell()
  1690. def goToEnd(self):
  1691. self.f.seek(0, os.SEEK_END)
  1692. pos = self.f.tell()
  1693. # pad to 16 byte boundary
  1694. pad_bytes = 16 - pos % 16
  1695. if 0 < pad_bytes < 16:
  1696. self.f.write(bytes(pad_bytes))
  1697. self.offsetOfNewPage = self.f.tell()
  1698. def setEndian(self, endian):
  1699. self.endian = endian
  1700. self.longFmt = self.endian + "L"
  1701. self.shortFmt = self.endian + "H"
  1702. self.tagFormat = self.endian + "HHL"
  1703. def skipIFDs(self):
  1704. while True:
  1705. ifd_offset = self.readLong()
  1706. if ifd_offset == 0:
  1707. self.whereToWriteNewIFDOffset = self.f.tell() - 4
  1708. break
  1709. self.f.seek(ifd_offset)
  1710. num_tags = self.readShort()
  1711. self.f.seek(num_tags * 12, os.SEEK_CUR)
  1712. def write(self, data):
  1713. return self.f.write(data)
  1714. def readShort(self):
  1715. (value,) = struct.unpack(self.shortFmt, self.f.read(2))
  1716. return value
  1717. def readLong(self):
  1718. (value,) = struct.unpack(self.longFmt, self.f.read(4))
  1719. return value
  1720. def rewriteLastShortToLong(self, value):
  1721. self.f.seek(-2, os.SEEK_CUR)
  1722. bytes_written = self.f.write(struct.pack(self.longFmt, value))
  1723. if bytes_written is not None and bytes_written != 4:
  1724. msg = f"wrote only {bytes_written} bytes but wanted 4"
  1725. raise RuntimeError(msg)
  1726. def rewriteLastShort(self, value):
  1727. self.f.seek(-2, os.SEEK_CUR)
  1728. bytes_written = self.f.write(struct.pack(self.shortFmt, value))
  1729. if bytes_written is not None and bytes_written != 2:
  1730. msg = f"wrote only {bytes_written} bytes but wanted 2"
  1731. raise RuntimeError(msg)
  1732. def rewriteLastLong(self, value):
  1733. self.f.seek(-4, os.SEEK_CUR)
  1734. bytes_written = self.f.write(struct.pack(self.longFmt, value))
  1735. if bytes_written is not None and bytes_written != 4:
  1736. msg = f"wrote only {bytes_written} bytes but wanted 4"
  1737. raise RuntimeError(msg)
  1738. def writeShort(self, value):
  1739. bytes_written = self.f.write(struct.pack(self.shortFmt, value))
  1740. if bytes_written is not None and bytes_written != 2:
  1741. msg = f"wrote only {bytes_written} bytes but wanted 2"
  1742. raise RuntimeError(msg)
  1743. def writeLong(self, value):
  1744. bytes_written = self.f.write(struct.pack(self.longFmt, value))
  1745. if bytes_written is not None and bytes_written != 4:
  1746. msg = f"wrote only {bytes_written} bytes but wanted 4"
  1747. raise RuntimeError(msg)
  1748. def close(self):
  1749. self.finalize()
  1750. self.f.close()
  1751. def fixIFD(self):
  1752. num_tags = self.readShort()
  1753. for i in range(num_tags):
  1754. tag, field_type, count = struct.unpack(self.tagFormat, self.f.read(8))
  1755. field_size = self.fieldSizes[field_type]
  1756. total_size = field_size * count
  1757. is_local = total_size <= 4
  1758. if not is_local:
  1759. offset = self.readLong()
  1760. offset += self.offsetOfNewPage
  1761. self.rewriteLastLong(offset)
  1762. if tag in self.Tags:
  1763. cur_pos = self.f.tell()
  1764. if is_local:
  1765. self.fixOffsets(
  1766. count, isShort=(field_size == 2), isLong=(field_size == 4)
  1767. )
  1768. self.f.seek(cur_pos + 4)
  1769. else:
  1770. self.f.seek(offset)
  1771. self.fixOffsets(
  1772. count, isShort=(field_size == 2), isLong=(field_size == 4)
  1773. )
  1774. self.f.seek(cur_pos)
  1775. offset = cur_pos = None
  1776. elif is_local:
  1777. # skip the locally stored value that is not an offset
  1778. self.f.seek(4, os.SEEK_CUR)
  1779. def fixOffsets(self, count, isShort=False, isLong=False):
  1780. if not isShort and not isLong:
  1781. msg = "offset is neither short nor long"
  1782. raise RuntimeError(msg)
  1783. for i in range(count):
  1784. offset = self.readShort() if isShort else self.readLong()
  1785. offset += self.offsetOfNewPage
  1786. if isShort and offset >= 65536:
  1787. # offset is now too large - we must convert shorts to longs
  1788. if count != 1:
  1789. msg = "not implemented"
  1790. raise RuntimeError(msg) # XXX TODO
  1791. # simple case - the offset is just one and therefore it is
  1792. # local (not referenced with another offset)
  1793. self.rewriteLastShortToLong(offset)
  1794. self.f.seek(-10, os.SEEK_CUR)
  1795. self.writeShort(TiffTags.LONG) # rewrite the type to LONG
  1796. self.f.seek(8, os.SEEK_CUR)
  1797. elif isShort:
  1798. self.rewriteLastShort(offset)
  1799. else:
  1800. self.rewriteLastLong(offset)
  1801. def _save_all(im, fp, filename):
  1802. encoderinfo = im.encoderinfo.copy()
  1803. encoderconfig = im.encoderconfig
  1804. append_images = list(encoderinfo.get("append_images", []))
  1805. if not hasattr(im, "n_frames") and not append_images:
  1806. return _save(im, fp, filename)
  1807. cur_idx = im.tell()
  1808. try:
  1809. with AppendingTiffWriter(fp) as tf:
  1810. for ims in [im] + append_images:
  1811. ims.encoderinfo = encoderinfo
  1812. ims.encoderconfig = encoderconfig
  1813. if not hasattr(ims, "n_frames"):
  1814. nfr = 1
  1815. else:
  1816. nfr = ims.n_frames
  1817. for idx in range(nfr):
  1818. ims.seek(idx)
  1819. ims.load()
  1820. _save(ims, tf, filename)
  1821. tf.newFrame()
  1822. finally:
  1823. im.seek(cur_idx)
  1824. #
  1825. # --------------------------------------------------------------------
  1826. # Register
  1827. Image.register_open(TiffImageFile.format, TiffImageFile, _accept)
  1828. Image.register_save(TiffImageFile.format, _save)
  1829. Image.register_save_all(TiffImageFile.format, _save_all)
  1830. Image.register_extensions(TiffImageFile.format, [".tif", ".tiff"])
  1831. Image.register_mime(TiffImageFile.format, "image/tiff")