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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import os
from pathlib import Path
from PIL import Image, ImageFilter, ImageOps, ImageDraw, ImageChops
import tempfile

def validate_path(p: str) -> Path:
    path = Path(os.path.expanduser(p)).resolve()
    if not path.exists():
        raise FileNotFoundError(f"Path does not exist: {path}")
    return path


def temp_output(suffix=".png"):
    return tempfile.NamedTemporaryFile(delete=False, suffix=suffix).name

def ensure_path_from_img(img) -> Path:
    """
    Ensures that the input is converted to a valid image file path.

    If `img` is a PIL.Image object, it saves it to a temporary file and returns the path.
    If `img` is already a path (string or Path), it wraps and returns it as a Path.

    Args:
        img: A PIL.Image or a path string

    Returns:
        Path to the image file
    """

    if isinstance(img, Image.Image):
        temp_input = temp_output()
        img.save(temp_input)
        return Path(temp_input)
    return validate_path(img)

def resize_image(input_path: str, width: int, height: int, output_path: str = None) -> str:
    """
    Resize an image and save to a new file.

    Args:
        input_path (str): Path to source image.
        output_path (str): Path to save resized image.
        width (int): New width in pixels.
        height (int): New height in pixels.

    Returns:
        str: Path to resized image.
    """
    if output_path is None:
        output_path = temp_output()
    input_path = validate_path(input_path)
    output_path = Path(output_path).expanduser().resolve()
    with Image.open(input_path) as img:
        img = img.resize((width, height))
        output_path.parent.mkdir(parents=True, exist_ok=True)
        img.save(output_path)
    return str(output_path)

def convert_grayscale(input_path: str, output_path: str = None) -> str:
    """
    Convert an image to grayscale.

    Args:
        input_path (str): Source image path.
        output_path (str): Path to save grayscale version.

    Returns:
        str: Path to grayscale image.
    """
    if output_path is None:
        output_path = temp_output()
    input_path = validate_path(input_path)
    output_path = Path(output_path).expanduser().resolve()
    with Image.open(input_path) as img:
        gray = img.convert("L")
        output_path.parent.mkdir(parents=True, exist_ok=True)
        gray.save(output_path)
    return str(output_path)

def image_metadata(path: str) -> str:
    """
    Get image metadata.

    Args:
        path (str): Image path.

    Returns:
        str: Format, size, and mode info.
    """
    path = validate_path(path)
    with Image.open(path) as img:
        return "\n".join([
            f"Format: {img.format}",
            f"Size: {img.size}",
            f"Mode: {img.mode}"
        ])

def convert_format(input_path: str, format: str, output_path: str = None) -> str:
    """
    Convert an image to a new format.

    Args:
        input_path (str): Source image path.
        output_path (str): Destination path with extension.
        format (str): Format (e.g. 'png', 'jpeg').

    Returns:
        str: Path to converted image.
    """
    if output_path is None:
        output_path = temp_output()
    input_path = validate_path(input_path)
    output_path = Path(output_path).expanduser().resolve()
    with Image.open(input_path) as img:
        output_path.parent.mkdir(parents=True, exist_ok=True)
        img.save(output_path, format=format.upper())
    return str(output_path)

def blur_image(input_path: str, radius: float, output_path: str = None) -> str:
    """
    Apply Gaussian blur to an image.

    Args:
        input_path (str): Image path.
        output_path (str): Path to save blurred image.
        radius (float): Blur radius.

    Returns:
        str: Path to blurred image.
    """
    if output_path is None:
        output_path = temp_output()
    input_path = validate_path(input_path)
    output_path = Path(output_path).expanduser().resolve()
    with Image.open(input_path) as img:
        img = img.filter(ImageFilter.GaussianBlur(radius))
        output_path.parent.mkdir(parents=True, exist_ok=True)
        img.save(output_path)
    return str(output_path)

def rotate_image(input_path: str, angle: float, output_path: str = None) -> str:
    """
    Rotate an image.

    Args:
        input_path (str): Source path.
        output_path (str): Destination path.
        angle (float): Rotation angle in degrees.

    Returns:
        str: Path to rotated image.
    """
    if output_path is None:
        output_path = temp_output()
    input_path = validate_path(input_path)
    output_path = Path(output_path).expanduser().resolve()
    with Image.open(input_path) as img:
        img = img.rotate(angle, expand=True)
        output_path.parent.mkdir(parents=True, exist_ok=True)
        img.save(output_path)
    return str(output_path)

