ASS Subtitles: How to position text below each word in a line - subtitle

I want to store some specific information (for example a number) below each word in a line of an ASS subtitle like the following
[Script Info]
; Script generated by Aegisub
; http://www.aegisub.org
ScriptType: v4.00+
Collisions: Normal
PlayResY: 600
PlayDepth: 0
Timer: 100,0000
Video Aspect Ratio: 0
Video Zoom: 6
Video Position: 0
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: DefaultVCD, Arial,60,&H00B4FCFC,&H00B4FCFC,&H00000008,&H80000008,-1,0,0,0,100,100,0.00,0.00,1,1.00,2.00,2,30,30,30,0
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:01.18,0:00:06.85,DefaultVCD, NTP,0000,0000,0000,,{\pos(400,570)}This is the first line\NAnd this is the second line
Here we have the two lines in the subtitle:
This is the first line
And this is the second line
What I want to achieve is autopositioning some extra information that belongs to each word to get this result:
This is the first line
1 2 3 4 5
And this is the second line
6 7 8 9 10 11
The number should be placed exactly below the word and left aligned, so that the word and the number start at the same x position.
I have already studied the spec but have not found a way to implement this.
I don't want to manually position the information that should show below each word, but instead I want libass to automatically find the appropriate x position and render it there.
Is there a way to achieve this?

Related

Subfigures in latex never shrinks to be on one row (pfd file)

I'm trying to adjust two subfigures into one row. This is my code:
\begin{figure}[ht]
\centering
\caption{Migration rates and nightlights in Albania in 1992-1995}
\begin{subfigure}[a]{0.3\textwidth}
\caption{Emigration rate before 1995 at the municipality level}
\label{fig: migration_rate}
\includegraphics[width=\linewidth, trim = 5cm 0cm 5cm 0cm, clip]{Figures/migration_rate.pdf}
\end{subfigure}
\begin{subfigure}[b]{0.3\textwidth}
\caption{Night luminosity in 1992}
\label{fig: nighlights}
\includegraphics[width=\linewidth, trim = 5cm 0cm 5cm 0cm, clip]{Figures/nightlight.pdf}
\end{subfigure}
\caption*{\footnotesize{\textbf{Notes}:}}.
\end{figure}
enter image description here
Even if I shrink the subfigure to tiny size (for example {0.01\textwidth}), the two always appear in one column. I have deep blank space on the left and right of both figures that I have tried to trim. Including or excluding "trim" does not change much.
How do I get the two into one row?
Thank you!

Moviepy - Place textclips at relative position

How do I place a textclip, right below the other? I require to place an image at the left of the video and two lines of text to the right of image, that are placed one below the other.
Something like this:
I tried to use set_position, but doesn't scale well for different videos (of different resolutions). By adjusting the arguments of set_position, I am able to place the textclips one below the other without gap in one resolution, but when I go to a higher resolution video, it shows a gap (I understand why the gap comes, but not sure how to prevent it)
txt_clip1 = TextClip("This is line 1 of text", fontsize = 12, color = 'white', bg_color='black')
txt_clip1 = txt_clip1.set_duration(7).set_start(0).set_end(7)
txt_clip1 = txt_clip1.set_position((0.1,0.90), relative=True).set_opacity(0.6)
txt_clip2 = TextClip("This is line 2 of the text, smaller font", fontsize = 8, color = 'white', bg_color='black')
txt_clip2 = txt_clip2.set_duration(7).set_start(0).set_end(7)
txt_clip2 = txt_clip2.set_position((0.1,0.93), relative=True).set_opacity(0.6)
I tried to insert a new line character in the text, but that doesn't suit me because the second line of text has different font properties.
Hopefully this will help you
txt_clip1 = TextClip(
"Cool effect 2nd line", color="black", bg_color="red", font="Amiri-Bold", kerning=5, fontsize=20
)
cvc = CompositeVideoClip([txt_clip1.set_position("East")], size=screensize)
txt_clip1 = txt_clip1.set_position((5,35))
.set_position mother take to arguments 1st is the position from the left screen and second is the position from the top
so your code is like something
txt_clip1 = txt_clip1.set_position((5,500))
txt_clip2 = txt_clip2.set_position((5,535))
It will place the text in the position you want.
Hopefully, this will help.
Happy coding

SwiftUI align SF Symbol as text with a text

I need to display a SF Symbol (image) and a text in a view (as a title). As the text could be quite long, I want that it passes under the image for second line.
Expected result (almost!):
I first tried a HStack with image and text. But text doesn't go under image: Second line starts with a left 'padding' (corresponding to left image space).
So I did this code (to get previous screenshot):
HStack(alignment:.top) {
Text(Image(systemName: "circle.hexagongrid.circle"))
.font(.caption)
.foregroundColor(.pink)
+
Text("A long text as example")
.font(.title2).bold()
.foregroundColor(.pink)
}
.frame(width:140)
But I want to align image with center of first line. Center of image should be vertically in middle of top of A and bottom of g.
It is important that my second (and more) line passes under my image. And I don't want a bigger font for image.
I don’t know if you can force the image to automatically be centred in all cases, but you can add a .baselineOffset() modifier to the image-in-text to shift the image upwards by a fixed amount. For example:
Text(Image(systemName: "circle.hexagongrid.circle"))
.font(.caption)
.foregroundColor(.pink)
.baselineOffset(3.0)
+
Text("A long text as example")
.font(.title2).bold()
.foregroundColor(.pink)
Once you have hit upon the right offset amount for the default text size, you might be able to accommodate accessibility variations by making the offset size relative to a base size, e.g.
Struct MyView: View {
#ScaledMetric(relativeTo: .title2) var baselineOffset: CGFloat = 3.0
// and in your body…
.baselineOffset(baselineOffset)
Firstly, you can remove the HStack because it is redundant. You can just replace it with brackets () to encapsulate the new Text, so you can use more modifiers like for setting the width.
The main solution is to use baselineOffset(_:) which means you can offset your image from the baseline of Text. Below, we only offset by 1, but this can be any value you want (including decimal numbers, e.g. 0.99).
Code:
struct ContentView: View {
var body: some View {
(
Text(Image(systemName: "circle.hexagongrid.circle"))
.font(.caption)
.foregroundColor(.pink)
.baselineOffset(1)
+
Text("A long text as example")
.font(.title2)
.bold()
.foregroundColor(.pink)
)
.frame(width: 140)
}
}
Result:
Bonus: you can move your foregroundColor(.pink) modifier after the Texts have been combined, so you don't have to duplicate that.
Have you tried embedding Image in Text?
Text("Super star \(Image(systemName: "star"))")

Using "gdal_translate" from .TIF to .JPG how to set background to white?

I tried using the following line:
gdal_translate -of jpeg -a_nodata 0 -b 1 -b 2 -b 3 c:\myfolder\mytif.tif c:\myfolder\myNewtif.jpg
This produces the image with wanted specs, but turns the background to black (transparency?), eventhough the original has white. Can I accomplish a white background with just gdal_translate?
File dump of a file with mask:
https://filebin.net/f15v63to2x3cc4z0
File dump of a file with no a mask: https://filebin.net/kc940hqotcoeny0w
gdalinfo output of a Tif that produces white background as expected:
Driver: GTiff/GeoTIFF
Files: test.tif
Size is 4799, 3196
Metadata:
TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
TIFFTAG_XRESOLUTION=300
TIFFTAG_YRESOLUTION=300
Image Structure Metadata:
COMPRESSION=LZW
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 3196.0)
Upper Right ( 4799.0, 0.0)
Lower Right ( 4799.0, 3196.0)
Center ( 2399.5, 1598.0)
Band 1 Block=4799x1 Type=Byte, ColorInterp=Red
Band 2 Block=4799x1 Type=Byte, ColorInterp=Green
Band 3 Block=4799x1 Type=Byte, ColorInterp=Blue
Tif that produces black background:
Warning 1: TIFFFetchNormalTag:Incompatible type for "RichTIFFIPTC"; tag ignored
Driver: GTiff/GeoTIFF
Files: 100011_1.tif
Size is 1640, 2401
Metadata:
TIFFTAG_DATETIME=2020:01:13 12:29:55
TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
TIFFTAG_SOFTWARE=Adobe Photoshop 21.0 (Windows)
TIFFTAG_XRESOLUTION=300
TIFFTAG_YRESOLUTION=300
Image Structure Metadata:
COMPRESSION=LZW
INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 2401.0)
Upper Right ( 1640.0, 0.0)
Lower Right ( 1640.0, 2401.0)
Center ( 820.0, 1200.5)
Band 1 Block=1640x39 Type=Byte, ColorInterp=Red
Mask Flags: PER_DATASET ALPHA
Band 2 Block=1640x39 Type=Byte, ColorInterp=Green
Mask Flags: PER_DATASET ALPHA
Band 3 Block=1640x39 Type=Byte, ColorInterp=Blue
Mask Flags: PER_DATASET ALPHA
Band 4 Block=1640x39 Type=Byte, ColorInterp=Alpha
Also of the following images that have black background after translate, gdal produces this warning "Warning 1: TIFFFetchNormalTag: Incompatible type for "RichTIFFIPTC"; tag ignored"
The file that you have shared on filebin contains an "alpha" mask, as you can see in the output of gdalinfo. The mask of this file says that the background is masked, while the rest of the image is not.
If you display the tiff with the default Ubuntu viewer, for example, you can see that the background pixels are masked out (they appear as a checker board)
If you inspect the raster data, you also see that the underlying pixels from the background are black, not white. That is why gdal_translate generates a jpeg with black pixels in the background, it is because they really are black (but masked out) in the original tiff file.
If you absolutely want the background to be white, you can do so with a few lines of Python using the rasterio library for example, by explicitly setting the masked pixels to white:
import rasterio
with rasterio.open("101679_1.tif") as src:
arr = src.read(masked=True)
# Convert all masked values to white
arr[arr.mask] = 255
# Write to jpeg file
profile = src.profile
profile["count"] = 3
profile["driver"] = "jpeg"
with rasterio.open("test.jpeg", "w", **profile) as dst:
dst.write(arr[:3])
This should give the following jpeg file:
The code snippet I have included above will also work on TIF files which already have a white background, because the arr[arr.mask] = 255 line will not do anything if the file contains no mask.
To process a directory full of .tif files, you can do:
from glob import glob
import rasterio
for tif in glob("*.tif"):
with rasterio.open(tif) as src:
arr = src.read(masked=True)
# Convert all masked values to white
arr[arr.mask] = 255
# Write to jpeg file
profile = src.profile
profile["count"] = 3
profile["driver"] = "jpeg"
with rasterio.open(tif.replace(".tif", ".jpeg"), "w", **profile) as dst:
dst.write(arr[:3])

