Sunday, June 25, 2023

Image processing in python with pillow

           Image processing in python with pillow

Note:

pillow <2.0.0 suports python versions 2.4,2.5,2.6,2.7

pillow >=2.0.0 supports python versions 2.6,2.7,3.2,3.3,3.4,3.5

BASIC INSTALLATION

installations require building from source as detailed below.

Installation pillow with pip:

$pip install pillow

Introduction

 Modes:

mode

bands

description

"l"

1

black and white (monochrome ),one bit per pixel

"L"

1

gray scale ,one 8-bit byte per pixel

"p"

1

palette encoding :one byte per pixel ,with a palette of class Image palette translating the pixels to colors . this mode is experimental ;refer to the online documentation  

"RHB"

3

true red green blue color ,three bytes per pixel

"RGBA"

4

true color with a transparency band ,four bytes per pixel ,with the A channel varying from 0 to 255 for opaque

"CMYK"

4

cyan magenta yellow black color four bytes per pixel

"YCbCr"

3

color video format , three 8-bit pixels

"I"

1

32-bit  integer pixels

"F"

1

32-bit float pixels

 

Sizes

(width,height)

Coordinates:

(x,y,z)

Open function:

image. open (file)

image.open(file,mode)

 

from PIL import Image

imgvar =Image.open(r"D:\Python27\Lib\site-packages\PIL\flo.jpg")

imgvar.show()

Output









Changing Image type

from PIL import Image

imgvar=Image.open(r"D:\Python27\Lib\site-packages\PIL\flo.jpg")

imgvar.save('new_dog_image.png')

Image name: new_dog_image.png

Resizing images

from PIL import image

imgvar=Image.open(r"D:\Python27\Lib\site-packages\PIL\flo.jpg")

new_imgvar=Image.resize((300,300))

new_imgvar.save('new_300_dog_image.jpg')

print("\n Original image size:",img_var.size)

print("\n resized image size:",imgvar.size)

Output

original image size:(300,168)

resized image size:(150,100)

original image size:(300,168)

Resizing images(thumbnail())

from PIL import image

imgvar=Image.open(r"D:\Python27\Lib\site-packages\PIL\flo.jpg")

imgvar.thumbnail((150,100))

imgavr.save('new_dog_image1.jpg')

print(imgvar.size)

imgvar.show()

OUTPUT

aspect ratio new size(150,84)



 

 

 Example for filter option

from PIL import Image

imgvar = Image.open(r'E:\Lib\site-packages\PIL\BIRD.jpg')

imgvar.thumbnail((120,50),image.NEAREST)

imgvar.save('new_dog_image1.jpg')

print(imgvar.size)

imgvar.show()

Output

the size of the picture is (89,50)

 

ROTATING IMAGE

 

from PIL import Image

im=image.open(r'E:\Lib\site-packages\PIL\BIRD.jpg')

im.rotate(45).show()

 

OUTPUT











this example image dog_image.jpg file rotate 45 degree

 

DRAWING IMAGE

 

from PIL import image

img=image.new("RGB",(100,250),"red")

img.show()

 

OUTPUT



 

 

 

 


INTERPOLATING TO IMAGES USING BLEND()

 

Image.blend(image1,image2,alpha)

 

from PIL import Image

img1=image.open('new_dog_image14.jpg')

img2=image.open('new_cat_image.jpg')

img_blend=Image.blend(img1,img2,0.0)

img_blend.show()

 

 

COLOR TRANSFORM

 

#convert RGB to gray scale mode

from PIL import Image

img1=Image.open(r'E:\Lib\site-packages\PIL\BIRD.jpg')

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

Output











CROPPING IMAGE FILES

 

#convert RBG to gray scale mode

 

from PIL import Image

img1=Image.open(r'E:\Lib\site-packages\PIL\BIRD.jpg')

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

crop_image.save("cropped_dog_image.jpg")

crop_image.show()

Output





FILTER AN IMAGE FILES

 

FILTERS

v BLUR

v CONTOUR

v DETAIL

v EDGE_ENHANCE

v EDGE_ENHANCE_MORE

v EMBOSS

v FIND_EDGES

v SMOOTH

v SMOOTH_MORE

v SHARPEN

 

from PIL import ImageFilter

from PIL import Image

#filter to enhance the picture to emboss

img1=Image.open(r'E:\Lib\site-packages\PIL\BIRD.jpg')

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

Output



















FLIPPING IMAGES

from PIL import Image

imgvar=Image.open(r'E:\Lib\site-packages\PIL\BIRD.jpg')

imgvar.show()

img_flip=imgvar.transpose(Image.FLIP_LEFT_RIGHT)

img_flip.show()

Output


















from PIL import Image

#find the size of the image

img1=Image.open(r'E:\Lib\site-packages\PIL\LILY.jpg')

img1.rotate(45).save("xyz.png")

img1.show()

img1.convert("L").save("jkpwimg1.jpg")

img2=Image.open("jkpwimg1.jpg")

img2.show()

img3=Image.open("xyz.png")

img3.show()

Output













PASTING AN IMAGE ON TO ANOTHER IMAGE

#an image pasted on the another image on the right bottom

from PIL import Image

img1=Image.open(r'E:\Lib\site-packages\PIL\p1.jpg')

img2=Image.open(r'E:\Lib\site-packages\PIL\p2.jpg')

image_copy=img1.copy()

position=((image_copy.width-img2.width-350),(image_copy.height-img2.height-250))

image_copy.paste(img2,position)

image_copy.save('pasted_image.jpg')

image_copy.show()

  

Output

img1


img2



 



0 comments:

Post a Comment