Log in

View Full Version : Which filter / plugin to use to achieve 2 colour black and white output?


konstantin1
15th January 2016, 17:28
I would like to convert a video for 2 colour (black and white) with dithering (if possible). Something like this:
http://www.imagemagick.org/Usage/quantize/monochrome.gif

I tried avisynth output to connect to ffmpeg and then to ImageMagick, do the colour quantization to 2 colours, and then connecting the output again to ffmpeg, and encoding it into a movie or animated gif, but I had no success. My command was:

palette=/tmp/palette.png
wine avs2yuv.exe "$video" -|ffmpeg -f yuv4mpegpipe -i - -f image2pipe -vcodec ppm - | pnmnoraw | process_ppm_pipeline +dither -colors 2 -colorspace gray -normalize | ffmpeg -f image2pipe -vcodec ppm -i - -vf "palettegen=max_colors=128:stats_mode=full" -vsync 0 -y "$palette"
wine avs2yuv.exe "$video" -|ffmpeg -f yuv4mpegpipe -i - -f image2pipe -vcodec ppm - | pnmnoraw | process_ppm_pipeline +dither -colors 2 -colorspace gray -normalize | ffmpeg -f image2pipe -vcodec ppm -i - -i "$palette" -lavfi "paletteuse=dither=bayer:bayer_scale=2" -vsync 0 -pix_fmt pal8 -gifflags +transdiff -y -f gif "$out"

I would like to omit ImageMagick because image pipeline processing doesn1t work as it sould be and do the colour quantization at avisynth level.

StainlessS
15th January 2016, 17:48
I dont know much about this, but would think best bet are old palette based pic/video based apps.
Something like 'Gif Construction Set' , 'Graphics WorkShop 95', or 'IrfanView'.

