In Python, how do I draw/ save a monochrome 2 bit BMP with Wand
Question:
I have a need to create a 2 bit monochrome Windows BMP format image, and need to draw lines in a pattern. Since I have just started using python, I followed a tutorial and installed the Wand module.
Drawing is fine, I get the content I need.
Problem is saving the image. No matter what I do, the resulting image is always 24 bpp BMP.
What I have so far:
from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color
draw=Drawing()
### SET VARS
mystepvalue = 15
with Image(filename='empty.bmp') as image:
print(image.size)
image.antialias = False
draw.stroke_width = 1
draw.stroke_color = Color('black')
draw.fill_color = Color('black')
#
# --- draw the lines ---
#
for xpos in range(0,4790,mystepvalue):
draw.line( (xpos,0), (xpos, image.height) )
draw(image) # Have to remember this to see anything...
# None of this makes a 2bit BMP
image.depth = 2
image.colorspace = "gray"
image.antialias = False
image.monochrome = True
image.imagedepth =2
image.save(filename='result.bmp')
Source image is 4800 x 283 px 169kb, result.bmp is same, but 4.075 kb.
It’s probably the order of the color settings that I miss.
Answers:
Try using Image.quantize()
to remove all the unique gray colors before setting the depth/colorspace properties.
# ...
image.quantize(2, colorspace_type='gray', dither=True)
image.depth = 2
image.colorspace = 'gray'
image.type = 'bilevel' # or grayscale would also work.
image.save(filename='result.bmp')
Also remember filename='bmp3:result.bmp'
or filename='bmp2:result.bmp'
controls which BMP version to encode with.
For me setting image mode to "1" did the job:
img.mode='1'
img.save('result.bmp')
Here are the docs on different modes: https://pillow.readthedocs.io/en/stable/handbook/concepts.html#concept-modes
I have a need to create a 2 bit monochrome Windows BMP format image, and need to draw lines in a pattern. Since I have just started using python, I followed a tutorial and installed the Wand module.
Drawing is fine, I get the content I need.
Problem is saving the image. No matter what I do, the resulting image is always 24 bpp BMP.
What I have so far:
from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color
draw=Drawing()
### SET VARS
mystepvalue = 15
with Image(filename='empty.bmp') as image:
print(image.size)
image.antialias = False
draw.stroke_width = 1
draw.stroke_color = Color('black')
draw.fill_color = Color('black')
#
# --- draw the lines ---
#
for xpos in range(0,4790,mystepvalue):
draw.line( (xpos,0), (xpos, image.height) )
draw(image) # Have to remember this to see anything...
# None of this makes a 2bit BMP
image.depth = 2
image.colorspace = "gray"
image.antialias = False
image.monochrome = True
image.imagedepth =2
image.save(filename='result.bmp')
Source image is 4800 x 283 px 169kb, result.bmp is same, but 4.075 kb.
It’s probably the order of the color settings that I miss.
Try using Image.quantize()
to remove all the unique gray colors before setting the depth/colorspace properties.
# ...
image.quantize(2, colorspace_type='gray', dither=True)
image.depth = 2
image.colorspace = 'gray'
image.type = 'bilevel' # or grayscale would also work.
image.save(filename='result.bmp')
Also remember filename='bmp3:result.bmp'
or filename='bmp2:result.bmp'
controls which BMP version to encode with.
For me setting image mode to "1" did the job:
img.mode='1'
img.save('result.bmp')
Here are the docs on different modes: https://pillow.readthedocs.io/en/stable/handbook/concepts.html#concept-modes