I want to use the Python API binding for ImageMagick http://wand-py.org to directly manipulate images. I am however not able to deduce from the documentation how to use a grayscale transformation. Can anybody provide information on this problem?
from wand.image import Image
try:
with Image(file=path) as img:
img.grayscale() # PSEUDOCODE for my given problem
except:
pass
Change a picture to grayscale or to black-and-whiteRight-click the picture that you want to change, and then click Format Picture on the shortcut menu. Click the Picture tab. Under Image control, in the Color list, click Grayscale or Black and White.
The Wand is an Imagick library for python. It supports the functionalities of Imagick API in Python 2.6, 2.7, 3.3+, and PyPy. This library not only helps in processing the images but also provides valuable functionalities for Machine Learning codes using NumPy.
This is the correct code:
you need to transform the colorspace:
with Image(filename=str(f)) as img:
img.transform_colorspace('gray')
This can be achieved by setting the colorspace of the image.
from wand.image import Image
with Image(filename='color.jpg') as img:
img.type = 'grayscale';
img.save(filename='grayscale.jpg');
further reading:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With