Mathematica : Add text to imported image / graphic as a label

Ive searched for hours ... but Im at a loss !
I have imported an image int Mathematica -> dimensions 2x2cm at 72DPI.
I am trying to "label" the image with a text string that:
- has font color "fontColor"
- has a black outline, so it contrasts to any underlying color
- sits in the bottom right corner of the imported image
- has size h/w in cm
- optionally sits in a text box with a white background
This is how far ive come:
MathCode:
image = Import["myimg.jpg"];
inchFactor = 2.54;(* 1 inch = 2.54cm *)
docRes = 72;
pixelConverter = docRes/inchFactor/2;
myText = First[
First[ImportString[
ExportString[
Style["glorious label string here", Bold, FontSize -> 15,
FontFamily -> "Verdana"], "PDF"], "PDF",
"TextMode" -> "Outlines"]]];
myTextGraphic =
Graphics[{EdgeForm[Directive[Black, Thickness[0.01]]], White,
myText}, Background -> White,
ImageSize -> {10*pixelConverter, 2*pixelConverter}];
myTextGraphic = Rasterize[myTextGraphic];
combined = SetAlphaChannel[myTextGraphic, myTextGraphicAlphaVersion];
I found the above method (PDF wrapper) for the black outline of the text string.
I am adding an AlphaChannel to the graphic of the text string using a version of it that only uses black/white.
I then try to combine the images with Overlay.
As none of this seems to work concerning the outputted image size and positioning, Im kindly asking for help.
There´s no need to "fix" that messy code.
Maybe you could point me to a script or tutorial - all I really want is to add and position a text string or text box to an underlying image.
Thanks a lot !
Have a look at this. There are other ways too.
img = Import["http://todayinsci.com/H/Hilbert_David/HilbertDavidThm.jpg"];
Column[{
img,
Text[Style["Professor Hilbert", Red]]
}]
imgCtr = Round[ImageDimensions[img]/2];
overlay = Framed[Graphics[{Text[Style["Professor Hilbert", Red, 9], imgCtr]},ImageSize-> {66, 14}], FrameStyle -> Green]
Overlay[{img, overlay}, Alignment -> Center]
It's late here so this is only the beginning of a solution for you but here's a simple way to add a text label to an image:
lbl = Graphics[Text[Style["Bottom", Red, Large]]]
which creates an image with the text 'Bottom' in red in a large font. Next, given an image called img1
ImageCompose[img1,lbl]
puts the text in the centre of the image. ImageCompose has options to allow you to position the second image (ie the label) wrt the first image. You can put the label on a coloured background like this:
lbl = Graphics[Text[Style["Bottom", Red, Large, Background -> Blue]]]
I haven't figured out, yet, how to write the text with a coloured outline.

Resources