Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use python wand to grayscale image

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
like image 676
user2075719 Avatar asked Apr 22 '13 11:04

user2075719


People also ask

How do I convert to grayscale?

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.

What is Python wand?

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.


2 Answers

This is the correct code:

you need to transform the colorspace:

  with Image(filename=str(f)) as img:
        img.transform_colorspace('gray')
like image 79
Omar Sarhan Avatar answered Sep 19 '22 09:09

Omar Sarhan


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:

  • http://docs.wand-py.org/en/latest/wand/image.html#wand.image.IMAGE_TYPES
  • http://docs.wand-py.org/en/latest/guide/colorspace.html
like image 36
Jen-Ya Avatar answered Sep 20 '22 09:09

Jen-Ya