Drawing text on a gif while preserving transparency [FFMPEG] - image-processing

Using the regular method, input > drawtext seems to remove transparency for GIFs and messes with the coloring
ffmpeg -i test.gif drawtext=text='Test':fontcolor=red:fontsize=24 output.gif
I've always tried using filter complex's palettegen and paletteuse, but those require arguments that drawtext doesn't provide:
ffmpeg -i banner.gif -filter_complex "[0:v] drawtext=text='Test':fontcolor=red:fontsize=24 [a]; [a] palettegen=reserve_transparent=1 [b]; paletteuse [a][b]" output.gif
What's the best way to efficiently draw text while preserving transparency?

Related

Convert png to webp does not preserve transparency

Using the Google cwebp utility (https://developers.google.com/speed/webp/docs/cwebp) and the ImageMagick convert utility, I am unable to preserve transparency in png images when converted to webp.
Using convert I've tried using alpha-compression=0 and alpha-quality=100, without positive result. See: https://imagemagick.org/script/webp.php
The images I am working with have transparent corners for a rounded icon image.
Script being used (in Fish shell):
for file in *.png;
cwebp $file -o (basename $file .png).webp;
end;
and
for file in *.png;
convert $file (basename $file .png).webp;
end;
Never mind. It was my image editor which was not displaying the transparency properly. Transparency was still there. Now filing a bug report at https://github.com/linuxmint/pix/issues

Convert a bunch of png or webp images to a webp animation

How to convert a bunch of png or webp images into webp animation ?
I tried this:
convert mytiles.png -crop 100x100 +repage tmp.webp
But I just get a bunch of webp images instead of an animation.
Another solution would be to use gif2webp but the homebrew webp package doesn't contain this command unlike what's written in the official documentation.
Convert frames to WebP, then use webpmux
The webpmux program comes with Google’s reference library for WebP, namely libwebp, which you can install through your package manager if not present yet. Its man page reads:
webpmux — create animated WebP files from non-animated WebP images, extract frames from animated WebP images, and manage XMP/EXIF metadata and ICC profile.
If your individual frames are in another format, you’ll first need to convert them to WebP. For this task you can use ImageMagick (more options here—be sure to disable lossy compression unless you want it):
convert frame001.png -define webp:lossless=true frame001.webp
Then you can combine frames using webpmux. The syntax is the following, where the capitalized words are placeholders:
webpmux \
-frame frame001.webp +D1+X1+Y1+M1±b \
-frame frame002.webp +D2+X2+Y2+M2±b \
-frame frame003.webp +D3+X3+Y3+M3±b \
[-loop L] [-bgcolor A,R,G,B] \
-o animation.webp
Each frame expects a number of settings. All of them but the duration can be left implicit.
Di is the duration of the frame, in milliseconds.
(Xi, Yi) is the spatial offset of the frame in the canvas (counting from the top-left corner, X going right and Y going down).
Mi is the dispose method for this frame, in other words what to do once this frame has expired. It has two possible values:
0: do not dispose, leave the canvas as-is;
1: dispose, i.e. clear the canvas and fill it with background (consumes energy, for that matter).
±b is the blending method for this frame, which specifies how to superimpose this frame on top of the existing canvas (it is only relevant when the frame has an alpha channel, otherwise it overwrites what is below it anyway). This parameter has two possible values:
-b: do not blend, the frame overwrites the existing canvas;
+b: use alpha blending, the frame combines with the existing canvas.
Then come the optional settings that apply to the animation as a whole:
-loop L specifies the number L of times to play the animation. You’ll probably want it to be infinite, which is meant by -loop 0, which is the default value.
-bgcolor A,R,G,B specifies the background color. The values of the four components (alpha, red, green, blue) range from 0 to 255. Viewers may use this color as a background for the canvas, so that the canvas is opaque, but they are not required to; in practice the canvas is overlayed on top of other elements, as in a web page for example, and I have not seen this color used.
See also the WebP specification about animations.
Final tip: since the command line options for each frame are more than just the input file name, you cannot use a wildcard like frame*.webp directly and writing the command line proves cumbersome. Thankfully, you can use your shell to build the command line, e.g with Bash:
frames=( )
for f in frame*.webp ; do
frames+=( -frame "$f" +100+0+0+0+b )
done
webpmux "${frames[#]}" -o animation.webp
The example above creates an animation whose frames are all images matching frame*.webp, have a duration of 100 ms, have no spatial offset, and combine with the previous frames (use alpha blending for new frames and do not dispose of them afterwards).

Edit image files with Gimp script-fu

I'm trying to edit a pdf file with 100 pages, all of them images I need to export as png, setting their image mode as greyscale, and setting also their resolution, width and heigth.
How can I write a scheme (or python) script that perform this actions so that i could apply them by gimp in batch mode?
I've searched in the internet but didn't find simpy stated instructions.
ImageMagick's convert will do all this in one call in a command prompt:
convert -density 200 -colorspace Gray input.pdf -geometry 1000 ouput.png
will produce 1000px-wide grayscale PNGs (output-0 to output-(N-1).png) using a 200DPI rendering of the PDF.
You can also use Gimp scripting but you'll have a lot more to learn and AFAIK the API for the PDF loader only loads at 100DPI.
A slightly more manual method could be to:
Load (manually) the image in Gimp (you can specifiy the DPI in that case). This loads all the pages as layers.
Image>Mode>RGB to convert the image to grayscale.
Image>Scale image to set the size of all the pages
Save the individual layers to PNG (there are scripts for this, for instance this one)

ImageMagick Command Line convert PNG to EPS with Transparency

I am trying to do a simple conversion from a transparent PNG file to EPS with transparency and currently my command looks like this:
convert "image1.png" "image1.eps"
It looks like all I am getting is a black image. Any ideas?
Thanks!
Converting a PNG to an EPS is more than just a simple format conversion. Its changing from a raster image to a vector image, so the raster image has to be "traced". The popular command line tool for doing this is potrace. With potrace installed (and its component tool mkbitmap) you could do it with something like this:
convert image1.png image1.bmp
mkbitmap image1.bmp -o image1.pgm
potrace image1.pgm -e -o image1.eps
The call to mkbitmap converts the color image to a graymap more suitable for tracing.
This will yield an eps with black lines on a white background. If you need a full color trace, inkskape is a GUI tool for doing this, and an inkscape user homebrewed a command line tool to do it, which can be found here

ImageMagick: Transparent Backgrounds for JPEG, not PNG

I'm using ImageMagick to convert PDF files to PNGs. (Lots of text, so I'd rather use PNG over JPEG.) I'm doing this on OS X, 10.8.2.
I tried using Ghostscript, then ImageMagick, as was done here and I got a gray background. I shortened it to one line, since ImageMagick can work with PDFs and tried this:
convert -background transparent -transparent white \
Background-Page01.pdf TestClearX1.png
I've tried that with PNG files and JPEG files. (And with and without -transparent white as well.)
If it's a JPEG, I can get a white background (which is probably clear, but in my viewer, I can't tell), but with a PNG, I always get a dark background. I thought about, as a test, trying to generate a BMP, then converting that to PNG, but it won't generate BMP files.
How can I get a transparent background for a PNG file?
And if that's not possible, since JPEG files are not good for text, is there a better alternative?
This isn't an answer, but the format of the comment section doesn't allow sensible formatting, so I am offering it here instead.
You alluded to the fact that Preview in OS X doesn't show transparency properly, and that is correct. To get around that, I made the following script, which overlays the files you want to view onto a checkerboard pattern like Photoshop does. I save it as preview and then use chmod +x preview on it to make it executable. Here is the script:
#!/bin/bash
################################################################################
# preview
# Preview images using OSX "open" but overlay images on top of checkerboard
# first so you can see transparency/transparent pixels, since Preview renders
# them grey :-(
################################################################################
for f in "$#"
do
base=$(basename "$f")
composite -compose Dst_Over -tile pattern:checkerboard "$f" "/tmp/$base"
open -g "/tmp/$base"
done
That enables you to do:
/.preview file1.png file2.gif
and the -g option also means Preview doesn't steal focus too :-)
So, instead of looking like this:
it looks like this:
There's two ways to export a PDF in LibreOffice, and one way does not provide any warnings. The other way provides a warning that PDF/A cannot have transparent objects. The problem is you're using PDF/A files that don't support transparent objects—this results in your PNG always having a background colour.

Resources