程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python PIL image operation (II) filter, image synthesis and image effect

編輯:Python

Catalog

ImageFilter Filter

ImageChops Picture synthesis

ImageEnhance Image effects


ImageFilter Filter

ImageFilter The module realizes the filter function , Using functions filter() You can use the filter effect on the specified picture , function filter() The grammar of :Image.filter()      # Parameter is filter mode

ImageFilter Predefined filters and custom filter functions are provided . Commonly used predefined filters :

  • BLUE: Fuzzy
  • CONTOUR: outline
  • DETAIL: details
  • EDGE_ ENHANCE: Edge enhancement
  • EDGE_ ENHANCE _MORE: More edge enhancement
  • EMBOSS: Relief
  • FIND_ EDGES: Looking for the edge
  • SHARPEN: sharpening
  • SMOOTH: smooth

from PIL import Image, ImageFilter
img1 = Image.open('poo/1.jpg')
gaosi = img1.filter(ImageFilter.GaussianBlur) # Gaussian blur
lunkuo = img1.filter(ImageFilter.CONTOUR) # outline
w, h = img1.size
new_img = Image.new('RGB', (w * 3, h), 'yellow')
new_img.paste(img1, (0, 0)) # Original picture
new_img.paste(gaosi, (w, 0))
new_img.paste(lunkuo, (w * 2, 0))
new_img.show()

ImageChops Picture synthesis

ImageChops The module contains several functions for image synthesis . These compositing functions are realized by calculating the pixel values in the channel . It is mainly used to make special effects 、 Composite picture and other operations .

Common built-in functions :

① Additive functions add() Arithmetic addition of two pictures :ImageChops.add(image1, image2, scale= 1.0, offset=0), The value of each pixel in the composite image , Is the corresponding pixel value of two images according to the formula out = ((image1 + image2) / scale + offset) Calculated .

② Subtraction function subtract() Arithmetic subtraction of two pictures :ImageChops.subtract(image1, image2, scale=1.0, offset=0), The value of each pixel in the composite image , Is the corresponding pixel value of two images according to the formula out = ((image1 - image2) / scale + offset) What you get by doing .

③ Darkening function darker() Compare the pixels of two pictures , Take the smaller value of the corresponding pixel in the two pictures , Therefore, the dark part of the corresponding position in the two images is preserved during the synthesis , And remove the bright part :ImageChops.darker(image1, image2),

The calculation formula of pixels out = min(image1, image2)

④ Lightening function lighter() And the darkening function dapker() contrary , The function is to compare two pictures ( Pixel by pixel comparison ), Return to new picture , This new picture is made by superimposing the brighter part of the two pictures . That is, at a certain point , Take the larger value of the two graphs ( bright ) The point of :ImageChops.lighter(image1, image2), The calculation formula of pixels out = max(imagel, image2)

⑤ Superposition function multiply() Superimpose two pictures on each other . If you superimpose a picture with pure black , You will get a pure black picture . If you use pure white to overlay with the picture , The picture will not be affected :ImageChops.multiply (image1, image2) The effect of the composite image is similar to that of two pictures stacked together on transparent tracing paper . Pixel calculation formula out = image1 * image2 / MAX

⑥ Screen functions screen() Reverse color first and then overlay , Realize the effect of composite image , For example, the effect of simultaneously projecting two slides onto one screen with two projectors :ImageChops.screen(image1, image2), Pixel calculation public out=MAX-(MAX-image1)* (MAX-image2)/MAX)

⑦ Inverse color function invert() It is similar to complement in set operation , The maximum value is Max, Subtract each pixel , Take out the reverse color . When the color is reversed 255 Subtract each pixel value of an image , Thus, the inverse phase of the original image is obtained , Its performance is as follows “ negative ” Nature of the image :ImageChops.invert (image), Pixel calculation formula out = MAX - image

⑧ Comparison function difference() Subtract pixel by pixel , Calculate the absolute value . The two images correspond to the image after subtracting the pixel values , Those with the same pixel value are black . It is usually used to find the difference between images :ImageChops.diference (image1, image2), Pixel calculation formula out = abs(image1 - image2)

from PIL import Image,ImageChops
img1 = Image.open('poo/1.jpg')
img2 = Image.open('poo/5.jpg')
ImageChops.add(img1,img2).show()
ImageChops.subtract(img1,img2).show()
ImageChops.darker(img1,img2).show()
ImageChops.lighter(img1,img2).show()
ImageChops.multiply(img1,img2).show()
ImageChops.screen(img1,img2).show()
ImageChops.invert(img2).show()
ImageChops.difference(img1,img2).show()

ImageEnhance Image effects

ImageEnbance The module contains several functions to enhance the image effect , Mainly used to adjust the color of the image 、 Contrast 、 Brightness and clarity, etc . In the module ImageEnhance in , All image enhancement objects are implemented through a common interface method enhance() call ,enhance(factor) Returns an enhanced Image object , Parameters factor Is a greater than 0 Floating point number ,1 Indicates that the original picture is returned .

When in Python Modules used in the program ImageEnhance When enhancing the image effect , You need to first create the corresponding enhancement adjuster , Then call the adjuster output function , According to the specified enhancement factor ( Less than 1 It means to weaken , Greater than 1 Indicates enhancement , be equal to 1 Indicates that the original drawing remains unchanged ) Adjustment , Finally, output the adjusted image .

In the module ImageEnhance in , Common built-in functions :

(1)ImageEnhance.Color(image) : Adjust the color balance of the image

(2) ImageEnhance.Contrast(image): Adjust the image contrast

(3) ImageEnhance.Brightness(image): Adjust the brightness of the image .

(4) ImageEnhance Sharpness(image): Adjust image sharpness , Used to sharpen / Passivation picture .( Sharpening operation factor yes 0~2 Between a floating point number . When factor=0 when , Returns a blurred picture object ; When factor=2 when , Returns a sharpened image object ; When factor=1 when , Returns the original picture object )

from PIL import Image,ImageEnhance
# Adjust color
img2 = Image.open('poo/5.jpg')
img_color = ImageEnhance.Color(img2)
w1, h1 = img2.size
img_out = Image.new('RGB', (w1 * 3, h1), 'green')
imga = img_color.enhance(1.9)
imgb = img_color.enhance(0.5)
img_out.paste(imga, (0, 0))
img_out.paste(img2, (w1, 0)) # Original picture
img_out.paste(imgb, (w1 * 2, 0))
img_out.show()
# Adjust the brightness
img_brightness = ImageEnhance.Brightness(img_out)
imgc = img_brightness.enhance(0.5).show()
# img_out.point(lambda i: i * 0.5).show() # adopt Image Of point() The method can also realize the adjustment of pixel illumination


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved