Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/PIL/ImageChops.py: 28%

77 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1# 

2# The Python Imaging Library. 

3# $Id$ 

4# 

5# standard channel operations 

6# 

7# History: 

8# 1996-03-24 fl Created 

9# 1996-08-13 fl Added logical operations (for "1" images) 

10# 2000-10-12 fl Added offset method (from Image.py) 

11# 

12# Copyright (c) 1997-2000 by Secret Labs AB 

13# Copyright (c) 1996-2000 by Fredrik Lundh 

14# 

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

16# 

17 

18from . import Image 

19 

20 

21def constant(image, value): 

22 """Fill a channel with a given grey level. 

23 

24 :rtype: :py:class:`~PIL.Image.Image` 

25 """ 

26 

27 return Image.new("L", image.size, value) 

28 

29 

30def duplicate(image): 

31 """Copy a channel. Alias for :py:meth:`PIL.Image.Image.copy`. 

32 

33 :rtype: :py:class:`~PIL.Image.Image` 

34 """ 

35 

36 return image.copy() 

37 

38 

39def invert(image): 

40 """ 

41 Invert an image (channel). 

42 

43 .. code-block:: python 

44 

45 out = MAX - image 

46 

47 :rtype: :py:class:`~PIL.Image.Image` 

48 """ 

49 

50 image.load() 

51 return image._new(image.im.chop_invert()) 

52 

53 

54def lighter(image1, image2): 

55 """ 

56 Compares the two images, pixel by pixel, and returns a new image containing 

57 the lighter values. 

58 

59 .. code-block:: python 

60 

61 out = max(image1, image2) 

62 

63 :rtype: :py:class:`~PIL.Image.Image` 

64 """ 

65 

66 image1.load() 

67 image2.load() 

68 return image1._new(image1.im.chop_lighter(image2.im)) 

69 

70 

71def darker(image1, image2): 

72 """ 

73 Compares the two images, pixel by pixel, and returns a new image containing 

74 the darker values. 

75 

76 .. code-block:: python 

77 

78 out = min(image1, image2) 

79 

80 :rtype: :py:class:`~PIL.Image.Image` 

81 """ 

82 

83 image1.load() 

84 image2.load() 

85 return image1._new(image1.im.chop_darker(image2.im)) 

86 

87 

88def difference(image1, image2): 

89 """ 

90 Returns the absolute value of the pixel-by-pixel difference between the two 

91 images. 

92 

93 .. code-block:: python 

94 

95 out = abs(image1 - image2) 

96 

97 :rtype: :py:class:`~PIL.Image.Image` 

98 """ 

99 

100 image1.load() 

101 image2.load() 

102 return image1._new(image1.im.chop_difference(image2.im)) 

103 

104 

105def multiply(image1, image2): 

106 """ 

107 Superimposes two images on top of each other. 

108 

109 If you multiply an image with a solid black image, the result is black. If 

110 you multiply with a solid white image, the image is unaffected. 

111 

112 .. code-block:: python 

113 

114 out = image1 * image2 / MAX 

115 

116 :rtype: :py:class:`~PIL.Image.Image` 

117 """ 

118 

119 image1.load() 

120 image2.load() 

121 return image1._new(image1.im.chop_multiply(image2.im)) 

122 

123 

124def screen(image1, image2): 

125 """ 

126 Superimposes two inverted images on top of each other. 

127 

128 .. code-block:: python 

129 

130 out = MAX - ((MAX - image1) * (MAX - image2) / MAX) 

131 

132 :rtype: :py:class:`~PIL.Image.Image` 

133 """ 

134 

135 image1.load() 

136 image2.load() 

137 return image1._new(image1.im.chop_screen(image2.im)) 

138 

139 

140def soft_light(image1, image2): 

141 """ 

142 Superimposes two images on top of each other using the Soft Light algorithm 

143 

144 :rtype: :py:class:`~PIL.Image.Image` 

145 """ 

146 

147 image1.load() 

148 image2.load() 

149 return image1._new(image1.im.chop_soft_light(image2.im)) 

150 

151 

152def hard_light(image1, image2): 

153 """ 

154 Superimposes two images on top of each other using the Hard Light algorithm 

155 

156 :rtype: :py:class:`~PIL.Image.Image` 

157 """ 

158 

159 image1.load() 

160 image2.load() 

161 return image1._new(image1.im.chop_hard_light(image2.im)) 

162 

163 

164def overlay(image1, image2): 

165 """ 

166 Superimposes two images on top of each other using the Overlay algorithm 

167 

168 :rtype: :py:class:`~PIL.Image.Image` 

169 """ 

170 

171 image1.load() 

172 image2.load() 

173 return image1._new(image1.im.chop_overlay(image2.im)) 

174 

175 

176def add(image1, image2, scale=1.0, offset=0): 

177 """ 

178 Adds two images, dividing the result by scale and adding the 

179 offset. If omitted, scale defaults to 1.0, and offset to 0.0. 

180 

181 .. code-block:: python 

182 

183 out = ((image1 + image2) / scale + offset) 

184 

185 :rtype: :py:class:`~PIL.Image.Image` 

186 """ 

187 

188 image1.load() 

189 image2.load() 

190 return image1._new(image1.im.chop_add(image2.im, scale, offset)) 

191 

192 

193def subtract(image1, image2, scale=1.0, offset=0): 

194 """ 

195 Subtracts two images, dividing the result by scale and adding the offset. 

196 If omitted, scale defaults to 1.0, and offset to 0.0. 

197 

198 .. code-block:: python 

199 

200 out = ((image1 - image2) / scale + offset) 

201 

202 :rtype: :py:class:`~PIL.Image.Image` 

203 """ 

204 

205 image1.load() 

206 image2.load() 

207 return image1._new(image1.im.chop_subtract(image2.im, scale, offset)) 

208 

209 

210def add_modulo(image1, image2): 

211 """Add two images, without clipping the result. 

212 

213 .. code-block:: python 

214 

215 out = ((image1 + image2) % MAX) 

216 

217 :rtype: :py:class:`~PIL.Image.Image` 

218 """ 

219 

220 image1.load() 

221 image2.load() 

222 return image1._new(image1.im.chop_add_modulo(image2.im)) 

223 

224 

225def subtract_modulo(image1, image2): 

226 """Subtract two images, without clipping the result. 

227 

228 .. code-block:: python 

229 

230 out = ((image1 - image2) % MAX) 

231 

232 :rtype: :py:class:`~PIL.Image.Image` 

233 """ 

234 

235 image1.load() 

236 image2.load() 

237 return image1._new(image1.im.chop_subtract_modulo(image2.im)) 

238 

239 

240def logical_and(image1, image2): 

241 """Logical AND between two images. 

242 

243 Both of the images must have mode "1". If you would like to perform a 

244 logical AND on an image with a mode other than "1", try 

245 :py:meth:`~PIL.ImageChops.multiply` instead, using a black-and-white mask 

246 as the second image. 

247 

248 .. code-block:: python 

249 

250 out = ((image1 and image2) % MAX) 

251 

252 :rtype: :py:class:`~PIL.Image.Image` 

253 """ 

254 

255 image1.load() 

256 image2.load() 

257 return image1._new(image1.im.chop_and(image2.im)) 

258 

259 

260def logical_or(image1, image2): 

261 """Logical OR between two images. 

262 

263 Both of the images must have mode "1". 

264 

265 .. code-block:: python 

266 

267 out = ((image1 or image2) % MAX) 

268 

269 :rtype: :py:class:`~PIL.Image.Image` 

270 """ 

271 

272 image1.load() 

273 image2.load() 

274 return image1._new(image1.im.chop_or(image2.im)) 

275 

276 

277def logical_xor(image1, image2): 

278 """Logical XOR between two images. 

279 

280 Both of the images must have mode "1". 

281 

282 .. code-block:: python 

283 

284 out = ((bool(image1) != bool(image2)) % MAX) 

285 

286 :rtype: :py:class:`~PIL.Image.Image` 

287 """ 

288 

289 image1.load() 

290 image2.load() 

291 return image1._new(image1.im.chop_xor(image2.im)) 

292 

293 

294def blend(image1, image2, alpha): 

295 """Blend images using constant transparency weight. Alias for 

296 :py:func:`PIL.Image.blend`. 

297 

298 :rtype: :py:class:`~PIL.Image.Image` 

299 """ 

300 

301 return Image.blend(image1, image2, alpha) 

302 

303 

304def composite(image1, image2, mask): 

305 """Create composite using transparency mask. Alias for 

306 :py:func:`PIL.Image.composite`. 

307 

308 :rtype: :py:class:`~PIL.Image.Image` 

309 """ 

310 

311 return Image.composite(image1, image2, mask) 

312 

313 

314def offset(image, xoffset, yoffset=None): 

315 """Returns a copy of the image where data has been offset by the given 

316 distances. Data wraps around the edges. If ``yoffset`` is omitted, it 

317 is assumed to be equal to ``xoffset``. 

318 

319 :param image: Input image. 

320 :param xoffset: The horizontal distance. 

321 :param yoffset: The vertical distance. If omitted, both 

322 distances are set to the same value. 

323 :rtype: :py:class:`~PIL.Image.Image` 

324 """ 

325 

326 if yoffset is None: 

327 yoffset = xoffset 

328 image.load() 

329 return image._new(image.im.offset(xoffset, yoffset))