EDIT:
Gif Construction Set(seems to be what I'm talking about):- http://gif-construction-set-pro.en.softonic.com/
Graphics WorkShop 95:- http://www.danielsays.com/ss-gallery-win9xme-sw-graphic-workshop95.html
IrfanView:- http://www.irfanview.com/

Have not used any of above for a decade or so.

You would 1st use Avisynth 'GrayScale()" to get 256 grays and from there need to convert to Palette based
if IrfanView etc do not import 24Bit. IrfanView at least seems to be currently still in development,
I expect that it still supports old palette based formats.

wonkey_monkey
15th January 2016, 18:17
You could use my rgba_rpn/y8_rpn (http://forum.doom9.org/showthread.php?t=172601) plugin to do this, but unfortunately right now my Windows laptop is dead so I can't try anything out. It can do either ordered or random dithering.

Things are complicated quite a bit by yuv's 16-235 levels, so I'll just give an RGB example for now:


input=avisource("...")
grey=input.converttorgb24.greyscale

rgba_rpn(grey, "X", "X", "[g0] 0.003921569 * @X", 1) # change the 1 to a 2 for random, instead of ordered, dithering
levels(0,1,1,0,255)


As I say, I haven't tested this, and in the time it's taken me to write it someone has probably given you a much better solution.

StainlessS
15th January 2016, 20:02
Here, requires CallCmd and TWriteAVI

http://forum.doom9.org/showthread.php?t=166063&highlight=CallCmd
http://forum.doom9.org/showthread.php?t=172837&highlight=TWriteAVI


# Req CallCmd(), TWriteAVI :

Avisource("D:\V\StarWars.avi") # Pre-RoboCropped
Trim(47971,53073)
Trim(0,-100)
GrayScale
Spline36Resize(1016,432) # 2.35:1
ConvertToRGB24

CONV = "C:\Program Files\ImageMagick-6.8.8-Q8\Convert.exe "
INDIR = "D:\PALETTE\IN\"
OUTDIR = "D:\PALETTE\OUT\"
EXT = "png"
IM = " -monochrome " # ImageMagick Command
#IM = " -ordered-dither h4x4a " # ImageMagick Command
CMD = INDIR + "#." + EXT + IM + OUTDIR + "#." + EXT
COMMAND = CONV + CMD
END = FrameCount-1
FPS = FrameRate
HIDE = True
Aud=Last
ImageWriter(file=INDIR+"%06d."+EXT,Start=0,End=END,type=EXT)
CallCmd(Command=COMMAND,Frames="0,0",Insert="#",Hide=HIDE,Debug=True)
ForceProcessAVI # Force above to be written without playing file
ImageSource(OUTDIR+"%06d."+EXT,Start=0,End=END,fps=FPS,Pixel_type="RGB24")
#ConvertToYV12 ### Or whatever you want
Return AudioDubEx(Last,Aud)

EDIT: Above Directories MUST exist.

ForceProcessAVI (from TwriteAVI) force writes the images and does conversion so that ImageSource constructor will not complain about missing png files.
(Imagesource constructor only called after output png's exist).
If splitting into two scripts, then in 2nd script load original AVI so as to obtain END, FPS and Audio.
EDIT: You can of course change ImageMagick command to whatever works best for you (I dont know IM too well).

Reel.Deel
15th January 2016, 20:31
Here's the result with davidhorman's script:

ImageSource("Imagemagick-logo.png", pixel_type="RGB24")
Grayscale()
rgba_rpn(last, "X", "X", "[g0] 0.003921569 * @X", 1) # change the 1 to a 2 for random, instead of ordered, dithering
levels(0,1,1,0,255)

https://upload.wikimedia.org/wikipedia/commons/0/0d/Imagemagick-logo.png http://s8.postimg.org/vo10xjqmr/IM_logo_avs.png

Here's a bit more information from IM: http://www.imagemagick.org/Usage/quantize/#monochrome

StainlessS
15th January 2016, 20:49
Here with IM = " -ordered-dither h4x4a "


https://s20.postimg.cc/qjn4s7ipp/SW_zpse3o3p0tp.png (https://postimg.cc/image/alef22ohl/)

EDIT: That shiny fellow looks kinda like my brother.

StainlessS
15th January 2016, 21:32
Reel.Deel, this is what I got from David's script [I added Spline36Resize(640,480) after Grayscale, too big]
I made sure to download current dll, did not know which version I had.

EDIT: Actually, I used your version of David's script.

https://s20.postimg.cc/flbva0u4d/IM-_Logo0_zps6k7zjnsb.png (https://postimg.cc/image/f8kh3ubuh/)

EDIT: My script, IM = " -monochrome "
https://s20.postimg.cc/kldwvpp4t/monochrome0_zpsb3de7dqu.png (https://postimg.cc/image/4adszecmx/)

EDIT: My script, IM = " -ordered-dither h4x4a "
https://s20.postimg.cc/y3kt801a5/ordered-dither_h4x4a0_zpsdwjvpn7m.png (https://postimg.cc/image/fo0caln5l/)

EDIT: Avisynth ImageSource screws up the loading, ie creates the BLACK surround not visible in Reel.Deel's color image,
must be some kind of Layer Info. ImageSource bug ? Perhaps ImageSource does not support multiple png layers.

wonkey_monkey
16th January 2016, 01:56
Here's the result with davidhorman's script:


I think you forgot the actual result...

Sparktank
16th January 2016, 02:36
http://s8.postimg.org/vo10xjqmr/IM_logo_avs.png

Some reason or other, postimage wouldn't some of us see the image.

Re-uploaded result to Imgur.
(linked image, click to view on Imgur)
http://i.imgur.com/g1PdYSn.png (http://imgur.com/g1PdYSn)

Sparktank
16th January 2016, 02:37
http://i1136.photobucket.com/albums/n494/StainlessStephen/SW_zpse3o3p0tp.png

This reminds of old SNES games...
Hmmm... I wonder if...

StainlessS
16th January 2016, 03:41
Reel.Deel, where did you get you Image, seems to have worked ok with ImageSource, I got mine from ImageMagick-6.8.8-Q8\Images\logo.png
(at least assuming same as uploaded by Sparktank)

EDIT: The Horman/Sparktank result is not same as the one I posted (wizards robe dithering).

Reel.Deel
16th January 2016, 04:16
Avisynth ImageSource screws up the loading, ie creates the BLACK surround not visible in Reel.Deel's color image, must be some kind of Layer Info. ImageSource bug ? Perhaps ImageSource does not support multiple png layers.

ImageSource handles PNGs with an alpha channel just fine when handled correctly, pixel_type defaults to RGB24, so when loading an image with an alpha channel you must set pixel_type="RGB32". Here's a script that processes the alpha channel, it seems that's what ImageMagick is doing.

ImageSource("Imagemagick-logo.png", pixel_type="RGB32")
Trim(0,-1)
o = last

gray = o.Greyscale()
gray = rgba_rpn(gray, "X", "X", "[g0] 0.003921569 * @X", 1) # change the 1 to a 2 for random, instead of ordered, dithering
gray = gray.levels(0,1,1,0,255)

alpha = o.ShowAlpha()
alpha = rgba_rpn(alpha, "X", "X", "[g0] 0.003921569 * @X", 1)
alpha = alpha.levels(0,1,1,0,255).ShowRed("Y8")

MergeARGB(alpha, gray, gray, gray) #change alpha to o to simply copy the alpha channel w/o any processing
ImageWriter(file="IMlogo", type="png")

Left: alpha mask processed | Right: alpha mask copied
https://web.archive.org/web/20160116025048/http://s29.postimg.org/iqcx4cez9/IMlogo_1.png https://web.archive.org/web/20160116025421/http://s15.postimg.org/gtr7wilsp/IMlogo_2.png

Reel.Deel, where did you get you Image, seems to have worked ok with ImageSource, I got mine from ImageMagick-6.8.8-Q8\Images\logo.png

From Wikipedia (https://en.wikipedia.org/wiki/ImageMagick). That explains the dithered shadow in IM.


---

I think you forgot the actual result...

Sparktank reported that postimage.org is acting up with some users.

Quick question, how can I change the expression in rgba_rpn to also process the alpha channel or just a full range Y8 channel instead of the full RGB. I could use ConvertToY8(matrix"PC.601") as an alternative to Grayscale(). It would be nice if y8_rpn supported Y8, I though it did because of the name but it fooled me :).

Sparktank
16th January 2016, 06:32
This just might be the thing I'm looking for...

Original dvd remux:
deint, cropped, resized
http://i.imgur.com/EOD3xpx.png (http://imgur.com/EOD3xpx)

fun part:
deint, cropped, resized
4kresize, SNESPixelEffect, TurnsTile, Scanlines, 480resize
rgba_rpn # no image export
http://i.imgur.com/0QbpcPz.png (http://imgur.com/0QbpcPz)

Now to just chop up the framerate...

StainlessS
16th January 2016, 19:08
Here's the result with davidhorman's script:

ImageSource("Imagemagick-logo.png", pixel_type="RGB24")
Grayscale()
rgba_rpn(last, "X", "X", "[g0] 0.003921569 * @X", 1) # change the 1 to a 2 for random, instead of ordered, dithering
levels(0,1,1,0,255)

Thanks Reel.Deel, twas above script (+ a resize) that I used, and was not expecting to havta mess with alpha. :thanks: