jpg2ascii.py
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[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 charac...
chars (str): String of ASCII characters to use, f...
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) # Multi...
# Resize image
img = img.resize((width, height), Image.Resamplin...
# Convert image to numpy array
pixels = np.array(img)
# Convert pixel values to ASCII characters
# Normalize pixel values to the length of our cha...
pixel_chars = [chars[int(pixel * (len(chars) - 1)...
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 + ...
for i in range(0, len(pixel...
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)(|\/tfjrx...
'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=...
print("\nDetailed ASCII Art:")
print(jpeg_to_ascii(image_path, width=100, chars=...
print("\nMinimal ASCII Art:")
print(jpeg_to_ascii(image_path, width=100, chars=...
except Exception as e:
print(f"Error: {str(e)}")
}}
終了行:
[[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 charac...
chars (str): String of ASCII characters to use, f...
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) # Multi...
# Resize image
img = img.resize((width, height), Image.Resamplin...
# Convert image to numpy array
pixels = np.array(img)
# Convert pixel values to ASCII characters
# Normalize pixel values to the length of our cha...
pixel_chars = [chars[int(pixel * (len(chars) - 1)...
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 + ...
for i in range(0, len(pixel...
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)(|\/tfjrx...
'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=...
print("\nDetailed ASCII Art:")
print(jpeg_to_ascii(image_path, width=100, chars=...
print("\nMinimal ASCII Art:")
print(jpeg_to_ascii(image_path, width=100, chars=...
except Exception as e:
print(f"Error: {str(e)}")
}}
ページ名: