Skip to main content

PyCodeSnap for format codes and code to image.

Project description

PyCodeSnap

Python License PyPI

A Python library to generate beautiful, modern code screenshots with rounded corners, syntax highlighting, and professional styling - similar to VS Code CodeSnap extension.

🌟 Features

  • High-Quality Output: Generate crisp, clear code screenshots with customizable resolution
  • Modern Design: Rounded corners, soft shadows, and sleek window controls
  • Syntax Highlighting: Support for 300+ programming languages with multiple themes
  • Customizable Styling: Adjustable colors, fonts, spacing, and dimensions
  • Professional Appearance: Clean, minimal design perfect for documentation and presentations
  • Cross-Platform: Works on Windows, macOS, and Linux

📦 Installation

pip install pycodesnap

Requirements

  • Python 3.7+
  • Pillow (PIL)
  • Pygments
pip install Pillow Pygments

🚀 Quick Start

from pycodesnap import CodeSnap

# Your code snippet
code = '''
def fibonacci(n: int) -> int:
    """Calculate fibonacci number recursively."""
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

# Example usage
for i in range(10):
    print(f"fibonacci({i}) = {fibonacci(i)}")
'''

# Create beautiful code screenshot
snap = CodeSnap("python", code)
snap.create(
    width=800,
    font_size=16,
    line_numbers=True,
    style="github-dark"
)

# Save the result
snap.save("my_code.png")

🛠️ Advanced Usage

from pycodesnap import CodeSnap

code = '''
// JavaScript example with modern styling
const fibonacci = (n) => {
    if (n <= 1) return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
};

// Generate sequence
const sequence = Array.from({length: 10}, (_, i) => fibonacci(i));
console.log('Fibonacci sequence:', sequence);
'''

snap = CodeSnap("javascript", code)

# Create high-quality screenshot with custom styling
snap.create(
    width=1000,
    font_size=18,
    line_numbers=True,
    style="monokai",
    background_color="#1e1e1e",
    corner_radius=25,
    shadow=True,
    window_controls=True,
    padding=(40, 30),
    line_number_color="#666666",
    quality_factor=3,
    line_spacing=1.4
)

# Save with maximum quality
snap.save("javascript_code.png", quality=100)

🎨 Supported Languages

PyCodeSnap supports syntax highlighting for 300+ programming languages including:

  • Python, JavaScript, TypeScript
  • Java, C, C++, C#, Go, Rust
  • HTML, CSS, SCSS, Less
  • PHP, Ruby, Swift, Kotlin
  • SQL, YAML, JSON, Markdown
  • Shell, PowerShell, Dockerfile
  • And many more...

🎯 Available Themes

  • default - Standard Pygments theme
  • github-dark - GitHub dark theme
  • monokai - Popular dark theme
  • dracula - Dracula color scheme
  • solarized-dark - Solarized dark theme
  • solarized-light - Solarized light theme
  • vim - Vim editor theme
  • emacs - Emacs editor theme

📐 API Reference

CodeSnap Class

CodeSnap(language: str, code: str)

Parameters:

  • language (str): Programming language identifier
  • code (str): Code content to render

Methods

create()

Generate the code screenshot with specified parameters.

Parameters:

  • width (int): Output image width (default: 1200)
  • font_size (int): Base font size (default: 16)
  • line_numbers (bool): Show line numbers (default: True)
  • style (str): Syntax highlighting theme (default: "github-dark")
  • background_color (str): Background color in hex (default: "#0d1117")
  • corner_radius (int): Rounded corner radius (default: 20)
  • shadow (bool): Add drop shadow (default: True)
  • window_controls (bool): Show window controls (default: True)
  • padding (tuple): (horizontal, vertical) padding (default: (30, 25))
  • line_number_color (str): Line number color (default: "#6e7681")
  • max_height (int): Maximum image height (default: 3000)
  • quality_factor (int): Quality multiplier (default: 3)
  • line_spacing (float): Line spacing multiplier (default: 1.3)
  • dpi (int): Output resolution (default: 300)

save()

Save the generated image to file.

Parameters:

  • fp (str/Path): File path
  • format (str): Image format (default: "PNG")
  • optimize (bool): Optimize image (default: True)
  • quality (int): JPEG quality 1-100 (default: 100)
  • compress_level (int): PNG compression 0-9 (default: 1)

show()

Display the image using system viewer.

to_bytes()

Convert image to bytes.

Parameters:

  • format (str): Image format (default: "PNG")
  • optimize (bool): Optimize image (default: True)
  • quality (int): JPEG quality (default: 100)

get_line_count()

Get total number of lines in code.

📝 Examples

Python Code Screenshot

from pycodesnap import CodeSnap

python_code = '''
def quick_sort(arr):
    """Quick sort implementation with type hints."""
    if len(arr) <= 1:
        return arr
    
    pivot = arr[len(arr) // 2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    
    return quick_sort(left) + middle + quick_sort(right)

# Test the function
numbers = [64, 34, 25, 12, 22, 11, 90]
sorted_numbers = quick_sort(numbers)
print(f"Original: {numbers}")
print(f"Sorted: {sorted_numbers}")
'''

snap = CodeSnap("python", python_code)
snap.create(width=900, style="github-dark")
snap.save("python_example.png")

Web Development Code

from pycodesnap import CodeSnap

html_css_js = '''
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Modern Web App</title>
    <style>
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 2rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 8px 32px rgba(0,0,0,0.1);
            padding: 2rem;
            margin: 1rem 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <h1>Hello, World!</h1>
            <p>This is a modern web application.</p>
        </div>
    </div>
    
    <script>
        // Modern JavaScript with ES6 features
        const greet = (name) => {
            return `Hello, ${name}! Welcome to our app.`;
        };
        
        document.addEventListener('DOMContentLoaded', () => {
            const message = greet('Developer');
            console.log(message);
        });
    </script>
</body>
</html>
'''

# Create separate screenshots for different languages
html_snap = CodeSnap("html", html_css_js)
html_snap.create(width=1100, style="github-dark", line_numbers=True)
html_snap.save("web_code.png")

🎨 Customization Examples

Minimal Style

snap.create(
    width=700,
    font_size=14,
    line_numbers=False,
    style="default",
    background_color="#ffffff",
    corner_radius=10,
    shadow=False,
    window_controls=False,
    padding=(20, 15)
)

Dark Theme with High Contrast

snap.create(
    width=900,
    font_size=16,
    line_numbers=True,
    style="monokai",
    background_color="#000000",
    corner_radius=15,
    shadow=True,
    line_number_color="#888888",
    line_spacing=1.5
)

High-Quality Print Ready

snap.create(
    width=1500,
    font_size=18,
    line_numbers=True,
    style="github-dark",
    background_color="#0d1117",
    corner_radius=20,
    shadow=True,
    quality_factor=4,
    dpi=300,
    line_spacing=1.4
)
snap.save("print_ready.png", quality=100)

📤 Output Formats

Support for multiple image formats:

# PNG (default - lossless)
snap.save("code.png")

# JPEG (compressed - smaller file size)
snap.save("code.jpg", format="JPEG", quality=95)

# WebP (modern format with excellent compression)
snap.save("code.webp", format="WEBP", quality=90)

🛡️ Error Handling

from pycodesnap import CodeSnap

try:
    snap = CodeSnap("python", "print('Hello World')")
    snap.create()
    snap.save("output.png")
except ValueError as e:
    print(f"Error: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

📞 Support

If you encounter any issues or have questions, please open an issue on GitHub.


Made with ❤️ for developers who appreciate beautiful code presentation!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycodesnap-0.2.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycodesnap-0.2-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file pycodesnap-0.2.tar.gz.

File metadata

  • Download URL: pycodesnap-0.2.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for pycodesnap-0.2.tar.gz
Algorithm Hash digest
SHA256 1c58c708facaa49e5b612c2d9f4f42313e78661e210b0c84fe04592ce6c0887e
MD5 938fc7117c4e785385d4d194b09f5ebf
BLAKE2b-256 625f19c447890ec0a2b2ec7ae623d80023430a5fb8d196502b0e5abdbc5b0f7f

See more details on using hashes here.

File details

Details for the file pycodesnap-0.2-py3-none-any.whl.

File metadata

  • Download URL: pycodesnap-0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for pycodesnap-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 44d647a1ccc7d5ceaab9fcf6c06d1232422de0cfd2ed0330f03f7b0a40c4ddd3
MD5 7c42dc3c64698af4e81c7e4d6f15e370
BLAKE2b-256 b254f2c56f8354841b6441c99063cfa2c0ca92c6bfde3fd3b8313b5a47dfaca4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page