def crop_image(input_path: str, left: int, top: int, right: int, bottom: int, output_path: str = None) -> str:
    """
    Crop an image.

    Args:
        input_path (str): Source image.
        output_path (str): Destination.
        left, top, right, bottom (int): Crop box coordinates.

    Returns:
        str: Path to cropped image.
    """
    if output_path is None:
        output_path = temp_output()
    input_path = validate_path(input_path)
    output_path = Path(output_path).expanduser().resolve()
    with Image.open(input_path) as img:
        img = img.crop((left, top, right, bottom))
        output_path.parent.mkdir(parents=True, exist_ok=True)
        img.save(output_path)
    return str(output_path)

def thumbnail_image(input_path: str, max_width: int, max_height: int, output_path: str = None) -> str:
    """
    Resize image while maintaining aspect ratio.

    Args:
        input_path (str): Source.
        output_path (str): Target.
        max_width (int), max_height (int): Constraints.

    Returns:
        str: Path to thumbnail image.
    """
    if output_path is None:
        output_path = temp_output()
    input_path = validate_path(input_path)
    output_path = Path(output_path).expanduser().resolve()
    with Image.open(input_path) as img:
        img.thumbnail((max_width, max_height))
        output_path.parent.mkdir(parents=True, exist_ok=True)
        img.save(output_path)
    return str(output_path)

def add_watermark(input_path: str, text: str, x: int, y: int, output_path: str = None) -> str:
    """
    Add watermark text.

    Args:
        input_path (str): Image file.
        output_path (str): Save path.
        text (str): Watermark content.
        x (int), y (int): Position.

    Returns:
        str: Path to watermarked image.
    """
    if output_path is None:
        output_path = temp_output()
    input_path = validate_path(input_path)
    output_path = Path(output_path).expanduser().resolve()
    with Image.open(input_path).convert("RGBA") as img:
        watermark = Image.new("RGBA", img.size)
        draw = ImageDraw.Draw(watermark)
        draw.text((x, y), text, fill=(255, 255, 255, 128))
        img = Image.alpha_composite(img, watermark).convert("RGB")
        output_path.parent.mkdir(parents=True, exist_ok=True)
        img.save(output_path)
    return str(output_path)

def flip_image(input_path: str, mode: str, output_path: str = None) -> str:
    """
    Flip image horizontally or vertically.

    Args:
        input_path (str): Source.
        output_path (str): Destination.
        mode (str): 'horizontal' or 'vertical'.

    Returns:
        str: Path to flipped image.
    """
    if output_path is None:
        output_path = temp_output()
    input_path = validate_path(input_path)
    output_path = Path(output_path).expanduser().resolve()
    with Image.open(input_path) as img:
        if mode == "horizontal":
            img = img.transpose(Image.FLIP_LEFT_RIGHT)
        elif mode == "vertical":
            img = img.transpose(Image.FLIP_TOP_BOTTOM)
        else:
            raise ValueError("Invalid mode")
        output_path.parent.mkdir(parents=True, exist_ok=True)
        img.save(output_path)
    return str(output_path)

def invert_colors(input_path: str, output_path: str = None) -> str:
    """
    Invert RGB color values.

    Args:
        input_path (str): Image file.
        output_path (str): Save location.

    Returns:
        str: Path to inverted image.
    """
    if output_path is None:
        output_path = temp_output()

    input_path = validate_path(input_path)
    output_path = Path(output_path).expanduser().resolve()
    with Image.open(input_path) as img:
        img = ImageOps.invert(img.convert("RGB"))
        output_path.parent.mkdir(parents=True, exist_ok=True)
        img.save(output_path)
    return str(output_path)

