3

I want to build a tiff file with text only and no margins around the text, using R.

I tried :

library(ggplot2)

plt <- ggplot() +
  geom_text(aes(x=0,y=0,label="test")) +
  theme_void()

ggsave(plt,filename="test.tif",bg="transparent")

However, there are large margins (I have added the borderline to highlight these margins):

enter image description here

What I want to get is a file with the image of the text, without any margin: enter image description here

Is there a way to create such a tiff file, using ggplot, plot or any other R function?

0

3 Answers 3

7

This is pretty close, in base R: thanks to @r2evans for code to auto-adjust the width/height of the image:

txt <- "hello world"
tiff("test.tiff", strwidth(txt, units="in"), 
       strheight(txt, units="in"), units="in", res=300)
par(mar = rep(0,4))
plot(0:1, 0:1, type = "n", ann = FALSE, axes=FALSE)
text(0.5, 0.5, txt)
dev.off()

"hello world"

Sign up to request clarification or add additional context in comments.

8 Comments

I would post as an answer, but it's too close to be distinct ... BenBolker, suggest you go with something like txt <- "hello world"; dev.new(); tiff("test.tiff", strwidth(txt, units="in"), strheight(txt, units="in"), units="in", res=300); par(..); plot(..); text(0.5, 0.5, txt); dev.off(). The call to dev.new() is purely to have a base-graphics canvas available, we don't need anything in it.
Thanks! I was getting around to strwidth()/strheight() but hadn't quite gotten there yet.
I believe the dev.new() is redundant. Also, plot.new(); plot.window(0:1, 0:1) instead plot() might be more concise.
perhaps dev.new() may not be redundant for RStudio IDE users? I believe the IDE does a lot of trickery under the hood for graphics management ... perhaps if (!length(dev.list())) dev.new() instead
Works fine for me without dev.new(). I'm on Linux though.
|
3

You can use the library magick with an empty canvas - annotate text and trim of the margins and it will give

out

I tried methods described here and here but they used R's devices like tiff but the strwidth / strheight methods were very unsatisfactory, also aligning the text along a grid or even ggplot2 turned out to still leave unwanted margins or even cut off text.

words <- c("The", "Turing", "test,", "originally", "called", "the", "imitation", 
           "game", "by", "Alan", "Turing", "in", "1949,[2]", "is", "a", 
           "test", "of", "a", "machine's", "ability", "a", "b", "c", "d", 
           "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", 
           "r", "s", "t", "u", "v", "w", "x", "y", "z")

library(magick)

text2tiff <- function(
    text,
    font_size = 500,
    font_family = "Arial",
    backgroundColor = "white",
    textColor = "black")
{

  width <- ceiling(strwidth(text, units = "inches", family = font_family,ps = par(ps = font_size)) * 96 + 200)
  height <- ceiling(strheight(text, units = "inches", family = font_family, ps = par(ps = font_size)) * 96 + 200)
  
  canvas <- image_blank(width = width, height = height, color = backgroundColor)
  
  text_image <- image_annotate(canvas, # Annotate text
                               text, 
                               font = font_family,
                               size = font_size,
                               color = textColor,
                               gravity = "center")

  image_write(image_trim(text_image), # Trim image to remove margins
              path = paste0(gsub("[^[:alnum:]]", "", text), ".tiff"), # take text as img name
              format = "tiff")
}

for(i in 1:length(words)) text2tiff(words[i])
  • For increased image quality, increase the fontsize
  • Adjust the file naming, don't know how long your text / word snippets are, they might exceed your OS' maximum file name

Comments

2

Update: Thanks for your comment: According to the answer of @Tim G we could us ggplot and magick :

library(ggplot2)
library(magick)

plt <- ggplot() +
  geom_text(aes(x = 0, y = 0, label = "hello world"), size = 10) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  theme_void() +
  theme(plot.margin = margin(0, 0, 0, 0, "pt"))

# temporary file
temp_file <- tempfile(fileext = ".tif")
ggsave(temp_file, plot = plt, bg = "white",
       width = 4, height = 2, units = "in", dpi = 300)


img <- image_read(temp_file)
img_cropped <- image_trim(img) # Crop the image to remove whitespace

image_write(img_cropped, "test_cropped.tif") # Save the cropped image

enter image description here

------------------------------------------

First answer:

enter image description here

With `ggplot2` we could remove extra space by setting `expand = c(0,0)` and remove some padding by setting `plot.margin()`:

library(ggplot2)
library(grid)  

plt <- ggplot() +
  geom_text(aes(x = 0, y = 0, label = "hello world"), size = 10) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  theme_void() +
  theme(plot.margin = margin(0, 0, 0, 0, "pt"))

ggsave("test.tif", plot = plt, bg = "white",
       width = 2, height = 1, units = "in", dpi = 300)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.