# Created by Pastoral CSDN
from PIL import Image
img = Image.open('01.jpg')
imgGrey = img.convert('L')
img.show()
imgGrey.show()
img.save('img_copy.jpg')
imgGrey.save('img_gray.jpg')
# # Created by Pastoral CSDN
from PIL import Image
import numpy as np
img = Image.open('01.jpg')
width, height = img.size
channel_mode = img.mode
mean_value = np.mean(img)
print(width)
print(height)
print(channel_mode)
print(mean_value)
# Created by Pastoral CSDN
from PIL import Image
width = 200
height = 100
img_white = Image.new('RGB', (width,height), (255,255,255))
img_black = Image.new('RGB', (width,height), (0,0,0))
img_L = Image.new('L', (width, height), (255))
img_white.show()
img_black.show()
img_L.show()
# Created by Pastoral CSDN
from PIL import Image
img = Image.open('01.jpg')
width, height = img.size
# Get the pixel value of the specified coordinate position
pixel_value = img.getpixel((width/2, height/2))
print(pixel_value)
# Or use load Method
pim = img.load()
pixel_value1 = pim[width/2, height/2]
print(pixel_value1)
# Set the value of the pixel at the specified coordinate position
pim[width/2, height/2] = (0, 0, 0)
# Or use putpixel Method
img.putpixel((w//2, h//2), (255,255,255))
# Set the value of pixels in the specified area
for w in range(int(width/2) - 40, int(width/2) + 40):
for h in range(int(height/2) - 20, int(height/2) + 20):
pim[w, h] = (255, 0, 0)
# img.putpixel((w, h), (255,255,255))
img.show()
# Created by Pastoral CSDN
from PIL import Image
img = Image.open('01.jpg')
# The channel separation
R, G, B = img.split()
R.show)
G.show()
B.show()
# Channel merging
img_RGB = Image.merge('RGB', (R, G, B))
img_BGR = Image.merge('RGB', (B, G, R))
img_RGB.show()
img_BGR.show()
# Created by Pastoral CSDN
from PIL import Image, ImageDraw, ImageFont
img = Image.open('01.jpg')
# establish Draw object :
draw = ImageDraw.Draw(img)
# The font color
fillColor = (255, 0, 0)
text = 'print text on PIL Image'
position = (200,100)
draw.text(position, text, fill=fillColor)
img.show()
# Created by Pastoral CSDN
from PIL import Image
img = Image.open('01.jpg')
width, height = img.size
img_NEARESET = img.resize((width//2, height//2)) # The default zoom mode is NEARESET( Nearest neighbor interpolation )
img_BILINEAR = img.resize((width//2, height//2), Image.BILINEAR) # BILINEAR 2x2 Bilinear interpolation of regions
img_BICUBIC = img.resize((width//2, height//2), Image.BICUBIC) # BICUBIC 4x4 Bicubic interpolation of regions
img_ANTIALIAS = img.resize((width//2, height//2), Image.ANTIALIAS) # ANTIALIAS High quality down sampling filtering
# Created by Pastoral CSDN
from PIL import Image
img = Image.open('01.jpg').convert('L')
width, height = img.size
pim = img.load()
for w in range(width):
for h in range(height):
if pim[w, h] > 100:
img.putpixel((w, h), 255)
# pim[w, h] = 255
else:
img.putpixel((w, h), 0)
# pim[w, h] = 0
img.show()
# Created by Pastoral CSDN
from PIL import Image
img = Image.open('01.jpg').convert('L')
width, height = img.size
threshold = 125
for w in range(width):
for h in range(height):
if img.getpixel((w, h)) > threshold:
img.putpixel((w, h), 255)
else:
img.putpixel((w, h), 0)
img.save('binary.jpg')
# Created by Pastoral CSDN
from PIL import Image
img = Image.open('01.jpg')
width, height = img.size
# The first two coordinates are the upper left coordinates
# The last two coordinates are the lower right coordinates
# width before , height After
box = (100, 100, 550, 350)
region = img.crop(box)
region.save('crop.jpg')
# Created by Pastoral CSDN
# Boundary extension
from PIL import Image
img = Image.open('test.png')
width, height = img.size
channel_mode = img.mode
img_makeBorder_full = Image.new(channel_mode, (2*width, height))
img_makeBorder_part = Image.new(channel_mode, (width+200, height))
# The image expands the whole image horizontally
img_makeBorder_full.paste(img, (0, 0, width, height))
img_makeBorder_full.paste(img, (width, 0, 2*width, height))
# The first two coordinates are the upper left coordinates
# The last two coordinates are the lower right coordinates
# width before , height After
box = (width-200, 0, width, height)
region = img.crop(box)
# The horizontal right side of the image is expanded by a ROI
img_makeBorder_part.paste(img, (0, 0, width, height))
img_makeBorder_part.paste(region, (width, 0, width+200, height))
img_makeBorder_part.show()
img_makeBorder_full.show()
# Created by Pastoral CSDN
from PIL import Image
import numpy as np
img = Image.open('01.jpg')
array = np.array(img) # PIL.Image turn numpy
img1 = Image.fromarray(array) # numpy turn PIL.Image
img1 = Image.fromarray(array.astype('uint8'))
img1.save('from_array.jpg')