#author("2024-10-25T02:03:46+09:00","default:BotComputing-WikiIoT","BotComputing-WikiIoT")
#author("2024-10-25T02:05:09+09:00","default:BotComputing-WikiIoT","BotComputing-WikiIoT")
[[FrontPage]]

#code(Python){{

# -*- coding: utf-8 -*-
#
#
# Generated by Claude.ai
#
from PIL import Image
import numpy as np

def jpeg_to_ascii(image_path, width=100, chars=" .:-=+*#%@"):
    """
    Convert a JPEG image to ASCII art.
    
    Args:
        image_path (str): Path to the JPEG image
        width (int): Desired width of ASCII art in characters
        chars (str): String of ASCII characters to use, from darkest to lightest
        
    Returns:
        str: ASCII art representation of the image
    """
    try:
        # Open and convert image to grayscale
        img = Image.open(image_path).convert('L')
        
        # Calculate new dimensions
        aspect_ratio = img.height / img.width
        height = int(width * aspect_ratio * 0.5)  # Multiply by 0.5 to account for terminal character spacing
        
        # Resize image
        img = img.resize((width, height), Image.Resampling.LANCZOS)
        
        # Convert image to numpy array
        pixels = np.array(img)
        
        # Convert pixel values to ASCII characters
        # Normalize pixel values to the length of our character list
        pixel_chars = [chars[int(pixel * (len(chars) - 1) / 255)] 
                      for row in pixels 
                      for pixel in row]
        
        # Join characters into rows and then join rows
        ascii_art = '\n'.join([''.join(pixel_chars[i:i + width]) 
                              for i in range(0, len(pixel_chars), width)])
        
        return ascii_art
    
    except Exception as e:
        return f"Error processing image: {str(e)}"

# Example usage
if __name__ == "__main__":
    # Example with different detail levels
    char_sets = {
        'simple': " .:-=+*#%@",
        'detailed': " .'`^\",:;Il!i><~+_-?][}{1)(|\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$",
        'minimal': " .*#@"
    }
    
    try:
        # Replace 'image.jpg' with your image path
        image_path = 'image.jpg'
        
        print("Simple ASCII Art:")
        print(jpeg_to_ascii(image_path, width=100, chars=char_sets['simple']))
        
        print("\nDetailed ASCII Art:")
        print(jpeg_to_ascii(image_path, width=100, chars=char_sets['detailed']))
        
        print("\nMinimal ASCII Art:")
        print(jpeg_to_ascii(image_path, width=100, chars=char_sets['minimal']))
        
    except Exception as e:
        print(f"Error: {str(e)}")



}}

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS