# Detect AI-Generated Images in PDFs

> Learn how to use the AI Image Detection API to check if images in a pdf file are AI-generated or partially AI-generated.

The Copyleaks [AI Image Detection](https://copyleaks.com/ai-detector/ai-image-detector) API is a powerful tool to determine if a given image was generated or partially generated by an AI.

As a consumer, you might have a PDF with images.
If you want to scan those images separately from the PDF, this guide is for you.

This guide will walk you through the process of extracting images from a pdf file and submitting them to the Copyleaks [AI Detector](https://copyleaks.com/ai-detector) and understanding the results.

## Get started

<Steps>
  <Step>
    ### Before you begin
    Before you start, ensure you have the following:
    - An active Copyleaks account. If you don't have one, **[sign up for free](https://api.copyleaks.com/signup)**.
    - You can find your API key on the **[API Dashboard](https://api.copyleaks.com/dashboard)**.
  </Step>

  <Step>
    ### Installation
    Install the relevant packages using `pip install -U PyMuPDF Pillow copyleaks`.
  </Step>

  <Step>
    ### Login
    <GuideLogin />
  </Step>

  <Step>
    ### Extracting images from a PDF file
    Next, we are going to extract all the images from the PDF.
    The function below will take a pdf file path and extract all its images to a specified directory.

    The following example takes the input PDF file and outputs
    all its nested images to the `output_folder` directory

    ```python
    import os
    import fitz  # package by the PyMuPDF module
    from pathlib import Path

    def extract_images(pdf_path: str, output_folder: str = "Extracted-Images") -> list[str]:
        """
        Extract all images from a PDF file.

        Args:
            pdf_path: Path to PDF file
            output_folder: Output folder for images

        Returns:
            List of extracted image paths as strings
        """
        os.makedirs(output_folder, exist_ok=True)

        extracted = []
        pdf_name = Path(pdf_path).stem

        pdf = None
        try:
            pdf = fitz.open(pdf_path)

            print(f"Processing: {pdf_path}")
            print(f"Pages: {len(pdf)}")

            image_count = 0

            for page_num in range(len(pdf)):
                page = pdf[page_num]
                images = page.get_images(full=True)

                print(f"Page {page_num + 1}: {len(images)} image(s)")

                for img_index, img in enumerate(images):
                    xref = img[0]
                    base_image = pdf.extract_image(xref)
                    image_bytes = base_image["image"]
                    ext = base_image["ext"]

                    image_count += 1
                    filename = f"{pdf_name}_page{
                        page_num + 1}_img{img_index + 1}.{ext}"
                    path = os.path.join(output_folder, filename)
                    try:
                        with open(path, "wb") as f:
                            f.write(image_bytes)
                    except Exception as e:
                        print(f"   Error: {e}")

                    extracted.append(path)
                    print(f"   {filename}")

        except Exception as e:
            print(f" Error: {e}")
            return []
        finally:
            if pdf is not None:
                pdf.close()
        print(f"\n Extracted {image_count} images")
        return extracted

    if __name__ == "__main__":
        extract_images("my_file.pdf", "output_dir")
    ```
  </Step>

  <Step>
    ### Submit for analysis
    Once we have the extracted images, you can submit them for analysis.

    We are going to use the [AI Image Detector Endpoint](
    /reference/actions/ai-image-detector/check) to send an image for analysis.

    #### AI Detection scan

    This function takes your image, converts it to base64, and submits it via
    the SDK's `ImageDetectionClient`. The SDK handles authentication and HTTP transport.

    ```python
    import os
    import base64
    import uuid
    from copyleaks.clients.image_detection_client import ImageDetectionClient
    from copyleaks.models.ai_image_detection import (
        CopyleaksAiImageDetectionRequestModel,
        CopyleaksAiImageDetectionModels,
    )

    def detect(image_path: str, auth_token: str):
        """Detect AI content in image using the Copyleaks SDK."""
        try:
            with open(image_path, 'rb') as f:
                image_data = base64.b64encode(f.read()).decode('utf-8')
        except Exception as e:
            print(f"   Error reading image: {e}")
            return None

        scan_id = str(uuid.uuid4())
        payload = CopyleaksAiImageDetectionRequestModel(
            base64=image_data,
            filename=os.path.basename(image_path),
            model=CopyleaksAiImageDetectionModels.AI_IMAGE_1_ULTRA,
            sandbox=False,
        )

        client = ImageDetectionClient()
        return client.submit(auth_token, scan_id, payload)

    if __name__ == "__main__":
        from pathlib import Path
        path = Path('directory_path')
        for entry in path.iterdir():
            if entry.is_file():
                print(detect(str(entry), 'auth_token'))
    ```
  </Step>

  <Step>
    ### Interpreting the response
    See the [Interpreting The Response](
    /guides/ai-detector/ai-image-detection/#interpreting-the-response) page on
    [Detecting AI-Generated Images](/guides/ai-detector/ai-image-detection/)
  </Step>
</Steps>

## Summary

You have successfully submitted images from PDF for AI detection. You are free to adapt this code to your needs.

## Next steps
<CardGroup cols={2}>
    <Card title="API Reference" icon="code" href="/reference/actions/ai-image-detector/check">Explore the full API reference for the AI Image Detection endpoint.</Card>
    <Card title="AI Image Detector Response" icon="brackets-curly" href="/reference/data-types/ai-detector/ai-image-detection-response">Explore the full response for the AI Image Detection.</Card>
    <Card title="Accuracy & 3rd Party Evaluations" icon="award" href="https://copyleaks.com/blog/ai-detector-continues-top-accuracy-third-party">Discover how Copyleaks AI Detector maintains top accuracy in third-party evaluations.</Card>
</CardGroup>
