#Import image and changing transparency

30 messages · Page 1 of 1 (latest)

lilac mural
#

I was looking for a way to import a png and then changing the transparency, but couldn't find how in the docs.

Currently I'm changing the alpha of my image on an external program and importing it into typst.

Is there something that I'm missing here?

Looking into the docs there are functions like 'decode' that might seem to help but I don't know if it is the case

modern vale
#

I don't think that Typst has this built-in. In theory, a plugin (image processing library compiled to WASM and then imported in Typst) could do this, and the output of the plugin would be given to decode(), but I don't think a package shipping such a plugin exists yet.

halcyon oxide
#

I have experimented with the wasm Pluginsystem and got something like this working pretty quickly. You can crop images, convert to greyscale, add transparency and so on. If there is general interest in this i'll look into providing this as a package. For the mean time I could gibe you the relatively untested and undocumented wasm binary

halcyon oxide
#

ok, I'll try to provide something this weekend

halcyon oxide
#

after struggling way to long with the wrong rustc version, here's the code to a WIP plugin example:
https://github.com/nineff/GrayNess
it actually doesn't include transparency yet, but it shouldn't be to hard to add.
I will create a proper typst wrapper around it and submit it as a package in the coming days.

GitHub

Contribute to nineff/GrayNess development by creating an account on GitHub.

#

Sadly, it is really slow.

halcyon oxide
#

I've now updated it to also provide the requested transparency functionality.

#

@lilac mural is this sufficient for you or should I create an "official" package?

lilac mural
#

It worked, but it really slows down compilation. You wrote a warning for slow compilation on the "blur" function in the repo README, but not for transparency, maybe I'm doing something wrong?

#

It takes a lot to apply transparency to a 220k png file

halcyon oxide
#

Yes, sadly all operations on images are really slow, but blurring especially so. I'm not sure if this can be significantly be sped up, though.

#

At least typst is clever enough to cache the results and once the image is rendered, subsequent edits are fast again

#

from my point of view, to make these operations faster, there are limited options. Either typst gets native support for basic image editing (which should not be too hard to do since the crates needed are already being used, but the devs would need to choose to do so and prioritize it), or edit images externally.
But I'm going to look into making the wasm plugin faster anyhow for my own education, I don't have a timeline for that however

spiral bane
#

theres also wasmtime which supports JIT, but it'd increase typst's size and compilation time, so it's being held off for now

halcyon oxide
#

yeah, JIT would definitively speed those operations up a lot, since they are the same calculations repeated over and over again.

#

I'm also not sure how well the compilation to wasm works for the image crate, since I had trouble to make it work at all, perhaps there could be some improvements by tweaking the compile settings and so on

#

for the moment, I'm happy it worked at all and the Proof of concept was achieved, and that quite quickly. I had fun doing it and even used it to generate fractals on the fly from within typst, which was even slower, but really cool. I love typst 😉

halcyon oxide
#

i have now created a typst package:

#

i was able to somewhat improve the speed by removing unnecessary functions (rotate can be achieved with typst code) and a different optimization level for the wasm binary

bleak geyser
halcyon oxide
#

true, but now I've already started the PR and I don't think there'd be a significant speed increase by removing them. the filesize may shrink, though.

#

the difference is 10kb, so very negligeable

halcyon oxide
rough kernel
#

That's really cool

#

Unfortunately the transparency function doesn't seem to work

halcyon oxide
#

can you provide a more detailed description of whats not working?