Computer Image fundamentals:
- Colors and RGBA Values
- Coordinates and Box Tuples
Manipulate Images:
- Cropping Images
- Resizing an Image
- Rotating and Flipping Images
2 extension thường dùng là .png, .jpeg
(image datatype) - - value -> Tuple(R,G,B,A)
R: Red
G: Green
B: Blue
A: Alpha ( cường độ của màu)
We use Pillow module
From PIL import ImageColor (Image Color is a sub module of Pillow)
Pixel đầu tiên là trái trên, pixel cuối cùng là phải dưới
1 tấm ảnh 3 x 3 resolution sẽ có dữ liệu là:
List[ List[Tuples] ] = [ [(RGBA) (0,0) , (RGBA) (0,1) , (RGBA) (0,2) ]
[(RGBA) (1,0) , (RGBA) (1,1) , (RGBA) (1,2) ]
[(RGBA) (2,0) , (RGBA) (2,1) , (RGBA) (2,2) ] ]
Module PIL
Box Tuples: (Left, Top, Right, Bottom)
note*: there are 2 type of path, which are ABSOLUTE PATH and RELATIVE PATH
There are some type of built-in functions in this module:
Here are the results:
=> 4000 * 6000
Example of a program to crop image:
From PIL import Image
Dog_img = image.open(“./dog.jpg“)
Cropped_dog_img = dog.img.crop( (2000, 1000, 4000, 6000) )
cropped_dog_img.save( “./cropped_dog_img.jpg” )
dog.img.paste( cropped_dog_img, (100,100) )
(here is another img), (here is the top left pixel)
We can resize an image as following:
width , height = dom_img.size
Compressed_10_dog_img = dog_img.resize( (int(width/10) , int(height/10) ))
Compressed_10_dog_img.save (“./image/dog_resize.jpg”)
Or, we can rotate the image as following:
Dog_90 = dog_img.rotate(90) (rotate 90 degree)
However, as the image is 4000 * 6000, when you rotate, some pixels may be deleted.
Therefore, you should expand the pixels:
Dog_90 = dog_img.rotate(90, expand = True)
We can get the stats of a particular pixel as following:
Pixel_at_100_100 = dog_img.getpixel( (100,100) )
print (pixel_at_100_100)
Now, let’s see an assignment to insert logo to several images:
From PIL import Image
Import os
Src_path = “your directory”
For image in os.listdir(src_path):
Img_path = os.path.join(image, src_path)
Img = image.open(img_path)
img.paste( logo, (0,0) )
If not os.path.exist(“ ./logo.image”):
os.makedirs(“ ./logo.image”)
Img.save( )
Logo = image.open(‘./image/logo1.png’) (insert your logo directory here)
Img = image.open( )