Overlay images and set transparency R - r

I have two images and I want to overlay one on top of the other such that the top layer is transparent and allows the features in the below layer to show to some extent.
I am searching for this functionality within magic and imager packages but can't seem to get it done. Found a good example at this link but it seems to be using some geographical packages (raster formatting) but the end result is what I want.
Image 1:
Image 2
Desired Result

With ImageMagick you can start with "input1.png" and overlay "input2.png" with a particular amount of transparency. This command in *nix shell format shows the basics...
magick input1.png \
\( input2.png -channel A -evaluate multiply 0.5 +channel \) \
-gravity center -composite result.png
First that reads in the base image. Then inside the parentheses it reads in the overlay image and reduces its alpha channel opacity to 50% with "-evaluate multiply 0.5". Then it composites the semi-transparent overlay onto the base image.
In that command the overlay will be centered. Setting the "-gravity ..." and specifying a horizontal and vertical offset with "-geometry +H+V" just before the composite allows you place the overlay anywhere you like on the base image.
To use this command from a Windows command line, the backslashes that escape the parentheses "\(...\)" should be removed "(...)", and the continued-line backslashes "\" need to be changed to carets "^".

Related

FontForge: Count of Defined Glyphs

I'm fairly new to FontForge, and I just want to merge two fonts for my specific usage.
I know how to merge two fonts from this question and I'm aware of TTF, ... 65535 lookup limits, so I'm trying to Detach & Remove Glyphs... for some glyph ranges, and for this:
I need to know how many glyph is defined, so I can decide which range to detach and remove. It seems fairly simple info, but I can not find where it is shown.
is there any menu or macro to show the current number of defined glyphs in FontForge?
I was looking for the same info. This will give you the number of defined glyphs fairly quickly:
fontforge -lang=ff \
-c 'Open($1); SelectWorthOutputting(); Print($selection)' "$FONTFILE" \
2>/dev/null |tr -d '][' |tr , '\n' |grep -c 1
The SelectWorthOutputting() function fills an array with ones for each defined glyph, so we can print this out and filter it to get the count.
I think found an answer, however it may not be the best approach. as FontForge saves projects in plain text, we can search for StartChar: keywords in the saved project file (project_name.sfd), which each section started by a StartChar: defines one glyphs in the font project. so if we count them we may know how many glyphs has been defined in the font file, for example, in bash the command:
grep -E "^StartChar\:" project_name.sfd | wc -l
would count them for us, and then we may know how many glyphs has been define so far.
This question pops up as first Google hit, so I'll just place this for posterity, but the proper code answer is from #Jerry Penner.
It's also possible from the program GUI to obtain the info in a quick dirty way:
Reduce the number of displayed glyphs via Encoding > Compact menu
Select the last glyph in your font
At the top right below the menu will be displayed the glyph "number" starting from 0 (so in below picture example the total count is 270).
Note: the number displayed is for the current compact view, not the actual glyph number in the font.

Explanation of all uses of the escape character in Unix terminals

I'd like to know all of the special uses of the escape character in Unix terminals, by which I mean the character \e. I know about colors and \ec which clears the screen in a scrollable terminal, what others are there and what do they do? For instance, what does this string do: echo -e "\e[3;12r\e[3H"?
That particular example (both by the way are VT100 escapes where \e is used in some shells to mean the ASCII escape character):
sets scrolling margins to lines 3 through 12 (starting from 1), and
moves the cursor to the beginning of line 3 (putting it at the top of the scrolling margins)
By the way, \ec resets the terminal. Clearing the screen is just one of several things done.
Further reading:
XTerm Control Sequences
vt100.net

Output percentage white content of image using imagemagick R

I am rendering two versions of the same web page and would like to know the most efficient use of space. I aim to do this by outputting the pages as images and looking at the percentage of the page that is white. The less white, the more efficient (my criteria!)
How can imagemagick in R be used to output the % white content of an image? I see it is possible through command line, but can't find anything on doing this inside R.
If you want exactly white (not lightgray or something, too), you can do it like this:
download.file("https://www.gravatar.com/avatar/b1cdf616876083f7c5ec1a49fc357530?s=328&d=identicon&r=PG", tf <- tempfile(fileext = ".png"), mode="wb")
f <- function(fn) {
require(magick)
img <- image_read(tf)
r <- as.raster(img)
sum(substr(tolower(r), 1, 7) == "#ffffff") / length(r) * 100
}
f(tf)
# [1] 44.33931
Sorry, I can't help you with the R side of things, but a suitable command for the command line that you may be able to adapt is as follows:
convert image.png -fill black +opaque white -format "%[fx:mean*100]" info:
That replaces with black (-fill black) everything that is not white (+opaque white) and then calculates the percentage white of the result.
Examples
convert -size 100x10 xc:white -bordercolor red -border 50 a.png # Generate test image
convert a.png -fill black +opaque white -format "%[fx:mean*100]" info: # Determine whites
4.5454
convert -size 100x10 xc:white -bordercolor blue -border 10 a.png # Generate test image
convert a.png -fill black +opaque white -format "%[fx:mean*100]" info: # Determine whites
27.7778
In ImageMagick command line, Mark's second line works without having to know the other color. So it works for both images without his first line of code.
convert VQZ9Y.png -fill black +opaque white -format "%[fx:100*mean]\%\n" info:
4.54545%
convert Qpdg8.png -fill black +opaque white -format "%[fx:100*mean]\%\n" info:
27.7778%

What is the correct index position of the Convert command's options of GraphicsMagick

I am confused about the covert command of GraphicsMagick.
According to the documentation (http://www.graphicsmagick.org/convert.html), the synopsis of convert command is:
convert [ options ... ] input_file output_file
According to the above synopsis, I can execute the convert command in the following way:
Example 1:
gm convert -strip -scale x400 inputFile.jpg outputFile.jpg
The above command is executed successfully. Now, I ran the the same command with different index position of the options.
Example 2:
gm convert inputFile.jpg -strip -scale x400 outputFile.jpg
The above example executed successfully. The above example's index position of the options are different, does not match against the synopsis.
My question is, which example is correct?
I fear the answer is partly philosophical, partly folklore, partly opinion and partly flexibility of GraphicsMagick and partly rigidity of manpages. Let me explain...
To understand the answer, you need to differentiate between settings and operators. #KurtPfeifle did a great job here which, for the sake of completeness, I will summarise as follows:
Some parameters are settings - they set and retain their value till the end of the command or till changed. For example, -size sets the size of all canvases created after it is set on the commandline. Until the point it is first set, the default canvas size is 1x1, after it is set it remains till the end of the command.
Some parameters are operators - they generally perform some image processing, such as thresholding or converting to greyscale. These ones operate on all images currently in the image list.
Maybe an example will help. First, we use the -size setting:
gm convert -background none xc:red -size 5x5 xc:lime xc:blue -size 10x10 xc:magenta +append setting.png
Initially, the default size was 1x1, so the red canvas comes out at 1x1. Then I changed the setting and it remained at 5x5 for the lime and blue canvases till I changed it to 10x10 for the magenta one. Those were settings and they set things up for the images yet to come.
Now, I do a similar thing with the -colorspace operator:
gm convert xc:red xc:lime xc:blue -colorspace gray +append operator.png
And you see that the -colorspace operator has changed all the images that were already in the image list, i.e. they process images that have previously been added to the list.
If you like, you could simplify things and say "settings apply to all that is to come, and operators apply to all that has gone". Although I am sure there are counter-examples.
The order of the commandline options was rationalised a few years back and GraphicsMagick permits some flexibility to retain some backward compatibility. That is probably the best explanation of your actual question.
So, in general, you should declare settings as soon as posssible on the commandline and apply operators at the point that makes most sense depending on what images are currently in your list.
Another reason is that it would be very cumbersome, to the point of unintelligibility, if all the GraphicsMagick combinations and permutations were put in a conventional manpage. Can you imagine:
gm convert [[[operators|settings]|[settings|operators]] image] [[settings|operators]|[operators]] ...

How to create a transparent image with GraphicsMagick CLI?

I'm coming up empty handed while trying to find out how to modify the opacity of an image using the GraphicsMagick CLI. I'm simply trying to input a standard image and convert it to the corresponding image with a reduced opacity. I'm running GraphicsMagick 1.3.14. Using ImageMagick, I can issue the command:
convert input.png -alpha set -channel a -evaluate set 50% +channel output.png
However, the -alpha option (among others) is unrecognized as a valid GM convert option (convert option reference here). Any help would be greatly appreciated.
This question was cross-posted on SuperUser, as I later thought SuperUser might be the more appropriate outlet. To quote Bob Friesenhahn:
The equivalent in GraphicsMagick is
gm convert input.png -operator Opacity Multiply 0.5 output.png
or
gm convert input.png -operator Opacity Assign 50% output.png
depending on what you really want to do (modulate or assign).
You should add -matte prior to -operator if you don't know if the image already has an active opacity channel.
Bob

Resources