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

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
swansail
Posts: 9
Joined: 2010-12-21T07:43:49-07:00
Authentication code: 8675308

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

Post 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;
Dulfer
Posts: 7
Joined: 2011-01-06T00:43:25-07:00
Authentication code: 8675308

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

Post 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
swansail
Posts: 9
Joined: 2010-12-21T07:43:49-07:00
Authentication code: 8675308

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

Post by swansail »

That works - excellent - thanks so much!
MarcusKirsch
Posts: 5
Joined: 2014-04-09T04:23:24-07:00
Authentication code: 6789

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

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

try using -depth 1
MarcusKirsch
Posts: 5
Joined: 2014-04-09T04:23:24-07:00
Authentication code: 6789

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

Post 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?
MarcusKirsch
Posts: 5
Joined: 2014-04-09T04:23:24-07:00
Authentication code: 6789

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

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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)
MarcusKirsch
Posts: 5
Joined: 2014-04-09T04:23:24-07:00
Authentication code: 6789

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

Post by MarcusKirsch »

imagemagick 6.8.5
MarcusKirsch
Posts: 5
Joined: 2014-04-09T04:23:24-07:00
Authentication code: 6789

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

Post by MarcusKirsch »

Are you using the PHP execute(commandline?) instead?
Did you try my PHP version?
Post Reply