79 lines
2.0 KiB
Python
79 lines
2.0 KiB
Python
"""Example of using pixelsort as a library."""
|
|
|
|
from PIL import Image
|
|
from pixelsort import pixelsort, analyze
|
|
|
|
# Load an image
|
|
img = Image.open("test_image.jpg")
|
|
|
|
# Analyze the image to get suggested thresholds
|
|
print("Analyzing image...")
|
|
stats = analyze(img)
|
|
print(f"Suggested bright value (subtle): {stats['suggestions']['bright']['subtle']}")
|
|
print(f"Suggested dark value (subtle): {stats['suggestions']['dark']['subtle']}")
|
|
print()
|
|
|
|
# Example 1: Simple bright mode sorting
|
|
print("Example 1: Bright mode with suggested threshold")
|
|
result1 = pixelsort(
|
|
img,
|
|
interval='bright',
|
|
bright_value=stats['suggestions']['bright']['subtle']
|
|
)
|
|
result1.save("example1_bright.png")
|
|
print("Saved example1_bright.png")
|
|
print()
|
|
|
|
# Example 2: Random intervals with hue sorting
|
|
print("Example 2: Random intervals with hue sorting")
|
|
result2 = pixelsort(
|
|
img,
|
|
interval='random',
|
|
sorting='hue',
|
|
char_length=100
|
|
)
|
|
result2.save("example2_random_hue.png")
|
|
print("Saved example2_random_hue.png")
|
|
print()
|
|
|
|
# Example 3: Diagonal sorting (45 degrees)
|
|
print("Example 3: Diagonal bright mode")
|
|
result3 = pixelsort(
|
|
img,
|
|
interval='bright',
|
|
sorting='value',
|
|
angle=45,
|
|
bright_value=stats['suggestions']['bright']['moderate']
|
|
)
|
|
result3.save("example3_diagonal.png")
|
|
print("Saved example3_diagonal.png")
|
|
print()
|
|
|
|
# Example 4: Edge detection with saturation sorting
|
|
print("Example 4: Edge detection with saturation sorting")
|
|
result4 = pixelsort(
|
|
img,
|
|
interval='edges',
|
|
sorting='saturation',
|
|
lower_threshold=0.3
|
|
)
|
|
result4.save("example4_edges_saturation.png")
|
|
print("Saved example4_edges_saturation.png")
|
|
print()
|
|
|
|
# Example 5: Multiple passes with randomness
|
|
print("Example 5: Bright mode with 2 passes and randomness")
|
|
result5 = pixelsort(
|
|
img,
|
|
interval='bright',
|
|
sorting='hue',
|
|
loops=2,
|
|
randomness=20,
|
|
bright_value=stats['suggestions']['bright']['moderate']
|
|
)
|
|
result5.save("example5_multi_pass.png")
|
|
print("Saved example5_multi_pass.png")
|
|
print()
|
|
|
|
print("All examples complete!")
|