atanunq / viuer

Rust library for displaying images in the terminal.
MIT License
241 stars 43 forks source link

How to load image from bytes (include_bytes)? #9

Closed KonradLinkowski closed 3 years ago

KonradLinkowski commented 3 years ago

I want to bundle my images with executable. This is the code I used and it fails compilation with error

expected enum `image::dynimage::DynamicImage`, found array `[u8; 23307]
use viuer::{Config, print_from_file, print};
let conf = Config {
  transparent: true,
  absolute_offset: false,
  ..Default::default()
};
let img = include_bytes!("image.png");
print(img, &conf);

How could I convert bytes to DynamicImage?

atanunq commented 3 years ago

Hi, It seems that you are not using the print_from_file function, although it is imported. It will help you with printing, all it needs is a path, see its signature here. You don't need to use include_bytes! in that case.

If you really want to load the image, you can have a look at the image library. It lets you do all kinds of manipulations to an image, including access to the raw data behind it. viuer uses it, and that's why the viuer::print method takes as an argument a DynamicImage.

KonradLinkowski commented 3 years ago

@atanunq thanks for the respons. print_from_file works fine, but I'm looking for the way to bundle the image with my executable.

atanunq commented 3 years ago

Oh, I see. I am afraid viuer cannot help you with that. You should have a look at image functionality to load the image as a usable representation. If this should happen at compile time, I guess you could do this in a macro expansion of some sorts. Also, I noticed there is a crate that does the magic for you - rust-embed.

Closing, as this project does not aim to achieve this functionality.