Page 1 of 1

How to create a small, 1bpp BMP text image?

Posted: 2010-12-21T08:01:07-07:00
by swansail
I am trying to create a small, black and white BMP text image, but I am always ending up with a 24 bit per pixel image output. What am I missing?

All of these produce a 24698 byte 24-bit image:

convert -size 128x48 -monochrome -gravity center label:"This\nIs A\nTest" text.bmp
convert -size 128x48 -monochrome -background white -fill black -gravity center label:"This\nIs A\nTest" text.bmp
convert -size 128x48 -depth 1 -gravity center label:"This\nIs A\nTest" text.bmp
convert -size 128x48 -colors 2 -gravity center label:"This\nIs A\nTest" text.bmp

Re: How to create a small, 1bpp BMP text image?

Posted: 2010-12-21T10:23:32-07:00
by fmw42
this works for me
put monochrome and other like things after your create the label and just before the output image

convert -size 128x48 -background white -fill black -gravity center label:"This\nIs A\nTest" -monochrome text.bmp
identify text.bmp
...

Type: Bilevel
Base type: Bilevel
Endianess: Undefined
Colorspace: RGB
Depth: 1-bit
Channel depth:
gray: 1-bit
Channel statistics:
Gray:
min: 0 (0)
max: 1 (1)
mean: 0.950195 (0.950195)
standard deviation: 0.217541 (0.217541)
kurtosis: 15.1308
skewness: -4.13894
Histogram:
306: ( 0, 0, 0) #000000 black
5838: (255,255,255) #FFFFFF white
Colormap: 2
0: (255,255,255) #FFFFFF white
1: ( 0, 0, 0) #000000 black


IM 6.6.6.6 Q16 (hdri) Mac OSX tiger

Re: How to create a small, 1bpp BMP text image?

Posted: 2010-12-21T11:20:47-07:00
by swansail
Interesting - I ran your command line (on CentOS 5.5) and then identify, and I get the output below. It is creating a file with gray in it?

I would expect a file of size 128x48=6144 bytes, but it still produces a 24698 byte file. How big is your file?

identify -verbose text.bmp
Image: text.bmp
Format: BMP (Microsoft Windows bitmap image)
Class: DirectClass
Geometry: 128x48
Type: Bilevel
Endianess: Undefined
Colorspace: Gray
Channel depth:
Gray: 1-bits
Alpha: 1-bits
Channel statistics:
Gray:
Min: 0 (0)
Max: 1 (1)
Mean: 0.96696 (0.96696)
Standard deviation: 0.178742 (0.178742)
Alpha:
Min: 1 (1)
Max: 1 (1)
Mean: 1 (1)
Standard deviation: 0 (0)
Colors: 2
Histogram:
203: ( 0, 0, 0, 0) black
5941: (255,255,255, 0) white
Rendering intent: Undefined
Resolution: 28.34x28.34
Units: PixelsPerCentimeter
Filesize: 24kb
Interlace: None
Background color: white
Border color: #DFDFDF00
Matte color: grey74
Page geometry: 128x48+0+0
Dispose: Undefined
Iterations: 0
Compression: Undefined
Orientation: Undefined
Signature: b039011c055ca40970fb9bf9fca9fc3f494aa4b544963df40e4e4e4024e86e34
Tainted: False
Version: ImageMagick 6.2.8 10/20/10 Q16 file:/usr/share/ImageMagick-6.2.8/doc/index.html

Re: How to create a small, 1bpp BMP text image?

Posted: 2010-12-21T15:57:31-07:00
by fmw42
You are using a very ancient version of IM. Version 6.2.8 is over 350 versions old. Lots of things have been fixed or improved. Upgrade!

Re: How to create a small, 1bpp BMP text image?

Posted: 2010-12-21T17:41:41-07:00
by swansail
Had a devil of a time installing the latest ImageMagick 6.6.6.6 rpm on Centos 5.5, its not very yummy...

In case others run into the same issue...
Removed the old with rpm -e ImageMagick
Downloaded the new rpm from here, installed with rpm -ivh --nodeps
Tried to run convert a couple of times, found I needed to locate and install the RPMs for libfftw, liblzma, libgomp
Now it runs, and what do you know, it works! 128x48x1bpp image

Thanks!

Re: How to create a small, 1bpp BMP text image?

Posted: 2011-01-06T07:27:05-07:00
by swansail
So, now that I can create a 1bpp BMP image with convert, I am trying to create the same image via the PHP Imagick extension ( http://us2.php.net/manual/en/book.imagick.php ). The code below creates the image, but even though I have set the color space and depth, I am still getting a 24bpp output image, and I have not been able to find an equivalent to -monochrome in PHP. Can anyone point me in the right direction?

$wtext = "This is a test";

/* Create Imagick objects */
$image = new Imagick();
$draw = new ImagickDraw();
$color = new ImagickPixel('black');

/* Font properties */
$draw->setFontSize(12);
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);

/* Get font metrics */
$metrics = $image->queryFontMetrics($draw, $wtext);

/* Create text */
$draw->annotation(0, $metrics['ascender'], $wtext);

/* Create image */
$image->newImage(128, 48, 'white');
$image->setImageColorSpace(2);
$image->setImageDepth(1);
$image->setImageFormat('bmp');
$image->drawImage($draw);

/* Output the image */
header("Content-Type: image/bmp");
echo $image;

Re: How to create a small, 1bpp BMP text image?

Posted: 2011-01-06T11:39:49-07:00
by fmw42
It is much easier and won't be much slower to just use your command line in PHP with exec.

Re: How to create a small, 1bpp BMP text image?

Posted: 2011-01-06T14:27:52-07:00
by swansail
I was using exec, but I am trying for a bit more speed and flexibility.

Is the monochrome option not available via the extension? I have everything else working - there must be some way to get it down to 1-bit...

Re: How to create a small, 1bpp BMP text image?

Posted: 2011-01-06T18:59:00-07:00
by fmw42
you probably won't gain much speed. see viewtopic.php?f=4&t=16779

for Imagick commands, see http://us3.php.net/manual/en/book.imagick.php , but I don't see any equivalent of -monotone there. You could try the equivalent of -threshold and/or -colors 2 and set the type to bilevel.

Re: How to create a small, 1bpp BMP text image?

Posted: 2011-01-07T14:47:54-07:00
by swansail
Still no luck. Tried the following, but the output is still 24 bits/pixel ;-(

$image->setImageDepth(1);
$image->setImageColorSpace(imagick::COLORSPACE_GRAY);
$image->setImageType(imagick::IMGTYPE_BILEVEL);

Re: How to create a small, 1bpp BMP text image?

Posted: 2011-01-07T14:54:21-07:00
by fmw42
swansail wrote:Still no luck. Tried the following, but the output is still 24 bits/pixel ;-(

$image->setImageDepth(1);
$image->setImageColorSpace(imagick::COLORSPACE_GRAY);
$image->setImageType(imagick::IMGTYPE_BILEVEL);
You did not use -threshold or -colors 2 in the above after -colorspace gray. Try adding the equivalents. Also set the output image type to BMP.


PS Does it work correctly in command line mode or PHP exec?

What version of IM and what version of Imagick are you using? Perhaps you need to upgrade.

Also perhaps this topic should be cross posted to the Imagick forum.

Re: How to create a small, 1bpp BMP text image?

Posted: 2011-01-07T16:23:47-07:00
by swansail
Ah, did not realize there is a separate IMagick forum. I will move the PHP part of this discussion there. Thanks for your help!

To answer your questions, I only posted a portion of the code, I am setting the output to bmp.

I was not able to find equivalents to the other commands you mentioned, but perhaps the other forum will yield the answer.

Re: How to create a small, 1bpp BMP text image?

Posted: 2011-01-07T18:26:00-07:00
by fmw42
see thresholdImage and perhaps quantizeImage at http://us3.php.net/manual/en/book.imagick.php

the Imagick forum is at viewforum.php?f=18