Python  - Image Handling                                     Home : www.sharetechnote.com

 

 

 

 

Python - Image Handling

 

 

 

 

 

Reading and Displaying Image with PIL

 

    import PIL

    from PIL import Image

     

    img = Image.open('Scene_01.png')

     

    print('Format = ',img.format, ' ,Mode = ',img.mode, ' ,Size = ', img.size)

    img.show()

       ==>

 

 

 

Reading and Displaying Image with matplotlib

 

    from matplotlib import image

    from matplotlib import pyplot

     

    img = image.imread('Scene_01.png')

     

    print('Data Type = ',img.dtype,' ,Size = ', img.shape)

      ==> Data Type =  float32  ,Size =  (734, 1137, 4)

     

    pyplot.imshow(img)

    pyplot.show()

      ==>

       

 

 

 

Resizing the Image

 

    import PIL

    from PIL import Image

     

    img = Image.open('Scene_01.png')

     

    print('Format = ',img.format, ' ,Mode = ',img.mode, ' ,Size = ', img.size)

       ==> Format =  PNG  ,Mode =  RGBA  ,Size =  (1137, 734)

     

    img.show()

       ==>

      

     

    img_resized = img.resize((200,200));

    print('Format = ',img_resized.format, ' ,Mode = ',img_resized.mode, ' ,Size = ', img_resized.size)

       ==> Format =  None  ,Mode =  RGBA  ,Size =  (200, 200)

     

    img_resized.show()

       ==>

 

 

 

Rotating the Image

 

    import PIL

    from PIL import Image

    from matplotlib import pyplot

     

    img = Image.open('Scene_01.png')

     

    pyplot.subplot(1,3,1)

    pyplot.imshow(img)

     

    pyplot.subplot(1,3,2)

    rot45 = img.rotate(45);

    pyplot.imshow(rot45)

     

    pyplot.subplot(1,3,3)

    rot90 = img.rotate(90);

    pyplot.imshow(rot90)

     

    pyplot.show()

      ==>

 

 

 

Cropping the Image

 

    import PIL

    from PIL import Image

    from matplotlib import pyplot

     

    img = Image.open('Scene_01.png')

     

    pyplot.subplot(1,3,1)

    pyplot.imshow(img)

     

    pyplot.subplot(1,3,2)

    cImg01 = img.crop((200,100,300,200));

    pyplot.imshow(cImg01)

     

    pyplot.subplot(1,3,3)

    cImg02 = img.crop((200,200,600,600));

    pyplot.imshow(cImg02)

     

    pyplot.show()

     ==>

 

 

 

Converting the Image Color

 

    import PIL

    from PIL import Image

    from matplotlib import pyplot

     

    img = Image.open('Scene_01.png')

     

    pyplot.subplot(1,2,1)

    pyplot.imshow(img)

     

    pyplot.subplot(1,2,2)

    imgGray = img.convert(mode='L')

    pyplot.imshow(imgGray)

     

    pyplot.show()

    ==>

     

 

 

 

Image to Array, Array to Image

 

    import PIL

    from PIL import Image

    from matplotlib import pyplot

    from numpy import asarray

     

    img = Image.open('Scene_01.png')

    imgAry = asarray(img);

     

    print('Size = ',imgAry.size, ',shape=', imgAry.shape)

       ==> Size =  3338232 ,shape= (734, 1137, 4)

     

    print(imgAry[0])

      ==> [[ 40  55  73 255]

             [ 42  57  74 255]

             [ 43  58  76 255]

               ...

            [129 149 175 255]

            [130 148 175 255]

            [130 148 175 255]]

      

     

    imgAry = 2 * imgAry - 100;

     

    mImg = Image.fromarray(imgAry)

     

    pyplot.subplot(1,2,1)

    pyplot.imshow(img)

     

    pyplot.subplot(1,2,2)

    pyplot.imshow(mImg)

     

    pyplot.show()

       ==>

       

        

 

 

 

Cropping image from Array

 

    import PIL

    from PIL import Image

    from matplotlib import pyplot

    from numpy import asarray

     

    img = Image.open('Scene_01.png')

    imgAry = asarray(img);

     

    imgAryCropped = imgAry[400:499,300:399];

     

    mImg = Image.fromarray(imgAryCropped)

     

    pyplot.subplot(1,2,1)

    pyplot.imshow(img)

     

    pyplot.subplot(1,2,2)

    pyplot.imshow(mImg)

     

    pyplot.show()

    ==>

 

 

 

Splitting Image into Color Channels

 

    import PIL

    from PIL import Image

    from matplotlib import pyplot

    from numpy import asarray

     

    img = Image.open('Scene_01.png')

    imgAry = asarray(img);

     

    print('Size = ',imgAry.size, ',shape=', imgAry.shape)

    dim = imgAry.shape;

     

    imgAryCroppedCh1 = imgAry[0:dim[0],0:dim[1],0];

    imgAryCroppedCh2 = imgAry[0:dim[0],0:dim[1],1];

    imgAryCroppedCh3 = imgAry[0:dim[0],0:dim[1],2];

     

    mImgCh1 = Image.fromarray(imgAryCroppedCh1)

    mImgCh2 = Image.fromarray(imgAryCroppedCh2)

    mImgCh3 = Image.fromarray(imgAryCroppedCh3)

     

    pyplot.subplot(2,2,1)

    pyplot.imshow(img)

     

    pyplot.subplot(2,2,2)

    pyplot.imshow(mImgCh1,cmap='Reds')

     

    pyplot.subplot(2,2,3)

    pyplot.imshow(mImgCh2,cmap='Greens')

     

    pyplot.subplot(2,2,4)

    pyplot.imshow(mImgCh3,cmap='Blues')

     

    pyplot.show()

    ==>

 

 

 

 

Saving the image

 

    import PIL

    from PIL import Image

    from matplotlib import pyplot

     

    img = Image.open('Scene_01.png')

     

    img.save('Scene_01.bmp', format='bmp')