Basic Image Operations

Python Pillow Color Conversions

Image Manipulation

Image Filtering

Image Enhancement and Correction

Image Analysis

Advanced Topics

  • Image Module
  • Python Pillow Useful Resources

    Python Pillow - ImageChops.logical_and() Function



    The PIL.ImageChops.logical_and() function performs a logical AND operation between corresponding pixels of two input images. Both input images must have mode "1", representing binary (black-and-white) images. If you intend to perform a logical AND on an image with a mode other than "1", you can use the multiply() function instead, providing a black-and-white mask as the second image.

    The operation is defined as follows −

    $$\mathrm{out\:=\:((image1\:and\:image2)\:\%\:MAX)}$$

    Syntax

    Following is the syntax of the function −

    PIL.ImageChops.logical_and(image1, image2)
    

    Parameters

    Here are the details of this function parameters −

    • image1 − The first input binary image with mode "1".

    • image2 − The second input binary image with mode "1".

    Return Value

    The return type of this function is an Image.

    Examples

    Example 1

    Here is an example that uses the ImageChops.logical_and() function to perform a logical AND operation between two binary images, which are created by the NumPy arrays.

    from PIL import Image, ImageChops
    import numpy as np
    
    # Create two binary images with mode "1"
    array1 = np.array([(255, 64, 3), (255, 0, 0), (255, 255, 0), (255, 255, 255), (164, 0, 3)], dtype=np.uint8)
    array2 = np.array([(200, 14, 3), (25, 222, 0), (255, 155, 0), (255, 55, 100), (180, 0, 78)], dtype=np.uint8)
    
    image1 = Image.fromarray(array1, mode="1")
    image2 = Image.fromarray(array2, mode="1")
    
    # Display the pixel values of the two input images
    print("Pixel values of image1 at (0, 0):", image1.getpixel((0, 0)))
    print("Pixel values of image2 at (0, 0):", image2.getpixel((0, 0)))
    
    # Perform logical AND between the two images
    result = ImageChops.logical_and(image1, image2)
    
    # Display the pixel values of the resulting image at (0, 0)
    print("Pixel values of the result at (0, 0) after logical AND:", result.getpixel((0, 0)))
    

    Output

    Pixel values of image1 at (0, 0): 255
    Pixel values of image2 at (0, 0): 255
    Pixel values of the result at (0, 0) after logical AND: 255
    

    Example 2

    In this example, the PIL.ImageChops.logical_and() function is used to perform a logical AND operation on two binary images (image1 and image2).

    from PIL import Image, ImageChops
    
    # Create two binary images with mode "1"
    image1 = Image.open('Images/dark_img1.png').convert('1')
    image2 = Image.open('Images/dark_img2.png').convert('1')
    
    # Perform logical AND between the two images
    result = ImageChops.logical_and(image1, image2)
    
    # Display the input and resulting images
    image1.show()
    image2.show()
    result.show()
    

    Output

    Input Image 1

    dark img1

    Input Image 2

    dark img2

    Output Image

    imagechops logical and

    Example 3

    Here is another example that demonstrates the working of the logical_and() function.

    from PIL import Image, ImageChops
    
    # Create two binary images with mode "1"
    image1 = Image.open('Images/Car_2.jpg').convert('1')
    image2 = Image.open('Images/ColorDots.png').convert('1')
    
    # Perform logical AND between the two images
    result = ImageChops.logical_and(image1, image2)
    
    # Display the input and resulting images
    image1.show()
    image2.show()
    result.show()
    

    Output

    Input Image 1

    car bw

    Input Image 2

    dots bw

    Output Image

    dotted car
    python_pillow_function_reference.htm
    Advertisements