zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
U
T±dVTã@sddlZddlZddlZddlmZmZdd„Zdd„Zdd    „Zd2d d „Z    d3dd„Z
ej j fdd„Z ej j ddfdd„Zd4dd„Zej j fdd„Zej jfdd„Zd5dd„Zd6dd„Zej j d dfd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„Zd7d.d/„Zd0d1„ZdS)8éNé)ÚImageÚ ImagePalettecCs^t|tƒrBt|ƒdkr(|\}}\}}qRt|ƒdkrR|\}}}}n|}}}}||||fS)Néé)Ú
isinstanceÚtupleÚlen)ÚborderÚleftÚtopÚrightÚbottom©rúCd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\PIL/ImageOps.pyÚ_borders
  rcCs&t|tƒr"ddlm}| ||¡}|S)Nr)Ú
ImageColor)rÚstrÚrÚgetcolor)ÚcolorÚmoderrrrÚ_color)s
  rcCs^|jdkrd}t|ƒ‚nB|jdkrN|jdkrDt|ƒdkrD|||}| |¡Sd}t|ƒ‚dS)NÚPzmode P support coming soon)ÚLÚRGBréz!not supported for this image mode)rÚNotImplementedErrorr    ÚpointÚOSError)ÚimageÚlutÚmsgrrrÚ_lut1s
 
 
 
r#Fc    CsV|r| d¡ |¡}n
| |¡}g}tdt|ƒdƒD]}|||d…}|dk    r†z d||<Wn&tk
r„|D] }    d||    <qrYnX|rŠt|tƒsž||f}d}
tdƒD]}    |
||    }
qª|
|dd} tdƒD]H} | || krú| || } d|| <n|| | 8<d} | dkrԐqqÔ|
|dd} tdddƒD]N} | || krb| || } d|| <n|| | 8<d} | dkr:qАq:tdƒD]} || r’qªq’tdddƒD]} || r¶qΐq¶| | krì| ttdƒƒ¡q4d    | | }| |}tdƒD]>}    t    |    ||ƒ}    |    dkr.d}    n|    dkr<d}    | 
|    ¡q
q4t ||ƒS)
aÙ
    Maximize (normalize) image contrast. This function calculates a
    histogram of the input image (or mask region), removes ``cutoff`` percent of the
    lightest and darkest pixels from the histogram, and remaps the image
    so that the darkest pixel becomes black (0), and the lightest
    becomes white (255).
 
    :param image: The image to process.
    :param cutoff: The percent to cut off from the histogram on the low and
                   high ends. Either a tuple of (low, high), or a single
                   number for both.
    :param ignore: The background pixel value (use None for no background).
    :param mask: Histogram used in contrast operation is computed using pixels
                 within the mask. If no mask is given the entire image is used
                 for histogram computation.
    :param preserve_tone: Preserve image tone in Photoshop-like style autocontrast.
 
                          .. versionadded:: 8.2.0
 
    :return: An image.
    rrrNédréÿéÿÿÿÿgào@) ÚconvertÚ    histogramÚranger    Ú    TypeErrorrrÚextendÚlistÚintÚappendr#)r ÚcutoffÚignoreÚmaskZ preserve_toner(r!ZlayerÚhÚixÚnZcutÚloÚhiÚscaleÚoffsetrrrÚ autocontrastCsl
 
 
 
 
 
 
 
 
 
 
 
 
 
