ImageMagick not rendering all Unicode characters - linux

I want to render small single character images with ImageMagick.
I'm calling it like this:
echo -n "\u1407" | convert -background black -fill white \
-font /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf
-pointsize 12 label:#- gif:1407.gif
When I just echo on my terminal, that has this font (DejaVu-Sans-Mono) set, I see this triangle: ᐇ but in the GIF is just a question mark. No special markings, just a question mark. It works for some other characters, like these special parens: ⟨⟩ at 27e8 and 27e9, but the next pair isn't working again.
What do I need to do to enable all characters that the font supplies? Did I set the wrong font?
My distribution is LMDE.

Working with bash & unicode, I always need to step back, and do an extra step.
echo -n ᐇ | hexdump
This gives me the correct hex-triplet
0000000 e1 90 87
So then my unicode escape sequence in bash would be..
echo -ne "\xE1\x90\x87"
(omitting the 0x0A which is the line-feed)
echo -ne "\xE1\x90\x87" | convert -background black -fill white \
-font /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf \
-pointsize 12 label:#- gif:1407.gif
However this produces the following image , as the character is not apart of the DejaVu Sans Mono set, but it is apart of the DejaVu Sans.
echo -ne "\xE1\x90\x87" | convert -background black -fill white \
-font /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf \
-pointsize 12 label:#- gif:1407.gif
Additional info about ᐇ
There could be a perfectly valid reason why this glyph is not included. A polite thing would be to reach out to the DejaVu team, and inquire if this is a bug, or included elsewhere. IRC & Mailing lists are a good start.

Related

How to avoid header truncation on the tiff using GhostScript. Convert ps to tiff

In this section I add the header file to the top of the tiff file.
echo "/CourierLatin1 findfont 8 scalefont setfont" >>${PS}
echo "40 2 moveto (${DATE}) show" >>${PS}
echo "200 2 moveto (${NAME}) show" >>${PS}
echo "400 2 moveto (${FROM}) show" >>${PS}
echo "510 2 moveto (${PAGEINFO}) show showpage" >>${PS}
${BIN}/gs -dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=tiffg4 -sOutputFile=${HDR_FILE} \
-dDEVICEWITHPOINTS=612 -dDEVICEHEIGHTPOINTS=11 ${PS} >>${LOG_FILE}
if [ $? -ne 0 ] ; then
cleanup
echo "${BIN}/gs failed" >>${LOG_FILE}
exit 1
fi
# Overlay the header onto the tiff page
SIZE=`${BIN}/tiffinfo ${f} | grep "Image Width:" | sed -e "s/Image Width//" | sed -e "s/Image Length//" | sed -e "s/ //g"`
WIDTH=`echo ${SIZE} | cut -d: -f2`
LENGTH=`echo ${SIZE} | cut -d: -f3`
OFFSET=`expr ${LENGTH} - 2156`
# no shifting of header line up or down is needed
OFFSET=0
if [ ${OFFSET} -lt 0 ] ; then
OFFSET=0
fi
echo "Adding ${HDR_FILE} to ${f} ${WIDTH}x${LENGTH} at 0,${OFFSET}" >>${LOG_FILE}
${CONVERT} tiff:${f} -draw "image over 0,${OFFSET} 0,0 'tiff:${HDR_FILE}'" -compress Group4 -flatten +matte tiff:${f} >>${LOG_FILE}
if [ $? -ne 0 ] ; then
cleanup
echo "${CONVERT} failed" >>${LOG_FILE}
exit 1
fi
Can come to me in any size page. So I first translate tiff post script to size.
I convert tiff again. Can see the code as below:
# Nasty compromise - Have to convert the tiff file to ps file so that I can turn around
# and create a 'faxable' tiff file (one that Radisys will accept)
${BIN}/tiff2ps -a -h11.0 -w8.5 ${LOCAL_FILE} >${PS_FILE}
if [ $? -ne 0 ] ; then
cleanup
echo "${BIN}/tiff2ps failed" >>${LOG_FILE}
exit 1
fi
# Part 2 of compromise
${BIN}/gs -dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=tiffg4 -sPAPERSIZE=letter -r204x196 -sOutputFile=${LOCAL_FILE} ${PS_FILE} >>${LOG_FILE} 2>&1
if [ $? -ne 0 ] ; then
cp ${BKUP_FILE} ${ORIG_FILE}
cleanup
echo "${BIN}/gs ps2tiff failed" >>${LOG_FILE}
exit 1
fi
Output tiff is given the Radisys. I dont understand Why header cut for this output.pdf.
You can see output fax image as below. why is Header cutted ?
Looks to me like you are printing the 'header' first, then putting the image from the TIFF file (in this case, the text 'FAX') on top of it.
TIFF files aren't masks, so the white space is not transparent. When you turn a TIFF file into PostScript it encapsulates the TIFF bitmap image as a PostScript image. Where the white space in the image lies on top of the 'header', it prints over it, obscuring part of the 'header'.
Its a little hard to be sure what's going on, I'm not a shell script expert, but it appears you first use Ghostscript to render a piece of PostScript to TIFF. That is your 'header'.
You then invoke $(CONVERT) which I'm going to guess is ImageMagick's convert utility to combine the header and some original TIFF file.
I suspect at this point is where your problem occurs, I'm guessing that you need to put the two tiff files in the reverse order so that the header is placed 'on top' of the original, instead of 'under' it (in Z-order terms. In your invocation of $(CONVERT) try swapping $(f) and $(HDR_FILE).
You can of course check the TIFF file at that point, presumably, to see if all is well.
You then take that TIFF file, use tiff2ps (which I think is part of libtiff) to wrap the image up as PostScript, and then use Ghostscript to re-render the image at a different (fax) resolution.
I'd say that you are using the wrong tool for that last step anyway. You already have a bitmap, you'd be better off using an image manipulation application to downsample it to the desired resolution. While your solution undoubtedly works, I would suspect that something like ImageMagick would do a better job.
At the very least, checking the TIFF file before you send it through tiff2ps should tell you whether the result is correct at that point.

How do I apply formatting like italic or bold to text using ANSI escaping sequences in a console?

How do I format text with ANSI escaping?
Like make things italic or bold and maybe strikethrough and super script.
How I format like, make things italic or bold using ANSI terminal escape codes?
Bold: Use ESC[1m
Italic, Strike-through and Superscript are not supported.
Some terminals support additional sequences. For example in a Gnome Terminal you can use:
echo -e "\e[1mbold\e[0m"
echo -e "\e[3mitalic\e[0m"
echo -e "\e[4munderline\e[0m"
echo -e "\e[9mstrikethrough\e[0m"
echo -e "\e[31mHello World\e[0m"
echo -e "\x1B[31mHello World\e[0m"
Source How to do: underline, bold, italic, strikethrough, color, background, and size in Gnome Terminal?, answer by Sylvain Pineau
Further Reading
ANSI Escape sequences

Appending images using ImageMagick convert

So I have a bunch of images that I wish to append vertically:
convert *.png -append output.png
However I ran into two problems:
Not all the images are of the same width. Hence images smaller in width than the widest image have white space trailing after them because they are left-aligned.
There's no spacing between the images.
How do you center align all the images and specify spacing between them?
Just use ImageMagick's montage utility. Align the images with the -gravity & -extent options, and adjust the spacing with -geometry.
montage fishscales.png circles.png verticalsaw.png \
-tile 1x -geometry +10+10 \
-gravity Center -extent 120 \
out.png
My approach would be a shell script as follows:
1. Run a loop over all your images and find the width of the widest (using ImageMagick `identify`) - say 8 to 10 lines of shell script
2. Create a transparent "spacer image" the same width as the widest image and the height you want for vertically spacing images - one line of shell script
3. Run a loop over all your images first adding the transparent "spacer" to the bottom of the existing output image then compositing images that are narrower than your widest image onto a transparent background the width of your widest image - then appending that to the output image - maybe 15 lines of shell script.
Here is the output with three images:
Red=80px wide
Green=180 px wide
Blue=190px wide
Could that sort of approach work for you - I could probably code it over a day or two between other things!
Here is what I mean:
#!/bin/bash
# User-editable vertical spacing between images
SPACING=10
function centre() {
echo DEBUG: Centering $1
TEMP=$$tmp.png
w=$(convert "$1" -ping -format "%w" info:)
h=$(convert "$1" -ping -format "%h" info:)
convert -size ${MAXW}x${h} xc:"rgba(0,0,0,0)" PNG32:$TEMP
composite -resize '1x1<' -gravity center "$1" $TEMP $TEMP
if [ $2 -eq 0 ]; then
mv $TEMP output.png
else
convert output.png $TEMP -append output.png
rm $TEMP
fi
}
# Step 1 - determine widest image and save width in MAXW
MAXW=0
for i in *.jpg; do
w=$(convert "$i" -ping -format "%w" info:)
[[ $w -gt $MAXW ]] && MAXW=$w
echo DEBUG: Image $i width is $w
done
echo DEBUG: Widest image is $MAXW
# Step 2 - Create transparent spacer image, with width MAXW
convert -size ${MAXW}x${SPACING} xc:"rgba(0,0,0,0)" PNG32:spacer.png
# Step 3 - Build output image
n=0
for i in *.jpg; do
# Add a vertical spacer if not first image
[[ $n -ne 0 ]] && convert output.png spacer.png -append output.png
# Centre image horizontally and append to output
centre "$i" $n
((n++))
done
Another, completely different option would be to add all heights of the N images and create a transparent canvas the width of the widest image and the height of all the images combined plus (N-1)* vertical spacing. Then overlay the images into the canvas at the corrrect place - this involves more maths and less processing and reprocessing of images which could mean that this approach would lose less quality than the one I have suggested. I would do this approach with PerlMagick as it is better for math than bash.

Color Linux command output [duplicate]

This question already has answers here:
How to change the output color of echo in Linux
(33 answers)
How do I output coloured text to a Linux terminal?
(16 answers)
Closed 1 year ago.
For example, I'd like to color the output of the locate command so it's easily distinguished from the other terminal text.
It should work something like this:
locate -bir pdf | some_command_to_color_the_result
Coloring shouldn't be limited for the locate command only: I need a generic solution that colors text using pipelines, for example feeding it the output of grep or cat.
If there's no built-in Linux command, any hints on how to create one are appreciated.
You can use escape sequences to change the font color of any output to the bash shell. Here are some of the color codes you'll need:
BLACK="\033[30m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
PINK="\033[35m"
CYAN="\033[36m"
WHITE="\033[37m"
NORMAL="\033[0;39m"
Once these are defined, you can use them in normal echo commands. For instance:
echo -e $GREEN this text is green $NORMAL and this is normal
Note that the -e is not always necessary, but on some OSs (incl. osx) is required for to enable escape sequences.
Given these definitions you can build scripts and pipes to color the output from other commands. Here is a complete example I use to color the output from svn up:
#!/bin/bash
BLACK="\033[30m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
PINK="\033[35m"
CYAN="\033[36m"
WHITE="\033[37m"
NORMAL="\033[0;39m"
TMPFILE=.cvsup.tmp
svn up > $TMPFILE
svn status >> $TMPFILE
printf $YELLOW
grep -e ^"\? " -e ^"I " $TMPFILE
printf $GREEN
grep -e ^"R " -e ^"U " -e ^"G " $TMPFILE
printf $BLUE
grep -e ^"M " -e ^"E " $TMPFILE
printf $RED
grep -e ^"C " -e ^"! " -e ^"X " -e ^"~ " $TMPFILE
printf $PINK
grep ^"R " $TMPFILE
printf $PINK
grep ^"D " $TMPFILE
printf $CYAN
grep ^"A " $TMPFILE
printf $NORMAL
rm $TMPFILE
You can also look at tput.
_Esc_="$( printf '\033' )"
_norm_="${Esc}[0m" #returns to "normal"
_bold_="${Esc}[0;1m" #set bold
_red_="${Esc}[0;31m" #set red
_boldred_="${Esc}[0;1;31m" #set bold, and set red.
somecommand | sed -e "s/someregexp/${_boldred_}&${_norm_}/g" # will color any occurence of someregexp in Bold red
printf "%s" "$_red_" ; locate something ; printf "%s" "$_norm_" # will color output of locate something in red
#I (ab)use printf "%s" "something", as it's more portable than echo,and easy to modify
There are many other ways (create a function/script that can colorize a regexp, for example, and then : somecommand | colorize -c _green_ 'foo.*bar' 'other' )
The main tool for that is of course lolcat!
locate -bir pdf | lolcat
To install:
sudo apt install lolcat
See man lolcat for customizations.
As suggested by Jonathan Leffler, comment posted as an anwser:
grep --color will provide colour
The following answered my question:
1- I create an alias in my .bashrc
alias color='grep --color .'
2- Then whenever I want to color the pipeline text output I use color alias like:
locate -bir pdf | color
This will color the output to red
I prefer use highlight utility:
highlight -O xterm256 -S sh
-S sh here means treats the input as shell script syntax.
More info: http://www.andre-simon.de/
I set it as an alias through ~/.bashrc:
There is a far better way to achieve customizable coloring:
colorit
You can use it as shown in other answers via some_command | colorit but it is nicely configurable over the .coloritrc. In mine I have stuff like
dnl Define some useful color variables
define(`red', `1')
define(`green', `2')
define(`magenta', `5')
dnl
dnl Mark macro arguments: regexp foreground-color [background-color]
dnl
define(`mark', ``mark "$1"'' `ifelse(`$#', `3', ``"\033[3$2;4$3m"'',
``"\033[3$2m"'')' `"\033[m"')
dnl
divert
mark(`warning', magenta)
mark(`Warning', magenta)
mark(`Traceback', magenta)
mark(`Error', red)
mark(`FAIL', red)
mark(`ERROR', red)
mark(`XFAIL', green)
mark(`ok', green)
mark(`OK', green)
mark(`PASS', green)
and use it all the time for coloring compiler output and similar stuff. See my .coloritrc for more.
I think the hl command available on git hub might help you :
have a look at http://www.flashnux.com/notes/page_000022_US.html
You should have a look at the hl command available on git hub:
git clone http://github.com/mbornet-hl/hl
and on :
http://www.flashnux.com/notes/page_000022_US.html
hl is a Linux command written in C, especially designed to color a text file or the output of a command. You can use up to 42 colors simultaneously, and use a configuration file to simplify command lines. You can colorize the output of every command that can be piped to another one. And if you know what regular expressions are, it will be very easy for you to use. You can use the man page to understand how to use it.
Use the tput command.
Most terminals support 8 foreground text colors and 8 background colors (though some support as many as 256). Using the setaf and setab capabilities, we can set the foreground and background colors. The exact rendering of colors is a little hard to predict. Many desktop managers impose "system colors" on terminal windows, thereby modifying foreground and background colors from the standard. Despite this, here are what the colors should be:
Value Color
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White
8 Not used
9 Reset to default color
Actual example: set color to red, cat and then change color back:
tput setaf 1; cat /proc/meminfo ; tput setaf 9

Using Stacks in ImageMagick to Selectively Blur Part of the Image

I'm trying to annotate some text onto a base image with a dropshadow. I don't like the results I get from using the -shadow option, so I'm putting down the text, blurring it, then putting down the text again in white, a few pixels offset from the shadow. Here's the command I'm using:
convert base_image.jpg \
-font TT0590M_.ttf \
-fill gray30 \
-annotate +0+0 '' -gravity North \
-annotate +72+32 'ABCDEFGHIJKLM' \
-blur 0x4 \
-fill white \
-annotate +72+27 'ABCDEFGHIJKLM' \
combined.png
My problem is that the -blur option is blurring not only the first layer of text, but the underlying base image as well. I only want the first layer of text to blur, not the base image.
I read up a little on using stacks and tried to isolate the first layer of text and the blur command using \( \) around that part, such as in the following:
convert base_image.jpg \
-font TT0590M_.ttf \
-fill gray30 \
-annotate +0+0 '' -gravity North \
\( -annotate +72+32 'ABCDEFGHIJKLM' \
-blur 0x4 \) \
-fill white \
-annotate +72+27 'ABCDEFGHIJKLM' \
combined.png
The results are the same - both the text shadow and the underlying base image are getting blured. I'm afraid I'm not having much luck understanding stacks or what other commands I should be using to get the effect I'm after.
As so often happens, I've continued to bang away at this since posting the question and I've managed to solve it on my own.
The important change is that I started with a blank, transparent canvas instead of starting with the base image. After I get the text right, I insert the base image into the stack, swap the order of the two images in the stack, and then composite them with a compose type of "screen", which lays the one overtop of the other.
One other note of import: the -channel RGBA is needed so that the blurring works in conjunction with the transparency of the text layer, due to a peculiarity in the way that IM works. Why this is necessary is explained on this page.
Also, on Windows systems (.bat file instead of shell script), the single quotes need to be double quotes and the backslashes "\" need to be carets "^", or it all comes crashing down.
The script below is the final, working result (*nix version):
convert \
-size 500x500 xc:transparent \
-font TT0590M_.ttf \
-annotate +0+0 '' -gravity North \
-fill gray30 \
-annotate +75+35 'ABCDEFGHIJKLMNOPQR\nABCDEFGHIJKLMNOPQR' \
-channel RGBA \
-blur 0x4 \
-fill white \
-annotate +72+30 'ABCDEFGHIJKLMNOPQR\nABCDEFGHIJKLMNOPQR' \
-insert 0 base_image.jpg \
-swap 0,1 \
-composite -compose screen \
combined.png

Resources