Plug in development 2022-05-14 13:58:39 阅读数:429
PIL, Full name Python Imaging Library, It's already Python In fact, the image processing standard library of the platform .PIL Very powerful , but API It's very easy to use .Pillow yes PIL A derivative of , But now it has become more than PIL A more dynamic image processing library of its own .pillow It can be said that it has replaced PIL, Encapsulate it as python The library of （pip You can install ）, And support python2 and python3.
Image color ：Pillow One of the colors used in 3 Tuples of elements represent , Such as (R,G,B). It can also be represented by a string with a specific meaning , Like black ：‘black’.
Image: It is Pillow The core class in the library , Represents an image . It has some common methods , Such as reading images , Save image , Image parameters, etc , As shown below ：
from PIL import Image from PIL import ImageFont from PIL import ImageDraw # Reading images from a file img = Image.open('E:/ Photo album / nikon / Flower .jpg'); print((img.format, img.mode, img.size)) # Output picture basic information data = list(img.getdata()) # Return the image sequence data and convert it to rgb list img.show(); img_resize = img.resize((256, 256)) # Adjust the size img_resize.save(" Flower 256.jpg") img_rotate = img.rotate(45) # The center rotates counterclockwise img_rotate.save(" Flower 45.jpg") # Custom new image img2 = Image.new("RGB", (256, 256), "black") # Black background print((img2.format, img2.mode, img2.size)) # Output picture basic information
There are comments on the content in the above code , There are several common function descriptions .Pillow The image processing function of the library is very powerful , The above is just a simple example , The functions involved in character recognition will be updated step by step later .
ImageFont: It represents a font object . Used to draw text on an image . A simple example of creating a font , As shown below ：
from PIL import ImageFont, ImageDraw, Image image = Image.new("RGB", (128, 128), "black") # Black background draw = ImageDraw.Draw(image) # Draw objects # use a truetype font font = ImageFont.truetype('.\chinese_fonts\DroidSansFallbackFull.ttf', 15) draw.text((10, 25), "world", font=font) image.show();
ImageDraw: It is based on Image 2D drawing class . It can be used to generate text containing , Graphics and other required custom images . As shown above , It draws a text in image On .text Method , The prototype is as follows ：
PIL.ImageDraw.Draw.text(xy, text, fill=None, font=None, anchor=None)
Draws the string at the given position.– Draw text at the specified position .
xy – Top left corner of the text.– The upper left corner of the text
text – Text to be drawn.– Text to be drawn
font – An ImageFont instance.– Font object
fill – Color to use for the text.– Fill color
The source code is as follows ：
def Pt2MM(val): ret = val * 25.4 / 72; return ret; def GetFontRenderSize(text, fontsize): canvas = Image.new('RGB', (512, 512)); draw = ImageDraw.Draw(canvas); monospace = ImageFont.truetype(r"msyh.ttc", fontsize); draw.text((0, 0), text, font=monospace, fill="white"); bbox = canvas.getbbox(); # Wide and high sizept = (bbox - bbox, bbox - bbox); fontsizewmm = Pt2MM(sizept); fontsizehmm = Pt2MM(sizept); return (fontsizewmm, fontsizehmm);
If you have any questions , Please leave a message .
版权声明：本文为[Plug in development]所创，转载请带上原文链接，感谢。 https://pythonmana.com/2022/134/202205141347179100.html