Python每天一題 0000

2021-07-22 09:51:26 字數 1470 閱讀 4697

#-*- coding: utf-8 -*-

from pil import image

im = image.open("c:/1.png")

print im.show()

使用image模組,i是大寫的,要寫from pil 不能直接寫import

使用dir檢視im有的屬性和方法。

先試了size

im.size

返回乙個元組

sizeout = im.size

返回乙個元組

也可以

x,y = im.size

然後是使用兩個模組

imagefont

imagedraw

設定字型和在上進行畫

imagefont 用於imagedraw類的text()

imagefont的truetype

imagefont.truetype(file,size, encoding=value)

使用arial字型

arial.ttf

即 font = imagefont.truetype(「arial.ttf」,3)

建立完font在建立乙個draw

draw = imagedraw.draw(im)

然後使用draw的text方法在上寫字

pil.imagedraw.draw.text(xy, text, fill=none, font=none, anchor=none)
引數:

xy – top left corner of the text.

text – text to be drawn.

font – an imagefont instance.

fill – color to use for the text.

也就是說第乙個引數是位置以左上角為初始位置,第二個引數是寫的文字內容,第三個引數是顏色。如(255,0,0)就是紅色。然後是字型

**

#-*- coding: utf-8 -*-

from pil import image

from pil import imagedraw

from pil import imagefont

im = image.open("c:/1.png")

#print im.show()

#print dir(im)

x,y = im.size

print x,y

font = imagefont.truetype("arial.ttf",int(y/5))

draw = imagedraw.draw(im)

draw.text((0.9*x,0.1*y),"3",(255,0,0),font)

print im.show()

參考:

Python每天一題 0001

今天比較有空就在寫乙個 這個就比較簡單了 就是生成啟用碼200個 我寫的是數字和大寫字母都有的那種 像這種 anoqeo8m taeccdki fhni80h1 y9w1wa4b 用的random的choice在列表中隨機出乙個,然後我讓他字元累加在一起8個 就是可以有256種。coding utf...

LeetCode 每天一題 python

請你來實現乙個 atoi 函式,使其能將字串轉換成整數。首先,該函式會根據需要丟棄無用的開頭空格字元,直到尋找到第乙個非空格的字元為止。接下來的轉化規則如下 如果第乙個非空字元為正或者負號時,則將該符號與之後面盡可能多的連續數字字元組合起來,形成乙個有符號整數。假如第乙個非空字元是數字,則直接將其與...

LeetCode 每天一題 python

判斷乙個整數是否是回文數。回文數是指正序 從左向右 和倒序 從右向左 讀都是一樣的整數。示例 1 輸入 121 輸出 true 示例 2 輸入 121 輸出 false 解釋 從左向右讀,為 121 從右向左讀,為 121 因此它不是乙個回文數。示例 3 輸入 10 輸出 false 解釋 從右向左...