ropensci / magick

Magic, madness, heaven, sin
https://docs.ropensci.org/magick
Other
459 stars 64 forks source link

density argument when reading pdf not respected #243

Closed dschneiderch closed 4 years ago

dschneiderch commented 4 years ago

Seems like reading a pdf into magick through pdftools always comes in as 72 dpi

npqleg = 'npq_legend.pdf'

> image_read_pdf(npqleg,density=288)
# A tibble: 1 x 7
  format width height colorspace matte filesize density
  <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
1 PNG     1259   1402 sRGB       TRUE         0 +72x+72
> pdftools::pdf_render_page(npqleg, dpi=288, antialias=T) %>% image_read(density="288")
# A tibble: 1 x 7
  format width height colorspace matte filesize density
  <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
1 PNG     1259   1402 sRGB       TRUE         0 +72x+72

Just pdf_render_page seems to work without errors:

> pdftools::pdf_render_page(npqleg, dpi=288, antialias=T)
4 channel 1259x1402 bitmap array: 'bitmap' raw [1:4, 1:1259, 1:1402] ff ff ff ff ...

image_read works ok though

> image_read(npqleg,density=288)
# A tibble: 1 x 7
  format width height colorspace matte filesize density  
  <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>    
1 PDF     1259   1402 sRGB       TRUE     19261 +288x+288

in the case of pdftools::pdf_render_page(npqleg, dpi=288, antialias=T) %>% png::writePNG('test2.png', dpi=288) why does image_read('test2.png') return an image with density 113? I expected ether 288 or 72. If i open test2.png in an image viewer it recognizes it to have dpi=288.

npq_legend.pdf

context

I'm trying to put together several plots from matplotlib that are formatted like npq_legend above. I have the figures saved as rasters at 100 dpi, but i'm trying to get a better legend so I saved a single copy as a pdf. i'm trying to follow this advice of supersampling to get decent text. http://www.imagemagick.org/discourse-server/viewtopic.php?p=54853&sid=1525b3ad11155f1b4ff1ad4d0a76ca5e#p54853

this is what i'm trying to crop:

image_read(npqleg, density=288) %>% image_draw()
rect(1075, 1175, 1250, 200, border='red')

> sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
 [1] glue_1.4.0      magick_2.3      fs_1.4.1.9000   here_0.1        forcats_0.5.0   stringr_1.4.0   dplyr_0.8.5     purrr_0.3.3     readr_1.3.1    
[10] tidyr_1.0.2     tibble_3.0.0    ggplot2_3.3.0   tidyverse_1.3.0

loaded via a namespace (and not attached):
 [1] qpdf_1.1         tidyselect_1.0.0 haven_2.2.0      lattice_0.20-38  colorspace_1.4-1 vctrs_0.2.4      generics_0.0.2   utf8_1.1.4       rlang_0.4.5     
[10] pillar_1.4.3     withr_2.1.2      DBI_1.1.0        dbplyr_1.4.2     modelr_0.1.6     readxl_1.3.1     lifecycle_0.2.0  munsell_0.5.0    gtable_0.3.0    
[19] cellranger_1.1.0 rvest_0.3.5      fansi_0.4.1      broom_0.5.5      pdftools_2.3     Rcpp_1.0.4       scales_1.1.0     backports_1.1.6  jsonlite_1.6.1  
[28] hms_0.5.3        askpass_1.1      png_0.1-7        stringi_1.4.6    grid_3.6.0       rprojroot_1.3-2  cli_2.0.2        tools_3.6.0      magrittr_1.5    
[37] crayon_1.3.4     pkgconfig_2.0.3  ellipsis_0.3.0   xml2_1.3.0       reprex_0.3.0     lubridate_1.7.8  assertthat_0.2.1 httr_1.4.1       rstudioapi_0.11 
[46] R6_2.4.1         nlme_3.1-139     compiler_3.6.0  
jeroen commented 4 years ago

OK I see the issue. It is not setting the density attribute correctly, but actually it is using the density, if you look at the size of the output image.

I'll try to fix it so that it also sets the attribute correct, but it really doesn't matter much. What matters is the resolution of the image; dpi is just an attribute.