r9r%écCs|jdkst‚|dkr<d|kr4|kr4dksjnt‚n.d|krd|krd|krddksjnt‚t|dƒ}t|dƒ}|dk    rt|dƒ}g}g}g}    td|ƒD].}
| |d¡| |d¡|     |d¡q¦|dkrztd||ƒ} | D]‚}
| |d|
|d|dt| ƒ¡| |d|
|d|dt| ƒ¡|     |d|
|d|dt| ƒ¡qòn0td||ƒ} td||ƒ} | D]„}
| |d|
|d|dt| ƒ¡| |d|
|d|dt| ƒ¡|     |d|
|d|dt| ƒ¡qš| D]„}
| |d|
|d|dt| ƒ¡| |d|
|d|dt| ƒ¡|     |d|
|d|dt| ƒ¡q$tdd|ƒD]0}
| |d¡| |d¡|     |d¡q¸| d¡}t||||    ƒS)    a›
    Colorize grayscale image.
    This function calculates a color wedge which maps all black pixels in
    the source image to the first color and all white pixels to the
    second color. If ``mid`` is specified, it uses three-color mapping.
    The ``black`` and ``white`` arguments should be RGB tuples or color names;
    optionally you can use three-color mapping by also specifying ``mid``.
    Mapping positions for any of the colors can be specified
    (e.g. ``blackpoint``), where these parameters are the integer
    value corresponding to where the corresponding color should be mapped.
    These parameters must have logical order, such that
    ``blackpoint <= midpoint <= whitepoint`` (if ``mid`` is specified).
 
    :param image: The image to colorize.
    :param black: The color to use for black input pixels.
    :param white: The color to use for white input pixels.
    :param mid: The color to use for midtone input pixels.
    :param blackpoint: an int value [0, 255] for the black mapping.
    :param whitepoint: an int value [0, 255] for the white mapping.
    :param midpoint: an int value [0, 255] for the midtone mapping.
    :return: An image.
    rNrr%rrrr)rÚAssertionErrorrr)r.r    r'r#)r ZblackZwhiteZmidZ
blackpointZ
whitepointZmidpointÚredÚgreenÚblueÚiZ    range_mapZ
range_map1Z
range_map2rrrÚcolorizežsJ&.
 
 
 
**0**.**.
r@cCsœ|j|j}|d|d}||krŽ||kr^t|j|j|dƒ}||dkrŽ|d|f}n0t|j|j|dƒ}||dkrŽ||df}|j||dS)aö
    Returns a resized version of the image, set to the maximum width and height
    within the requested size, while maintaining the original aspect ratio.
 
    :param image: The image to resize and crop.
    :param size: The requested output size in pixels, given as a
                 (width, height) tuple.
    :param method: Resampling method to use. Default is
                   :py:attr:`~PIL.Image.Resampling.BICUBIC`.
                   See :ref:`concept-filters`.
    :return: An image.
    rr)Úresample)ÚwidthÚheightÚroundÚresize)r ÚsizeÚmethodZim_ratioZ
dest_ratioZ
new_heightZ    new_widthrrrÚcontainðs    rH)çà?rIc    CsÀt|||ƒ}|j|kr|}n t |j||¡}|jr@| | ¡¡|j|dkr†t    |d|jt
dt |ddƒƒƒ}|  ||df¡n6t    |d|j t
dt |ddƒƒƒ}|  |d|f¡|S)at
    Returns a resized and padded version of the image, expanded to fill the
    requested aspect ratio and size.
 
    :param image: The image to resize and crop.
    :param size: The requested output size in pixels, given as a
                 (width, height) tuple.
    :param method: Resampling method to use. Default is
                   :py:attr:`~PIL.Image.Resampling.BICUBIC`.
                   See :ref:`concept-filters`.
    :param color: The background color of the padded image.
    :param centering: Control the position of the original image within the
                      padded version.
 
                          (0.5, 0.5) will keep the image centered
                          (0, 0) will keep the image aligned to the top left
                          (1, 1) will keep the image aligned to the bottom
                          right
    :return: An image.
    rr)rHrFrÚnewrÚpaletteÚ
putpaletteÚ
getpaletterBrDÚmaxÚminÚpasterC)    r rFrGrÚ    centeringZresizedÚoutÚxÚyrrrÚpad s 
&&rUcCs6t|ƒ\}}}}| |||jd||jd|f¡S)a/
    Remove border from image.  The same amount of pixels are removed
    from all four sides.  This function works on all image modes.
 
    .. seealso:: :py:meth:`~PIL.Image.Image.crop`
 
    :param image: The image to crop.
    :param border: The number of pixels to remove.
    :return: An image.
    rr)rÚcroprF)r r
