In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what are the basic operations of python PIL Image image processing". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "what are the basic operations of python PIL Image image processing" together!
1. Image loading, grayscale, display and save from PIL import Imageimg = Image.open ('01.jpg ')imgGrey = img.convert ('L')img.show()imgGrey.show()img.save ('img_copy.jpg ')imgGrey.save ('img_gray.jpg')2. Image width, height, channel mode, average Get from PIL import Imageimport numpy as npimg = Image.open ('01.jpg ')width, height = img.sizechannel_mode = img.modemean_value = np.mean(img)print(width)print(height)print(channel_mode)print(mean_value)3. Create an empty image of the specified size and specified channel type from PIL import Imagewidth = 200height = 100img_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()4. Access and manipulate image pixels from PIL import Imageimg = Image.open ('01.jpg ')width, height = img.size#Get 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 pixel value at specified coordinate position pim[width/2, height/2] = (0, 0, 0 0)#Or use putpixel method img.putpixel((w//2, h//2), (255,255,255))#Set the pixel value of 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))img.show()5. Image Channel Split and Merge from PIL import Imageimg = Image.open ('01.jpg ')#Channel Split R, G, B = img.split()R.show)G.show()B.show()#Channel Merge img_RGB = Image.merge ('RGB', (R, G, B) img_BGR = Image.merge ('RGB ', (B, G, R) img_RGB.show()img_BGR.show()6. Output text from PIL import Image, ImageDraw, ImageFontimg = Image.open ('01.jpg ')#Create Draw object:draw = ImageDraw.Draw(img)#Font Color fillColor = (255, 0, 0)text ='print text on PIL Image'position = (200,100)draw.text(position, text, fill=fillColor)img.show()7. Image zoom from PIL import Imageimg = Image.open ('01.jpg ')width, height = img.sizeimg_NEARESET = img.resize((width//2, height//2)) #Zoom default mode is NEARESET img_BILINEAR = img.resize((width//2, height//2), Image.BILINEAR) #Bilinear interpolation of BILINEAR 2x2 region img_BICUBIC = img.resize(width//2, height//2), Image.BICUBIC) # BICUBIC bicubic interpolation in 4x4 region img_ANTIALIAS = img.resize((width//2, height//2), Image.ANTIALIAS) # ANTIALIAS High Quality Downsampling Filtering 8. Image traversal operation from PIL import Imageimg = Image.open ('01.jpg').convert('L')width, height = img.sizepim = 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] = 255else:img.putpixel((w, h), 0)# pim[w, h] = 0img.show ()9. Image thresholding, binarization from PIL import Imageimg = Image.open ('01.jpg').convert('L')width, height = img. sizeththreshold = 125for 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')10. Image cropping from PIL import Imageimg = Image.open ('01.jpg ')width, height = img.size#The first two coordinates are the upper-left coordinates #The second two coordinates are the lower-right coordinates # width first, height second box = (100, 100, 550, 350)region = img.crop(box)region.save ('crop.jpg')11. Image Boundary Extension #Boundary Extension from PIL import Imageimg = Image.open ('test.png ')width, height = img.sizechannel_mode = img.modeimg_makeOrder_full = Image.new (channel_mode, (2*width, height))img_makeOrder_part = Image.new(channel_mode, (width+200, height))#Image Horizontal Extension Entire Image img_makeOrder_full.paste(img,(width, (0, 0, width, height))img_makeOrder_full.paste(img, (width, 0, 2*width, height))#First two coordinate points are upper-left coordinates #Last two coordinate points are lower-right coordinates #First, height After box = (width-200, 0, width, height)region = img.crop(box)#Extend a ROIimg_makeOrder_part.paste(img, (0, width, height))img_makeOrder_part.paste(region, (width, 0, width+200, height))img_makeOrder_part.show()img_makeOrder_full.show()12. PIL.Image and numpy format conversion from PIL import Imageimport numpy as npimg = Image.open ('01.jpg ')array = np.array(img) # PIL.Image to numpyimg1 = Image. fromarray(array) # numpy to PIL. Imageimg1 = Image.fromarray(array. atype ('uint 8'))img1.save ('from_array.jpg') Thank you for reading, the above is "python PIL Image image processing basic operations what" content, after learning this article, I believe that everyone has a deeper understanding of the basic operation of python PIL Image image processing, and the specific use situation needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.