Tuesday, 6 August 2024

Python - Image Processing

 -----------------------------------------------

Python Image Processing using Pillow:

----------------------------------------------

PIL(Python Imaging Library):

----

-PIL is a library(Standard Procedures)

-Support file formats : PNG,JPG,JPEG,GIF,PPM,TIFF and BMP

-Operation on Images: Cropping, Resizing, Rotating, Greyscaling and adding text to images  


Installation:

--------------

Step 1: open command prompt

Step 2: type following path : C:\Users\user\AppData\Local\Programs\Python\Python36-32\Scripts

Step 3: pip install Pillow

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 1: Display Image

from PIL import Image

img1=Image.open("Desert.jpg")

img1.show()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Attributes:filename,format,mode,size,width,height

#Program 2: Display Image & Format,size, Mode

from PIL import Image

img1=Image.open("1.jpg")

img1.show()

print("Format :",img1.format)

print("size :",img1.size)

print("Mode :",img1.mode)

----------------------------

#Program 3: Changing Image Type or copy Image

from PIL import Image

img1=Image.open("1.jpg")

img1.save("new.png")

img1.thumbnail((80,80))

img1.show()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 4: Resizing Images

from PIL import Image

img1=Image.open("1.jpg")

img2=img1.resize((250,250))

print("Original Image Size : ",img1.size)

print("Resized Image Size : ",img2.size)

img2.show()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 5: Cropping Image files(portion of the image)

from PIL import Image

img1=Image.open("Desert.jpg")

img2=img1.crop((100,100,150,150))

img2.show()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 6: Rotating Images

from PIL import Image

img1=Image.open("Desert.jpg")

img1.rotate(45).show()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 7: Color Transforms

from PIL import Image

img1=Image.open("Desert.jpg")

img1.convert("L").show()

#note : L - greyscale from RGB and CMYK Images

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 8: Filter an Image

from PIL import Image

from PIL import ImageFilter

img1=Image.open("Desert.jpg")

img1.filter(ImageFilter.EMBOSS).show()


Note:

-----

BLUR,CONTOUR,EDGE_ENHANCE,EDGE_ENHANCE_MORE,EMBOSS,FIND_EDGES

SMOOTH,SMOOTH_MORE,SHARPEN

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 9: Flipping an Image

from PIL import Image

img1=Image.open("Desert.jpg")

img1.show()

img1.transpose(Image.FLIP_LEFT_RIGHT).show()

Note:

-----

FLIP_TOP_BOTTOM,ROTATE_90,ROTATE_180,ROTATE_270

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


No comments:

Post a Comment