python image black and white 1

python image black and white

from PIL import Image
img = Image.open("image.jpg")
img.convert("1").save("result.jpg")

Here is what the above code is Doing:
1. Importing the Image module from the PIL library.
2. Opening the image using the Image.open() function.
3. Converting the image to black and white using the Image.convert() function.
4. Saving the image using the Image.save() function.

The result of running the above code is a black and white image.

If you want to convert an image to black and white, but also want to keep the original image, you can use the Image.copy() function.

Here’s an example:

from PIL import Image
img = Image.open(“image.jpg”)
bw_img = img.convert(“1”)
bw_img.save(“bw_image.jpg”)
img.save(“image.jpg”)

The above code will save two images to your computer. One image will be the original image, and the other image will be the black and white version of the image.

If you want to convert an image to black and white, but also want to keep the original image, you can use the Image.copy() function.

Here’s an example:

from PIL import Image
img = Image.open(“image.jpg”)
bw_img = img.convert(“1”)
bw_img.save(“bw_image.jpg”)
img.save(“image.jpg”)

The above code will save two images to your computer. One image will be the original image, and the other image will be the black and white version of the image.

Similar Posts