convert to dithered 3 bit (8 color) bitmap?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
vesel
Posts: 9
Joined: 2015-05-17T08:07:12-07:00
Authentication code: 6789

convert to dithered 3 bit (8 color) bitmap?

Post by vesel »

I have video frames I need to convert to dithered 3 bit (8 color: red, green, blue, cyan, magenta, yellow, white, black) RGB images.

Image

I want to convert it to this:
Image
Worked in GIMP. But I need to batch convert thousands of these.

By using these commands in ImageMagick I get this:

Code: Select all

convert c:\Users\User\Desktop\1.png -dither Floyd-Steinberg -colors 8 c:\Users\User\Desktop\1.bmp
Image

Seems like I need to create my own color palette like in GIMP (unless there is one for 3 bit rgb built in), otherwise the program generates optimal color palette from the image itself. How can I do that?

And what command can I use to convert a whole folder of images, not just one?

Finally, this is a different question, but can I save each channel as separate monochrome (1 bit) bmp files as well?
Not easy finding the commands I need.
Thank you.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert to dithered 3 bit (8 color) bitmap?

Post by snibgo »

Code: Select all

convert xc:red xc:lime xc:blue xc:cyan xc:magenta xc:yellow xc:white xc:black -append mymap.png

convert in.png -remap mymap.png out.png
Repeat the second command for each different input file.

To convert all the files in a folder, I would use a loop in a shell script. Perhaps "mogrify" would do the job.

"-separate" will take one input image and make three images, for the R, G and B channels. There are many different ways of making an image monochrome.
snibgo's IM pages: im.snibgo.com
vesel
Posts: 9
Joined: 2015-05-17T08:07:12-07:00
Authentication code: 6789

Re: convert to dithered 3 bit (8 color) bitmap?

Post by vesel »

Thank you.

What about the ImageMagick API? I tried writing a Python program to automate conversion of files in folders, but I can't find where the '-remap' functionality of the convert.exe program is in the API.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: convert to dithered 3 bit (8 color) bitmap?

Post by dlemstra »

Which Python API are you using? And just to be clear, we are not the authors of those API's.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
vesel
Posts: 9
Joined: 2015-05-17T08:07:12-07:00
Authentication code: 6789

Re: convert to dithered 3 bit (8 color) bitmap?

Post by vesel »

I'm currently using http://docs.wand-py.org/en/0.4.0/ but any will do.
You can just tell me where the functionality in the C library is located.

And how to do ordered dithering and still use custom 8 color pallete?
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: convert to dithered 3 bit (8 color) bitmap?

Post by dlemstra »

The method in the C API is RemapImage. The first parameter is 'QuantizeInfo' that can be used to set the dither settings.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
vesel
Posts: 9
Joined: 2015-05-17T08:07:12-07:00
Authentication code: 6789

Re: convert to dithered 3 bit (8 color) bitmap?

Post by vesel »

I checked the Wand sourcecode and it seems there's no wrapper around that function.

I also checked "PythonMagick" library which seems to be a wrapper around the C++ "Magick++" library. I found the function Image.map there which is also made available in PythonMagick, but it only seems to allow Floyd-Steinberg dithering, unless there's another function I missed this is also not a usable library for this job.
http://www.imagemagick.org/Magick++/Image.html
Remap image colors with closest color from reference image. Set dither_ to true in to apply Floyd/Steinberg error diffusion to the image. By default, color reduction chooses an optimal set of colors that best represent the original image. Alternatively, you can choose a particular set of colors from an image file with this option.
EDIT: Also seems to be missing with the 3rd and final wrapper, "MagickWand".
vesel
Posts: 9
Joined: 2015-05-17T08:07:12-07:00
Authentication code: 6789

Re: convert to dithered 3 bit (8 color) bitmap?

Post by vesel »

For now I've just written a Python script that runs a windows shell command inside a loop, not a good way to do it but will do for now.

How to do ordered dithering and still use custom 8 color pallete?

And how can I set the locations and names of the extracted channel files to save? "-separate" saves all 3 channels in one folders and names them itself.
I could loop a second time in the folder for renaming and moving the channel files, but since processing 10,000+ files already take a long time it would be best to keep it in a single command and loop.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: convert to dithered 3 bit (8 color) bitmap?

Post by glennrp »

vesel wrote: How to do ordered dithering and still use custom 8 color pallete?

Code: Select all

convert in.png -gamma 0.45455 -ordered-dither 2x2 png8:out.png
Ordered dithering automatically uses the 8-color saturated colors palette.
Without the initial gamma conversion, the output image would be too light.
Try "3x3", "4x4", etc (chosen from "convert -list threshold") to taste.
vesel
Posts: 9
Joined: 2015-05-17T08:07:12-07:00
Authentication code: 6789

