Reading an image¶
Here are several different methods of reading images for using them with chafa.py. They perform similarly so just pick the one you like the most!
Using Loader
¶
The Loader
is a class included with chafa.py which aims making it easy to read and draw images to the chafa.Canvas
, however, it does depend on the MagickWand C-library.
from chafa import *
from chafa.loader import Loader
# Init canvas config
config = CanvasConfig()
# Set canvas height and width
config.height = 30
config.width = 30
# Open image with the loader
image = Loader("./snake.jpg")
width = image.width
height = image.height
rowstride = image.rowstride
# Init the canvas
canvas = Canvas(config)
# Draw to canvas
canvas.draw_all_pixels(
image.pixel_type,
image.get_pixels(),
width,
height,
rowstride
)
# Write picture
print(canvas.print().decode())
Using Pillow¶
When reading pixel data from an image opened with Pillow, we get a 2D array which needs to be reshaped into a 1D array before passing to chafa. This is still pretty fast since both Pillow and NumPy are well optimized.
from chafa import *
from PIL import Image
# Init canvas config
config = CanvasConfig()
# Set canvas height and width
config.height = 30
config.width = 30
# Open image with PIL
image = Image.open("./snake.jpg")
width = image.width
height = image.height
bands = len(image.getbands())
# Put image into correct format
pixels = image.tobytes()
# Init the canvas
canvas = Canvas(config)
# Draw to canvas
canvas.draw_all_pixels(
PixelType.CHAFA_PIXEL_RGB8,
pixels,
width,
height,
width * bands
)
# Write picture
print(canvas.print().decode())
Using pyvips¶
The pyvips library is another fast image manipulation library which can be used to read images in a pretty straight forward manner.
import pyvips
from chafa import *
# Init canvas config
config = CanvasConfig()
# Set canvas height and width
config.height = 30
config.width = 30
# Open image with vips
image = pyvips.Image.new_from_file("./snake.jpg")
width = image.width
height = image.height
bands = image.bands
# Init the canvas
canvas = Canvas(config)
# Draw to canvas
canvas.draw_all_pixels(
PixelType.CHAFA_PIXEL_RGB8,
image.write_to_memory(),
width,
height,
width * bands
)
# Write picture
print(canvas.print().decode())