SoFunction
Updated on 2024-11-19

python PIL Image image processing basic operation examples

1. Image loading, grayscaling, display and saving

from PIL import Image

img = ('')
imgGrey = ('L')

()
()

('img_copy.jpg')
('img_gray.jpg')

2. Image width, height, channel mode, average value acquisition

from PIL import Image
import numpy as np

img = ('')

width, height = 
channel_mode = 
mean_value = (img)

print(width)
print(height)
print(channel_mode)
print(mean_value)

3. Create an empty image of the specified size and channel type.

from PIL import Image

width = 200
height = 100

img_white = ('RGB', (width,height), (255,255,255))
img_black = ('RGB', (width,height), (0,0,0))
img_L = ('L', (width, height), (255))

img_white.show()
img_black.show()
img_L.show()

4. Accessing and manipulating image pixels

from PIL import Image

img = ('')

width, height = 

# Get the pixel value at the specified coordinate position
pixel_value = ((width/2, height/2))
print(pixel_value)

# Or use the load method
pim = ()
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 the putpixel method
((w//2, h//2), (255,255,255))

# Set the value of the specified area pixel
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)
# ((w, h), (255,255,255))
()

5. Image channel separation and merging

from PIL import Image

img = ('')

# Channel separation
R, G, B = ()

)
()
()

# Channel Merge
img_RGB = ('RGB', (R, G, B))
img_BGR = ('RGB', (B, G, R))
img_RGB.show()
img_BGR.show()

6. Outputting text on images

from PIL import Image, ImageDraw, ImageFont

img = ('')

# Create a Draw object.
draw = (img)
# Font color
fillColor = (255, 0, 0)

text = 'print text on PIL Image'
position = (200,100)

(position, text, fill=fillColor)
()

7. Image scaling

from PIL import Image

img = ('')

width, height = 

img_NEARESET = ((width//2, height//2)) # Scaling default mode is NEARESET (Nearest Neighbor Interpolation)
img_BILINEAR = ((width//2, height//2), ) # BILINEAR Bilinear interpolation of 2x2 regions
img_BICUBIC = ((width//2, height//2), ) # BICUBIC Bicubic interpolation of 4x4 regions
img_ANTIALIAS = ((width//2, height//2), ) # ANTIALIAS High quality downsampling filtering

8. Image traversal operations

from PIL import Image

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

width, height = 

pim = ()

for w in range(width):
for h in range(height):
if pim[w, h] > 100:
((w, h), 255)
# pim[w, h] = 255
else:
((w, h), 0)
# pim[w, h] = 0

()

9. Image thresholding, binarization

from PIL import Image

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

width, height = 

threshold = 125

for w in range(width):
for h in range(height):
if ((w, h)) > threshold:
((w, h), 255)
else:
((w, h), 0)

('')

10. Image cropping

from PIL import Image

img = ('')

width, height = 

# The first two coordinate points are the upper left corner coordinates #
# The last two points are the lower right coordinates #
# width in the front, height in the back
box = (100, 100, 550, 350)

region = (box)

('')

11. Image boundary expansion

# Boundary extensions
from PIL import Image

img = ('')

width, height = 
channel_mode = 

img_makeBorder_full = (channel_mode, (2*width, height))
img_makeBorder_part = (channel_mode, (width+200, height))

# image level extends the entire image
img_makeBorder_full.paste(img, (0, 0, width, height))
img_makeBorder_full.paste(img, (width, 0, 2*width, height))

# The first two coordinate points are the upper left corner coordinates #
# The last two points are the lower right coordinates #
# width in the front, height in the back
box = (width-200, 0, width, height)
region = (box)

# Expand an ROI to the right of the image level
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()

12. Conversion to numpy format

from PIL import Image
import numpy as np

img = ('')

array = (img) # To numpy

img1 = (array) # numpy to
img1 = (('uint8'))

('from_array.jpg')

More aboutPython PIL Image Image Processing Basic Operations ExampleCheck out the relevant links below