Page 1 of 1

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

Posted: 2011-01-08T12:17:49-07:00
by swansail
I am trying to create a small 1bit per pixel black and white BMP text image, but I am always ending up with a 24 bit per pixel image output.

I can generate the desired 1bpp image with convert using the monochrome option:
convert -font URWGothicBook -pointsize 10 -size 128x48 -monochrome label:"Test text" text.bmp

But I cannot find an equivalent to monochrome in IMagick? I have tried several other options, but I still get 24bpp output.

Here is what I have tried so far - any suggestions would be appreciated!
I know I could use exec with convert, but I would like to use IMagick if possible.

Code: Select all

$wtext = "This is test text";

/* 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->setImageDepth(1);
$image->setImageColorSpace(imagick::COLORSPACE_GRAY);
$image->setImageType(imagick::IMGTYPE_BILEVEL);
$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-10T10:52:22-07:00
by Dulfer

Code: Select all

$image = new Imagick();
$draw = new ImagickDraw();
$color = new ImagickPixel('black');

/* Font properties */
$draw->setFontSize(12);
$draw->setFillColor($color);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
$wtext = "This is test text";

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

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

/* Create image */
$image->newImage(128, 48, 'white');
$image->drawImage($draw);  
$image->setImageType(imagick::IMGTYPE_BILEVEL);   
$image->setImageFormat('bmp');

/* Output the image */
header("Content-Type: image/bmp");
echo $image;
			
?>
First draw, then we define

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

Posted: 2011-01-10T19:42:06-07:00
by swansail
That works - excellent - thanks so much!

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

Posted: 2014-04-09T04:29:34-07:00
by MarcusKirsch
Hello, I tried the above code and it creates a BW image, but not a truly 1bit BW image file, which is what I need.
The image info needs to say: Colorspace Gray , not RGB.

Anyone?

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

Posted: 2014-04-09T09:29:54-07:00
by fmw42
try using -depth 1

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

Posted: 2014-04-10T00:31:57-07:00
by MarcusKirsch

Code: Select all


$im = new Imagick();
$im->readimage($filename.$prefix);

$im->setImageFormat('bmp');
$im->setImageColorSpace(imagick::COLORSPACE_GRAY);
$im->setImageType(imagick::IMGTYPE_BILEVEL);
$im->setImageFormat('bmp');

$im->writeimage($filename."BW.bmp");

is there an issue if the base image is PNG?

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

Posted: 2014-04-10T06:08:13-07:00
by MarcusKirsch
Sorry, complete code:

Code: Select all


/* Create a new imagick object */
$filename = "test";
$prefix = ".png";

$im = new Imagick();
$im->readimage($filename.$prefix);
$im->setImageFormat('bmp');
$im->posterizeimage(2, false);
$im->setImageColorSpace(imagick::COLORSPACE_GRAY);
$im->setImageType(imagick::IMGTYPE_BILEVEL);
$im->setImageDepth(1);
$im->writeimage($filename."BW.bmp");

/* Output the image */
header("Content-Type: image/bmp");
echo $im;
Still does a RGB file. Is there any particular order in which the colorpsce, type and depth needs to be called?

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

Posted: 2014-04-10T09:35:47-07:00
by fmw42
What version of Imagemagick and Imagick are you using?

This works just fine with IM 6.8.8.10 Q16 Mac OSX

Code: Select all

convert rose: rose.png
convert rose.png -posterize 2 -colorspace gray -type bilevel -depth 1 tmp.bmp

Code: Select all

Image: tmp.bmp
  Format: BMP (Microsoft Windows bitmap image)
  Class: DirectClass
  Geometry: 70x46+0+0
  Units: PixelsPerCentimeter
  Type: Bilevel
  Base type: Bilevel
  Endianess: Undefined
  Colorspace: Gray
  Depth: 8/1-bit
  Channel depth:
    gray: 1-bit
  Channel statistics:
    Gray:
      min: 0 (0)
      max: 255 (1)
      mean: 54.4845 (0.213665)
      standard deviation: 104.523 (0.409893)
      kurtosis: -0.0480455
      skewness: 1.39712
  Colors: 2
  Histogram:
      2532: (  0,  0,  0) #000000 gray(0)
       688: (255,255,255) #FFFFFF gray(255)

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

Posted: 2014-04-13T02:33:49-07:00
by MarcusKirsch
imagemagick 6.8.5

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

Posted: 2014-04-13T02:35:07-07:00
by MarcusKirsch
Are you using the PHP execute(commandline?) instead?
Did you try my PHP version?