I am using magick library in R. I want to add watermark on some pictures.
I used image_annotatefunction as below.
img <- image_read("C:\\Users\\Maydin\\Desktop\\manzara.png")
image_annotate(img, "my watermark", gravity = "northwest", location = "+200+275",
degrees = -30, size =50, font = NULL, color = "transparent",
strokecolor = "gray90", boxcolor = NULL)
At the end, the output looks like this;
However, what I want to have is something like this ,
Is that doable in magick in R?
For instance, this
download.file("https://i.stack.imgur.com/7X5To.png", tf<-tempfile(fileext = ".png"), mode="wb")
library(magick)
img <- image_read(tf)
library(extrafont)
truetype_path <- paste0("#", subset(fonttable(), FullName=="Matura MT Script Capitals", fontfile)[1,])
image_annotate(img, "my watermark", gravity = "northwest", location = "+70+220",
degrees = -30, size = 80, font = truetype_path, color = "#FFFFFF66",
strokecolor = NULL, boxcolor = NULL)
gives this image:
I.e., choose a nice font like maybe Matura MT Script Capitals, tell image_annotate where to find it on your harddrive, adjust the opacity in the color argument - et voila. The font does not drop a shadow or show a relief, but maybe you can emulate this by plotting the text two times, the dark shadow one with a little offset to the other light one.
#lukA nicely demonstrates a solution using extrafonts package with magick package, but it looks like you can refer to the font by name within image_annotate() without the clever lookup of the full path. Use extrafonts::fonttable() to find the name.
library(extrafonts)
library(magick)
#download original example file
download.file("https://i.stack.imgur.com/7X5To.png", tf<-tempfile(fileext = ".png"), mode="wb")
img <- image_read(tf)
#Use stroke to create an outline of the text with 50% alpha
magick::image_annotate(img, "Preview", location = "+100+175", degrees = -30, size=75, weight=700, font = "MonotypeCorsiva" , color = "transparent",
strokecolor = "#00000050", boxcolor = NULL)
Related
I was trying to embed an image on an echart plot. The image is coming perfectly if I provide a link of the image, but while I am trying to call the same image from my directory, the image is not coming in the plot.
I was tring the example provided in this website and the code I am using is as below.
library( echarts4r)
cars %>%
e_charts(speed) %>%
e_scatter(dist) %>%
e_image_g(
right = 20,
top = 20,
z = -999,
style = list(
image = "~/Rlogo.png",
width = 150,
height = 150,
opacity = .6
)
)
So how can I capture this Rlogo image from my local directory to the plot?
Is it possible to insert external images into a Powerpoint with officer without changing the image dimensions?
This is the code I have now where x is an rpptx object:
library(officer)
image_url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Instagram_logo_2016.svg/768px-Instagram_logo_2016.svg.png"
t_file <- tempfile(pattern = "logo_", fileext = ".png")
download.file(image_url, destfile = t_file)
x <- x %>%
ph_with(value = external_img(t_file), location = ph_location(left = 9.4, top = 4.6))
This will change the dimensions of the image to 4x3 inches (the default for ph_location()).
I know you can use external R packages to get image metadata then pass this to the function so the actual image dimensions are used, but I don't want to add any more external R package dependencies.
You can always specify the image dimensions within your external_img function. I'm assuming you know what dimensions you want, so say your dimensions are width = 9, height = 5 it would look something like this:
library(officer)
image_url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Instagram_logo_2016.svg/768px-Instagram_logo_2016.svg.png"
t_file <- tempfile(pattern = "logo_", fileext = ".png")
download.file(image_url, destfile = t_file)
x <- x %>%
#specify height and width
ph_with(value = external_img(t_file, width = 9, height = 5), location = ph_location(left = 9.4, top = 4.6))
Hope that is helpful!
I'm using webshot to save a png file from HTML :
p <- plot_ly(plotly::wind, r = ~r, t = ~t) %>% add_area(color = ~nms)%>%
layout(radialaxis = list(ticksuffix = "%"), orientation = 270)
saveWidget(as.widget(p), "temp.html")
webshot("temp.html",file = "temp.png",cliprect = "viewport")
This is giving me a plot like this. It returns a small image in the upper left corner. What should I do to have a full-size image in a center?
I'm facing an issue while trying to format/style an existing excel file with data.
I want to change the format of a numeric cell, add background color and a border.
require(XLConnect)
wb <- loadWorkbook("example.xlsx", create = FALSE)
cs <- createCellStyle(wb)
setDataFormat(cs, format = "###,##0")
setFillBackgroundColor(cs, color = XLC$"COLOR.YELLOW")
setBorder(cs, side = "all", type = XLC$"BORDER.THIN",
color = XLC$"COLOR.BLACK")
setCellStyle(wb, sheet = "PSNB", row = 24, col = 3, cellstyle = cs)
saveWorkbook(wb)
After running the above code, the cell doesn't have the background color (Yellow) and the data format persisted.
When i double click on the cell, i can see the background color changing to yellow and the commas(Ex: 100,000) appearing.
Any help would be greatly appreciated!
I'm using XLConnect 0.2-13
I think you are rather looking to set the fill foreground color instead of the fill background color. Background colors are usually only needed in conjunction with non-solid fill patterns (see setFillPattern).
The following may do what you are looking for:
require(XLConnect)
wb <- loadWorkbook("example.xlsx", create = FALSE)
cs <- createCellStyle(wb)
setDataFormat(cs, format = "###,##0")
setFillForegroundColor(cs, color = XLC$"COLOR.YELLOW")
setFillPattern(cs, fill = XLC$FILL.SOLID_FOREGROUND)
setBorder(cs, side = "all", type = XLC$"BORDER.THIN",
color = XLC$"COLOR.BLACK")
setCellStyle(wb, sheet = "PSNB", row = 24,col = 3, cellstyle = cs)
saveWorkbook(wb)
Note the use of setFillForegroundColor and setFillPattern instead of setFillBackgroundColor.
I want to indent a (flex)table created with the ReporterRs package. Here's an example:
library(ReporteRs)
df <- data.frame(Column1 = 1:5, Column2 = c("a","b","c","d","e"))
Mydoc = docx(title="A testdoc for testing tables")
options('ReporteRs-fontsize'=10, 'ReporteRs-default-font'='Arial')
FTab = FlexTable( data = df, add.rownames = FALSE, header.columns = TRUE,
body.text.props = textProperties( font.size = 10,font.family = "Arial" ),
header.text.props = textProperties( font.size = 10,font.family = "Arial", font.weight = "bold"))
FTab = setFlexTableBorders(FTab,inner.vertical = borderNone(),inner.horizontal = borderNone(),
outer.vertical = borderNone(),
outer.horizontal = borderProperties(color = "black", style = "solid", width = 1))
Mydoc = addFlexTable(Mydoc, FTab)
nu <- format(Sys.time(), "%Y%m%d%H%M")
writeDoc(Mydoc, paste0("testreport_",nu,".docx"))
This creates a docx with a left aligned table. I want the table to move 1.5 cm to the right. So no center or right alignment, but an indentation of 1.5 cm. Is this possible? For text, I can use a pre-defined style that indents 1.5 cm, but for tables that doesn't seem possible. Or is it?
As a workaround, I could add an extra column at the left, without any borders or text. But I prefer a neat solution.
From the package documentation, unless you want to write a patch, it seems you'd better go with the invisible column:
Function addFlexTable
add FlexTable to document objects.
Word and html document
Add a FlexTable object in a document object with the function addFlexTable.
Expected arguments are:
the document object
the FlexTable object
eventually a parProperties object to define alignement. Note that with docx objects, only alignment will be used, if you’d like to add space around a table, specify padding on preceding and or following paragraph.