r r r rrrrrV3s rVcCsR|dkr| ¡S|dkr&d}t|ƒ‚n(t||jƒt||jƒf}| ||¡SdS)aá
    Returns a rescaled image by a specific factor given in parameter.
    A factor greater than 1 expands the image, between 0 and 1 contracts the
    image.
 
    :param image: The image to rescale.
    :param factor: The expansion factor, as a float.
    :param resample: Resampling method to use. Default is
                     :py:attr:`~PIL.Image.Resampling.BICUBIC`.
                     See :ref:`concept-filters`.
    :returns: An :py:class:`~PIL.Image.Image` object.
    rrz!the factor must be greater than 0N)ÚcopyÚ
ValueErrorrDrBrCrE)r ZfactorrAr"rFrrrr7Bs 
r7cCs| |jtjj| |¡|¡S)aL
    Deform the image.
 
    :param image: The image to deform.
    :param deformer: A deformer object.  Any object that implements a
                    ``getmesh`` method can be used.
    :param resample: An optional resampling filter. Same values possible as
       in the PIL.Image.transform function.
    :return: An image.
    )Z    transformrFrZ    TransformZMESHZgetmesh)r ZdeformerrArrrÚdeformYs ÿrYc    Csâ|jdkr| d¡}| |¡}g}tdt|ƒdƒD]¤}dd„|||d…Dƒ}t|ƒdkrp| ttdƒƒ¡q2t t    j
|¡|dd    }|s¢| ttdƒƒ¡q2|d
}tdƒD]"}|  ||¡||||}q²q2t ||ƒS) a„
    Equalize the image histogram. This function applies a non-linear
    mapping to the input image, in order to create a uniform
    distribution of grayscale values in the output image.
 
    :param image: The image to equalize.
    :param mask: An optional mask.  If given, only the pixels selected by
                 the mask are included in the analysis.
    :return: An image.
    rrrrcSsg|] }|r|‘qSrr)Ú.0Z_frrrÚ
<listcomp>yszequalize.<locals>.<listcomp>rr&r%r) rr'r(r)r    r+r,Ú    functoolsÚreduceÚoperatorÚaddr.r#)    r r1r2r!ÚbZhistoÚstepr4r?rrrÚequalizeis 
 
 
  rbc Cs¨t|ƒ\}}}}||jd|}||jd|}t||jƒ}    |jrltj| ¡d}
t|    tƒrp|
     |    ¡}    nd}
t
  |j||f|    ¡} |
r”|   |
j¡|   |||f¡| S)zÖ
    Add border to the image
 
    :param image: The image to expand.
    :param border: Border width, in pixels.
    :param fill: Pixel fill value (a color value).  Default is 0 (black).
    :return: An image.
    rr)rKN)rrFrrrKrrMrrrrrJrLrP) r r
