2017/10/17

Python - draw text on image and image to numpy array

markdown ## 目標 嘗試生成以微軟正黑體寫成的數字0~9並轉換成 numpy array ##本文包含 * 生成圖片和保存圖片 * 在圖片上寫出指定字型和大小的字 * 設定反鋸齒模式 * 圖片轉成 numpy array ## 程式碼 ``` import numpy from PIL import Image from PIL import ImageFont from PIL import ImageDraw image_size = (8,13) font_size = 10 for i in range(10): # 空白圖片生成 image = Image.new('L', image_size, 0) # 取得繪圖器 draw = ImageDraw.Draw(image) # 微軟正黑體 font = ImageFont.truetype("C:/Windows/Fonts/msjh.ttc", font_size) # 關閉反鋸齒 draw.fontmode = '1' # 測量文字尺寸 text_size = draw.textsize(str(i),font) # print('text_size:', text_size) # 文字置中 text_position = ((image_size[0]-text_size[0])//2,(image_size[1]-text_size[1])//2) # print('text_position:', text_position) # 畫上文字 draw.text(text_position, str(i), 255, font) # 存檔 image.save(str(i)+'.bmp') # 轉成 numpy array na = numpy.array(image.getdata()).reshape(image.size[1], image.size[0]) # 印出 print(na) ``` ## 輸出 ``` Using TensorFlow backend. [[ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 255 255 255 255 0 0] [ 0 0 255 0 0 255 255 0] [ 0 255 0 0 0 0 255 0] [ 0 255 0 0 0 0 255 0] [ 0 255 0 0 0 0 255 0] [ 0 255 0 0 0 0 255 0] [ 0 255 255 0 0 255 0 0] [ 0 0 255 255 255 255 0 0] [ 0 0 0 0 0 0 0 0]] [[ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 255 255 0 0 0] [ 0 0 255 255 255 0 0 0] [ 0 0 0 0 255 0 0 0] [ 0 0 0 0 255 0 0 0] [ 0 0 0 0 255 0 0 0] [ 0 0 0 0 255 0 0 0] [ 0 0 0 0 255 0 0 0] [ 0 0 255 255 255 255 255 0] [ 0 0 0 0 0 0 0 0]] [[ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 255 255 255 0 0] [ 0 0 255 0 0 0 255 0] [ 0 0 0 0 0 0 255 0] [ 0 0 0 0 0 0 255 0] [ 0 0 0 0 0 255 0 0] [ 0 0 0 0 255 0 0 0] [ 0 0 0 255 0 0 0 0] [ 0 0 255 255 255 255 255 0] [ 0 0 0 0 0 0 0 0]] [[ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 255 255 0 0 0] [ 0 0 255 0 0 255 0 0] [ 0 0 0 0 0 255 0 0] [ 0 0 0 255 255 0 0 0] [ 0 0 0 0 0 255 0 0] [ 0 0 0 0 0 255 0 0] [ 0 0 255 0 0 255 0 0] [ 0 0 255 255 255 0 0 0] [ 0 0 0 0 0 0 0 0]] [[ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 255 255 0 0] [ 0 0 0 0 255 255 0 0] [ 0 0 0 255 0 255 0 0] [ 0 0 255 0 0 255 0 0] [ 0 255 0 0 0 255 0 0] [ 0 255 255 255 255 255 255 0] [ 0 0 0 0 0 255 0 0] [ 0 0 0 0 0 255 0 0] [ 0 0 0 0 0 0 0 0]] [[ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 255 255 255 255 0 0] [ 0 0 255 0 0 0 0 0] [ 0 0 255 0 0 0 0 0] [ 0 0 255 255 255 0 0 0] [ 0 0 0 0 0 255 0 0] [ 0 0 0 0 0 255 0 0] [ 0 0 255 0 0 255 0 0] [ 0 0 255 255 255 0 0 0] [ 0 0 0 0 0 0 0 0]] [[ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 255 255 255 0] [ 0 0 0 255 0 0 0 0] [ 0 0 255 0 0 0 0 0] [ 0 0 255 0 255 255 0 0] [ 0 0 255 255 0 0 255 0] [ 0 0 255 0 0 0 255 0] [ 0 0 255 0 0 0 255 0] [ 0 0 0 255 255 255 0 0] [ 0 0 0 0 0 0 0 0]] [[ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 255 255 255 255 255 255 0] [ 0 0 0 0 0 255 0 0] [ 0 0 0 0 0 255 0 0] [ 0 0 0 0 255 0 0 0] [ 0 0 0 0 255 0 0 0] [ 0 0 0 255 0 0 0 0] [ 0 0 0 255 0 0 0 0] [ 0 0 255 0 0 0 0 0] [ 0 0 0 0 0 0 0 0]] [[ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 255 255 0 0 0] [ 0 0 255 0 0 255 0 0] [ 0 0 255 0 0 255 0 0] [ 0 0 0 255 255 0 0 0] [ 0 0 0 255 0 255 0 0] [ 0 0 255 0 0 0 255 0] [ 0 0 255 0 0 0 255 0] [ 0 0 0 255 255 255 0 0] [ 0 0 0 0 0 0 0 0]] [[ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0] [ 0 0 0 255 255 255 0 0] [ 0 0 255 0 0 0 255 0] [ 0 0 255 0 0 0 255 0] [ 0 0 255 0 0 0 255 0] [ 0 0 0 255 255 255 255 0] [ 0 0 0 0 0 0 255 0] [ 0 0 0 0 0 255 0 0] [ 0 0 255 255 255 0 0 0] [ 0 0 0 0 0 0 0 0]] ``` ## 參考文件 PIL影像相關:[http://pillow.readthedocs.io/en/stable/reference/index.html](http://pillow.readthedocs.io/en/stable/reference/index.html)

沒有留言: