1

I am looking for a python module that will allow me to import an image from my disk and do the following manipulations :-

  • Convert a black colour pixel to white.

  • Convert all othe colored pixels to white.

So as to obtain a pure black and white image. Since they are so many of modules related to image editing so I am unable to decide which one to use ?

Please Help.

  • PIL seems to be an obvious choice. Was there a particular problem when you tried to use it? – mkrieger1 Jun 16 '21 at 11:56
  • well seems that following your instructions of converting pixels, all pixels will be white meaning that it will be just a picture of white pixels. using PIL might work, there probably is a method to convert an image to bw – Matiiss Jun 16 '21 at 11:59
  • @mkrieger1 I have no idea of this module. – Mr. Anonymous Jun 16 '21 at 12:00
  • Okay. Then you can learn it. – mkrieger1 Jun 16 '21 at 12:01
  • Does this answer your question? [Using python PIL to turn a RGB image into a pure black and white image](https://stackoverflow.com/questions/9506841/using-python-pil-to-turn-a-rgb-image-into-a-pure-black-and-white-image) – Matiiss Jun 16 '21 at 12:01
  • @user933496 it is what You have tagged Yourself: Python Imaging Library - PIL – Matiiss Jun 16 '21 at 12:02
  • 1
    Don't install PIL. Install Pillow instead. It works the same, but is better maintained. – 9769953 Jun 16 '21 at 12:03
  • @9769953 actually that is literally the same exact thing, it is just that You can't `pip install PIL` because there is no such thing so to get PIL one needs to `pip install pillow` but still it is referenced PIL when importing – Matiiss Jun 16 '21 at 12:05
  • @Matiiss Thus, it's not the same thing: if one doesn't mention "install pillow" instead, the OP may be a long time searching how to install a 10+ year old PIL package. – 9769953 Jun 16 '21 at 12:09

1 Answers1

0

You can use the Python Imaging Library (PIL) module to perform the image manipulation tasks you described. PIL allows you to open, manipulate, and save many different image file formats.

Here's an example code that uses PIL to perform the manipulations you described: from PIL import Image

Open the image file:

image = Image.open('image.jpg')

Convert the image to black and white:

image = image.convert('1')

Invert the colors so that black pixels become white and vice versa:

image = ImageOps.invert(image)

Save the modified image to a file:

image.save('modified_image.jpg')

In the code above, the Image class is used to open and manipulate the image.

First, the convert() method is used to convert the image to black and white, the argument '1' specifies that the image should be converted to a black and white image with one bit per pixel.

Then, the invert() method is used to invert the colors so that black pixels become white and vice versa.

Finally, the save() method is used to save the modified image to a file.

Note that you will need to install PIL first, you can install it using pip: pip install pillow

BDL
  • 21,052
  • 22
  • 49
  • 55