Úfillr r r rrBrCrrKrRrrrÚexpandˆs     
  rdçcCsxt|ƒ}d|dkr dks*nd|d<d|dkrBdksLnd|d<d|kr`dksfnd}||jd||jdf}|jd|dd|jd|ddf}|d|d}|d|d}||krè|d}    |d}
n4||kr||d}    |d}
n|d}    |d|}
|d|d|    |d} |d|d|
|d} | | | |    | |
f} |j||| dS)a
    Returns a resized and cropped version of the image, cropped to the
    requested aspect ratio and size.
 
    This function was contributed by Kevin Cazabon.
 
    :param image: The image to resize and crop.
    :param size: The requested output size in pixels, given as a
                 (width, height) tuple.
    :param method: Resampling method to use. Default is
                   :py:attr:`~PIL.Image.Resampling.BICUBIC`.
                   See :ref:`concept-filters`.
    :param bleed: Remove a border around the outside of the image from all
                  four edges. The value is a decimal percentage (use 0.01 for
                  one percent). The default value is 0 (no border).
                  Cannot be greater than or equal to 0.5.
    :param centering: Control the cropping position.  Use (0.5, 0.5) for
                      center cropping (e.g. if cropping the width, take 50% off
                      of the left side, and therefore 50% off the right side).
                      (0.0, 0.0) will crop from the top left corner (i.e. if
                      cropping the width, take all of the crop off of the right
                      side, and if cropping the height, take all of it off the
                      bottom).  (1.0, 0.0) will crop from the bottom left
                      corner, etc. (i.e. if cropping the width, take all of the
                      crop off the left side, and if cropping the height take
                      none from the top, and therefore all off the bottom).
    :return: An image.
    rergð?rIrr)Zbox)r,rFrE)r rFrGZbleedrQZ bleed_pixelsZ    live_sizeZlive_size_ratioZ output_ratioZ
crop_widthZ crop_heightZ    crop_leftZcrop_toprVrrrÚfit¢s2#þ
 
 
 rfcCs| tjj¡S)zq
    Flip the image vertically (top to bottom).
 
    :param image: The image to flip.
    :return: An image.
    )Ú    transposerÚ    TransposeÚFLIP_TOP_BOTTOM©r rrrÚflipøsrkcCs
| d¡S)zi
    Convert the image to grayscale.
 
    :param image: The image to convert.
    :return: An image.
    r)r'rjrrrÚ    grayscalesrlcCs>g}tdƒD]}| d|¡q |jdkr4| |¡St||ƒS)zc
    Invert (negate) the image.
 
    :param image: The image to invert.
    :return: An image.
    rr%Ú1)r)r.rrr#)r r!r?rrrÚinvert s rncCs| tjj¡S)zq
    Flip image horizontally (left to right).
 
    :param image: The image to mirror.
    :return: An image.
    )rgrrhÚFLIP_LEFT_RIGHTrjrrrÚmirrorsrpcCs<g}dd|d}tdƒD]}| ||@¡qt||ƒS)zÁ
    Reduce the number of bits for each color channel.
 
    :param image: The image to posterize.
    :param bits: The number of bits to keep for each channel (1-8).
    :return: An image.
    rérr©r)r.r#)r Úbitsr!r1r?rrrÚ    posterize#s
 rté€cCs>g}tdƒD]&}||kr$| |¡q | d|¡q t||ƒS)z¿
    Invert all pixel values above a threshold.
 
    :param image: The image to solarize.
    :param threshold: All pixels above this greyscale level are inverted.
    :return: An image.
    rr%rr)r Ú    thresholdr!r?rrrÚsolarize2s   rwcCsâ| ¡}| d¡}tjjtjjtjjtjjtjjtjj    tjj
dœ |¡}|dk    rÚ|  |¡}| ¡}d|krÖ|d=d|j krŠ|  ¡|j d<nLd|j kr¨|  ¡ ¡|j d<n.d|j krÖdD]}t |d|j d¡|j d<q¶|S| ¡S)    a
    If an image has an EXIF Orientation tag, other than 1, return a new image
    that is transposed accordingly. The new image will have the orientation
    data removed.
 
    Otherwise, return a copy of the image.
 
    :param image: The image to transpose.
    :return: An image.
    i)rérééérqNÚexifzRaw profile type exifzXML:com.adobe.xmp)ztiff:Orientation="([0-9])"z,<tiff:Orientation>([0-9])</tiff:Orientation>r)ZgetexifÚgetrrhroZ
ROTATE_180riZ    TRANSPOSEZ
ROTATE_270Z
TRANSVERSEZ    ROTATE_90rgÚinfoÚtobytesÚhexÚreÚsubrW)r r|Z orientationrGZtransposed_imageZtransposed_exifÚpatternrrrÚexif_transposeCsB 
ùø    
 
 
 
þÿ
ÿ r„)rNNF)Nrr%r:)r)N)rr)ru)r\r^rrrrrrr#r9r@Z
ResamplingZBICUBICrHrUrVr7ZBILINEARrYrbrdrfrkrlrnrprtrwr„rrrrÚ<module>s. 
[
R&
 
 
V