def list_images_in_directory(path: str) -> str:
    """
    List image files in a directory.

    Args:
        path (str): Directory path.

    Returns:
        str: Newline-separated list of image paths.
    """
    dir_path = Path(path).expanduser().resolve()
    if not dir_path.is_dir():
        raise NotADirectoryError(f"{dir_path} is not a directory.")
    exts = {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif"}
    files = [str(p) for p in dir_path.iterdir() if p.suffix.lower() in exts]
    return "\n".join(files) if files else "No image files found."

def get_image_metadata(path: str) -> dict:
    """
    Get metadata from an image.

    Args:
        path (str): Image path.

    Returns:
        dict: Metadata fields including format, size, mode.
    """

    path = validate_path(path)
    with Image.open(path) as img:
        return {
            "path": str(path),
            "format": img.format,
            "mode": img.mode,
            "size": img.size,
            "width": img.width,
            "height": img.height,
            "info": img.info
        }

import random

def apply_random_color_variation(input_path: str, strength: float = 0.1,output_path: str = None) -> str:
    """
    Apply random color variation to an image and save the result.

    Args:
        input_path (str): Source image path.
        output_path (str): Target image path.
        strength (float): Amount of variation per channel (0.0 to 1.0, default = 0.1).

    Returns:
        str: Path to the color-augmented image.
    """
    if output_path is None:
        output_path = temp_output()
    input_path = validate_path(input_path)
    output_path = Path(output_path).expanduser().resolve()

    with Image.open(input_path).convert("RGB") as img:
        pixels = img.load()
        for y in range(img.height):
            for x in range(img.width):
                r, g, b = pixels[x, y]
                r = int(min(max(r + random.randint(-int(255 * strength), int(255 * strength)), 0), 255))
                g = int(min(max(g + random.randint(-int(255 * strength), int(255 * strength)), 0), 255))
                b = int(min(max(b + random.randint(-int(255 * strength), int(255 * strength)), 0), 255))
                pixels[x, y] = (r, g, b)

        output_path.parent.mkdir(parents=True, exist_ok=True)
        img.save(output_path)

    return str(output_path)


def add_images(image_path1: str, image_path2: str, output_path: str = None) -> str:
    """
    Add two images pixel-wise and save the result.

    Args:
        image_path1 (str): Path to the first image.
        image_path2 (str): Path to the second image.
        output_path (str): Path to save the resulting image.

    Returns:
        str: Path to the added image.
    """
    if output_path is None:
        output_path = temp_output()

    path1 = validate_path(image_path1)
    path2 = validate_path(image_path2)
    output_path = Path(output_path).expanduser().resolve()

    with Image.open(path1).convert("RGB") as img1, Image.open(path2).convert("RGB") as img2:
        if img1.size != img2.size:
            raise ValueError("Images must be the same size")
        result = ImageChops.add(img1, img2, scale=1.0, offset=0)
        output_path.parent.mkdir(parents=True, exist_ok=True)
        result.save(output_path)

    return str(output_path)
from PIL import Image
from pathlib import Path

def concat_images(image_path1: str, image_path2: str, mode: str = "horizontal", output_path: str = None) -> str:
    """
    Concatenate two images either horizontally or vertically.

    Args:
        image_path1 (str): Path to the first image.
        image_path2 (str): Path to the second image.
        output_path (str): Path to save the concatenated image.
        mode (str): 'horizontal' or 'vertical'.

    Returns:
        str: Path to the output image.
    """
    if output_path is None:
        output_path = temp_output()

    img1 = Image.open(image_path1).convert("RGB")
    img2 = Image.open(image_path2).convert("RGB")

    if mode == "horizontal":
        new_width = img1.width + img2.width
        new_height = max(img1.height, img2.height)
        new_img = Image.new("RGB", (new_width, new_height))
        new_img.paste(img1, (0, 0))
        new_img.paste(img2, (img1.width, 0))
    elif mode == "vertical":
        new_width = max(img1.width, img2.width)
        new_height = img1.height + img2.height
        new_img = Image.new("RGB", (new_width, new_height))
        new_img.paste(img1, (0, 0))
        new_img.paste(img2, (0, img1.height))
    else:
        raise ValueError("Invalid mode. Use 'horizontal' or 'vertical'.")

    output_path = Path(output_path).expanduser().resolve()
    output_path.parent.mkdir(parents=True, exist_ok=True)
    new_img.save(output_path)
    return str(output_path)