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

[Python basic series] one small program per day -01

編輯:Python

List of articles

    • subject
    • Reference code
    • The code analysis

subject

The first 0000 topic
Put your QQ Head portrait ( Or Weibo / Letterhead ) Add a red number to the top right corner , It is similar to the prompt effect of the amount of unread information on wechat . Similar to the effect in the picture .

Reference code

from PIL import Image, ImageDraw, ImageColor, ImageFont
def add_num(img):
draw = ImageDraw.Draw(img)
font1 = ImageFont.truetype('/System/Library/Fonts/Apple Symbols.ttf', size=40)
fillcolor = ImageColor.colormap.get('red')
width, height = img.size
draw.text((width-30, 0), '1', font=font1, fill=fillcolor)
# On the new object (width-30, 0) Red at the beginning “1”
img.save('result.jpg', 'jpeg')
return 0
if __name__ == '__main__':
image = Image.open('test.jpg')
add_num(image)

The code analysis

PIL(Python Image Library),Python Image processing standard library of the platform .

  • Open the picture Image.open()
  • Save the picture img.save()
  • Get the picture size width, height = img.size

ImageDraw The module provides a simple representation of the image object 2D draw .

  • establish Draw object draw = ImageDraw.Draw(img)

ImageFont Modular truetype function

  • Usage method :ImageFont.truetype(file,size)
  • effect : Load one TrueType perhaps OpenType The font file , And create a font object . This function loads a font object from the specified file , And create a font object for the font of the specified size .

ImageColor

  • PIL.ImageColor Contains two functions that convert strings to color values getrgb() And getcolor().
  • The color name is passed in as a parameter , Allowed names are defined in PIL.ImageColor.colormap in

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