Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save_as_image() as jpeg leads to entirely black image #381

Closed
rempsyc opened this issue Mar 7, 2022 · 5 comments
Closed

save_as_image() as jpeg leads to entirely black image #381

rempsyc opened this issue Mar 7, 2022 · 5 comments

Comments

@rempsyc
Copy link
Contributor

rempsyc commented Mar 7, 2022

I noticed that per default, saving as png with save_as_image makes the table have a transparent background, so when checked on GitHub with dark mode, it is really difficult to read it:

transparent background

So I tried saving as jpeg instead, as those should have an actual non-transparent background. To my surprise, the backgrounds were instead all black! I've tried with the basic example provided in ?save_as_image with the same result. Demo:

library(flextable)
ft <- flextable( head( mtcars ) )
save_as_image(x = ft, path = "myimage.jpeg")

myimage

I've also tried using webshot2 as well for the png (it doesn't support jpeg) but it didn't change the problematic transparent backgrounds.

Current workaround is to manually fill background with white in an image processing software for each image before pushing to GitHub.

sessionInfo

sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252   
[3] LC_MONETARY=English_Canada.1252 LC_NUMERIC=C                   
[5] LC_TIME=English_Canada.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] flextable_0.7.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8        ps_1.6.0          digest_0.6.29     R6_2.5.1          jsonlite_1.8.0   
 [6] magrittr_2.0.2    evaluate_0.15     zip_2.2.0         rlang_1.0.1       gdtools_0.2.4    
[11] cli_3.2.0         uuid_1.0-3        data.table_1.14.2 callr_3.7.0       xml2_1.3.3       
[16] rmarkdown_2.12    webshot_0.5.2     tools_4.1.2       officer_0.4.1     processx_3.5.2   
[21] xfun_0.30         fastmap_1.1.0     compiler_4.1.2    systemfonts_1.0.4 base64enc_0.1-3  
[26] htmltools_0.5.2   knitr_1.37     

@davidgohel
Copy link
Owner

related to:

This is not something that I can fix.

  • jpeg does not support transparency.
  • webshot2 does not allow transparent background.
  • webshot does allow transparent background.
  • png does support transparency: see illustration below (made with your illustration)

Capture d’écran 2022-03-07 à 09 34 15

If the need is to display flextable in github dark mode, you can use a dark theme or create your own (see flextable::theme_vader()).

I am going to add details in the manual of save_as_image().

@rempsyc
Copy link
Contributor Author

rempsyc commented Mar 7, 2022

Thanks for your answer David! I really appreciate it (and I will never stop praising your package too 😅).

I have a few more questions though.

jpeg does not support transparency.

  1. Does that mean that flextables have a transparent background by default?
  2. Does that mean there is no way or option to specify the background not to be transparent?
  3. Does that mean that there is no point in saving any flextable as jpeg?
  4. If the latter is true, why does the help ?save_as_image specify argument path as:

path: image file to be created. It should end with .png, .pdf, or .jpeg.

To be clear, the example I provided outputs a fully black image even when not hosted on GitHub. Like literally opening the image in an image viewer, it is all black. Is that expected?

library(flextable)
ft <- flextable( head( mtcars ) )
save_as_image(x = ft, path = "myimage.jpeg")

Note: I'm afraid using a dark theme for the flextable would not be a universal solution as people browsing GitHub on light theme will not see it correctly. So I suppose the trick to edit the images manually in an image processor software to fill their background is still the best workaround.

@davidgohel
Copy link
Owner

To me, this is transparent:

myimage

This is how it looks on my mac.

Capture d’écran 2022-03-07 à 22 50 30

I am done with screenshot now.

So your issue is about having a white background! Use the following code before starting any flextable production

set_flextable_defaults(background.color = "white")

See https://ardata-fr.github.io/flextable-book/format.html#default-formatting-properties

?flextable()
Default formatting properties are automatically applied to every flextable you produce. You can change these default values with function set_flextable_defaults().

Q:

  1. yes, it's "transparent"
  2. no, you could use bg() for example, but the suggested prop above is better, you will only need it once

[...] Is that expected?

Yes! Software often add black or gray background. Watch the screenshot above and note I change this background color. My mac display it with gray bg.

I don't know what to answer to Q3 and Q4. What do you want to know by asking these questions? If jpeg should be removed as option? That saving to jpeg has no sense?

@rempsyc
Copy link
Contributor Author

rempsyc commented Mar 8, 2022

(I'm sorry I wasn't more clear earlier, in my previous code examples, I was saving as jpeg, not png, whereas I see in your example that you are saving as png. Your screenshots make sense to be transparent since they are png. I think the issue was saving as jpeg WHILE having a transparent background led to a fully black image. Changing the background to white first did solve the issue!)

And oh wow! Yes! You are right,

set_flextable_defaults(background.color = "white")

is exactly what I was looking for! When saving as png, it is now perfect (when saved as jpeg it does work as well and we can see the table clearly, but it also adds a large black border, though it doesn't matter since I prefer saving as png anyway). Hopefully, this thread and solution will be useful to future readers encountering this issue.

Thanks so much! You really did think of everything, it never ceases to amaze me :D Thanks so much for your patience with us all while you've put such a tremendous amount of work into perfecting this amazing package and documentation 🙏

PS: I have recently migrated my custom garage functions into a personal package, is there a proper way to let you know that your package is being integrated into other packages and show appreciation? Like dropping this info here as a sidenote, or is it better practice to open a new issue, email you, or message you through your website, or is there already another sort of system in place?

@github-actions
Copy link

github-actions bot commented Sep 8, 2022

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants