TiffImagePlugin.py 81 KB

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