Re: convert to dithered 3 bit (8 color) bitmap?

Post by vesel »

Thank you. What colors are in the "saturated colors" palette? Red, green, blue, cyan, magenta, yellow, white, black?

I still can't find a way to set the save locations of extracted color channels.

Also, I'm unable to save back any image I've converted to a BMP which uses palettes.
When converting

Code: Select all

convert something.bmp something.png
I get this error:

Code: Select all

convert.exe: no images defined `steinberg)\converted\0032.png' @ error/convert.c
/ConvertImageCommand/3212.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: convert to dithered 3 bit (8 color) bitmap?

Post by glennrp »

vesel wrote:Thank you. What colors are in the "saturated colors" palette? Red, green, blue, cyan, magenta, yellow, white, black?
Yes. They are the various combinations of 255 and 0, for the three color channels.
I still can't find a way to set the save locations of extracted color channels.
Not sure why you would need to do that.
Also, I'm unable to save back any image I've converted to a BMP which uses palettes.
This worked for me, on a Linux (Ubuntu) platform:

Code: Select all

convert in.png -gamma 0.45455 -ordered-dither 2x2 bmp2:out.bmp
The resulting .bmp appears to be a paletted BMP.
The "bmp2:" prefix can be omitted in which case the BMP header is a few bytes larger but the image is the same.
When converting

Code: Select all

convert something.bmp something.png
I get this error:

Code: Select all

convert.exe: no images defined `steinberg)\converted\0032.png' @ error/convert.c
/ConvertImageCommand/3212.
It seems likely that the command you used was not "convert something.bmp something.png" unless the
second "something" was more complicated. Maybe some problem with your Windows shell syntax; I can't help you with that.
vesel
Posts: 9
Joined: 2015-05-17T08:07:12-07:00
Authentication code: 6789

Re: convert to dithered 3 bit (8 color) bitmap?

Post by vesel »

I still can't find a way to set the save locations of extracted color channels.
Not sure why you would need to do that.
I mentioned this,
And how can I set the locations and names of the extracted channel files to save? "-separate" saves all 3 channels in one folders and names them itself.
I could loop a second time in the folder for renaming and moving the channel files, but since processing 10,000+ files already take a long time it would be best to keep it in a single command and loop.
Also, I'm unable to save back any image I've converted to a BMP which uses palettes.
This worked for me, on a Linux (Ubuntu) platform:

Code: Select all

convert in.png -gamma 0.45455 -ordered-dither 2x2 bmp2:out.bmp
The resulting .bmp appears to be a paletted BMP.
No, I mean trying to convert the paletted BMP I have created with ImageMagick later to a PNG, or any other format. Seems like ImageMagick doesnt take a BMP which uses a palette as input file for me...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert to dithered 3 bit (8 color) bitmap?

Post by snibgo »

"-separate" doesn't save anything. All it does is replace each image in the list with (n) images, where n is typically 3.

If you save the result in the ordinary way with a filenme like mydir\x.png, you will get three files: mydir\x-0.png, mydir\x-1.png and mydir\x-2.png.

If you want to save them with different filenames, you can use "clone" and "write", eg:

Code: Select all

convert rose: -separate ( -clone 0 +write rose-red.png +delete ) ( -clone 1 +write rose-green.png +delete ) ( -clone 2 +write rose-blue.png +delete ) +write info: NULL:
snibgo's IM pages: im.snibgo.com
vesel
Posts: 9
Joined: 2015-05-17T08:07:12-07:00
Authentication code: 6789

Re: convert to dithered 3 bit (8 color) bitmap?

Post by vesel »

Thank you.

Now about loading the dithered palettized images back to ImageMagick to convert it to an image not using a palette, are you guys getting the same error as me? or can you successfully convert these images?

The reason I want to do this is I want to use Blender to save the frames to a video file and upload it to youtube to show few people how the dithered 8 color videos will look, but Blender does not support paletted BMPs, so I need to convert them back to a RGB BMP to create a video file. There are over 15000 BMPs, several gigabytes, that's why I want to use Blender to do the job.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: convert to dithered 3 bit (8 color) bitmap?

Post by glennrp »

I looked at blender.org and don't see the limitation you mentioned.
It claims to be able to read PNG files, among others, so why not simply do

Code: Select all

convert in.png -gamma 0.45455 -ordered-dither 2x2 out.png
and read your set of PNG files into blender? Use "mogrify" instead of "convert"
to convert batches of images at once.
Last edited by glennrp on 2015-05-26T06:10:50-07:00, edited 1 time in total.
Post Reply