Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/IptcImagePlugin.py: 35%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

132 statements  

1# 

2# The Python Imaging Library. 

3# $Id$ 

4# 

5# IPTC/NAA file handling 

6# 

7# history: 

8# 1995-10-01 fl Created 

9# 1998-03-09 fl Cleaned up and added to PIL 

10# 2002-06-18 fl Added getiptcinfo helper 

11# 

12# Copyright (c) Secret Labs AB 1997-2002. 

13# Copyright (c) Fredrik Lundh 1995. 

14# 

15# See the README file for information on usage and redistribution. 

16# 

17from __future__ import annotations 

18 

19from io import BytesIO 

20from typing import cast 

21 

22from . import Image, ImageFile 

23from ._binary import i16be as i16 

24from ._binary import i32be as i32 

25 

26COMPRESSION = {1: "raw", 5: "jpeg"} 

27 

28 

29# 

30# Helpers 

31 

32 

33def _i(c: bytes) -> int: 

34 return i32((b"\0\0\0\0" + c)[-4:]) 

35 

36 

37## 

38# Image plugin for IPTC/NAA datastreams. To read IPTC/NAA fields 

39# from TIFF and JPEG files, use the <b>getiptcinfo</b> function. 

40 

41 

42class IptcImageFile(ImageFile.ImageFile): 

43 format = "IPTC" 

44 format_description = "IPTC/NAA" 

45 

46 def getint(self, key: tuple[int, int]) -> int: 

47 return _i(self.info[key]) 

48 

49 def field(self) -> tuple[tuple[int, int] | None, int]: 

50 # 

51 # get a IPTC field header 

52 s = self.fp.read(5) 

53 if not s.strip(b"\x00"): 

54 return None, 0 

55 

56 tag = s[1], s[2] 

57 

58 # syntax 

59 if s[0] != 0x1C or tag[0] not in [1, 2, 3, 4, 5, 6, 7, 8, 9, 240]: 

60 msg = "invalid IPTC/NAA file" 

61 raise SyntaxError(msg) 

62 

63 # field size 

64 size = s[3] 

65 if size > 132: 

66 msg = "illegal field length in IPTC/NAA file" 

67 raise OSError(msg) 

68 elif size == 128: 

69 size = 0 

70 elif size > 128: 

71 size = _i(self.fp.read(size - 128)) 

72 else: 

73 size = i16(s, 3) 

74 

75 return tag, size 

76 

77 def _open(self) -> None: 

78 # load descriptive fields 

79 while True: 

80 offset = self.fp.tell() 

81 tag, size = self.field() 

82 if not tag or tag == (8, 10): 

83 break 

84 if size: 

85 tagdata = self.fp.read(size) 

86 else: 

87 tagdata = None 

88 if tag in self.info: 

89 if isinstance(self.info[tag], list): 

90 self.info[tag].append(tagdata) 

91 else: 

92 self.info[tag] = [self.info[tag], tagdata] 

93 else: 

94 self.info[tag] = tagdata 

95 

96 # mode 

97 layers = self.info[(3, 60)][0] 

98 component = self.info[(3, 60)][1] 

99 if layers == 1 and not component: 

100 self._mode = "L" 

101 band = None 

102 else: 

103 if layers == 3 and component: 

104 self._mode = "RGB" 

105 elif layers == 4 and component: 

106 self._mode = "CMYK" 

107 if (3, 65) in self.info: 

108 band = self.info[(3, 65)][0] - 1 

109 else: 

110 band = 0 

111 

112 # size 

113 self._size = self.getint((3, 20)), self.getint((3, 30)) 

114 

115 # compression 

116 try: 

117 compression = COMPRESSION[self.getint((3, 120))] 

118 except KeyError as e: 

119 msg = "Unknown IPTC image compression" 

120 raise OSError(msg) from e 

121 

122 # tile 

123 if tag == (8, 10): 

124 self.tile = [ 

125 ImageFile._Tile("iptc", (0, 0) + self.size, offset, (compression, band)) 

126 ] 

127 

128 def load(self) -> Image.core.PixelAccess | None: 

129 if self.tile: 

130 args = self.tile[0].args 

131 assert isinstance(args, tuple) 

132 compression, band = args 

133 

134 self.fp.seek(self.tile[0].offset) 

135 

136 # Copy image data to temporary file 

137 o = BytesIO() 

138 if compression == "raw": 

139 # To simplify access to the extracted file, 

140 # prepend a PPM header 

141 o.write(b"P5\n%d %d\n255\n" % self.size) 

142 while True: 

143 type, size = self.field() 

144 if type != (8, 10): 

145 break 

146 while size > 0: 

147 s = self.fp.read(min(size, 8192)) 

148 if not s: 

149 break 

150 o.write(s) 

151 size -= len(s) 

152 

153 with Image.open(o) as _im: 

154 if band is not None: 

155 bands = [Image.new("L", _im.size)] * Image.getmodebands(self.mode) 

156 bands[band] = _im 

157 _im = Image.merge(self.mode, bands) 

158 else: 

159 _im.load() 

160 self.im = _im.im 

161 self.tile = [] 

162 return ImageFile.ImageFile.load(self) 

163 

164 

165Image.register_open(IptcImageFile.format, IptcImageFile) 

166 

167Image.register_extension(IptcImageFile.format, ".iim") 

168 

169 

170def getiptcinfo( 

171 im: ImageFile.ImageFile, 

172) -> dict[tuple[int, int], bytes | list[bytes]] | None: 

173 """ 

174 Get IPTC information from TIFF, JPEG, or IPTC file. 

175 

176 :param im: An image containing IPTC data. 

177 :returns: A dictionary containing IPTC information, or None if 

178 no IPTC information block was found. 

179 """ 

180 from . import JpegImagePlugin, TiffImagePlugin 

181 

182 data = None 

183 

184 info: dict[tuple[int, int], bytes | list[bytes]] = {} 

185 if isinstance(im, IptcImageFile): 

186 # return info dictionary right away 

187 for k, v in im.info.items(): 

188 if isinstance(k, tuple): 

189 info[k] = v 

190 return info 

191 

192 elif isinstance(im, JpegImagePlugin.JpegImageFile): 

193 # extract the IPTC/NAA resource 

194 photoshop = im.info.get("photoshop") 

195 if photoshop: 

196 data = photoshop.get(0x0404) 

197 

198 elif isinstance(im, TiffImagePlugin.TiffImageFile): 

199 # get raw data from the IPTC/NAA tag (PhotoShop tags the data 

200 # as 4-byte integers, so we cannot use the get method...) 

201 try: 

202 data = im.tag_v2._tagdata[TiffImagePlugin.IPTC_NAA_CHUNK] 

203 except KeyError: 

204 pass 

205 

206 if data is None: 

207 return None # no properties 

208 

209 # create an IptcImagePlugin object without initializing it 

210 class FakeImage: 

211 pass 

212 

213 fake_im = FakeImage() 

214 fake_im.__class__ = IptcImageFile # type: ignore[assignment] 

215 iptc_im = cast(IptcImageFile, fake_im) 

216 

217 # parse the IPTC information chunk 

218 iptc_im.info = {} 

219 iptc_im.fp = BytesIO(data) 

220 

221 try: 

222 iptc_im._open() 

223 except (IndexError, KeyError): 

224 pass # expected failure 

225 

226 for k, v in iptc_im.info.items(): 

227 if isinstance(k, tuple): 

228 info[k] = v 

229 return info