基於python實現圖片轉字符畫代碼實例

這篇文章主要介紹了基於python實現圖片轉字符畫代碼實例,文中經過示例代碼介紹的很是詳細,對你們的學習或者工做具備必定的參考學習價值,須要的朋友能夠參考下python

直接上代碼圖片就使用我家爽妹子的吧web

若是沒有安裝pil模塊的話先cmd安裝下微信

輸入:pip install pillow網絡

   
  1. # -*- coding: utf-8 -*-app

  2. from PIL import Imageless

  3.  編輯器

  4. codeLib = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`'. '''#生成字符畫所需的字符集學習

  5. count = len(codeLib)flex

  6.  url

  7. def transform1(image_file):

  8. image_file = image_file.convert("L")#轉換爲黑白圖片,參數"L"表示黑白模式

  9. codePic = ''

  10. for h in range(0,image_file.size[1]): #size屬性表示圖片的分辨率,'0'爲橫向大小,'1'爲縱向

  11. for w in range(0,image_file.size[0]):

  12. gray = image_file.getpixel((w,h)) #返回指定位置的像素,若是所打開的圖像是多層次的圖片,那這個方法就返回一個元組

  13. codePic = codePic + codeLib[int(((count-1)*gray)/256)]#創建灰度與字符集的映射

  14. codePic = codePic+'\r\n'

  15. return codePic

  16.  

  17. def transform2(image_file):

  18. codePic = ''

  19. for h in range(0,image_file.size[1]):

  20. for w in range(0,image_file.size[0]):

  21. g,r,b = image_file.getpixel((w,h))

  22. gray = int(r* 0.299+g* 0.587+b* 0.114)

  23. codePic = codePic + codeLib[int(((count-1)*gray)/256)]

  24. codePic = codePic+'\r\n'

  25. return codePic

  26. fp = open(r'C:\路\3.jpg','rb')

  27. image_file = Image.open(fp)

  28. image_file=image_file.resize((int(image_file.size[0]*0.2), int(image_file.size[1]*0.1)))#調整圖片大小

  29. print (u'Info:',image_file.size[0],' ',image_file.size[1],' ',count)

  30.  

  31. tmp = open('tmp.txt','w')

  32. tmp.write(transform1(image_file))

  33. tmp.close()

  34. a,b,c=1,2,3

  35. print(a,b,c)

結果不知還可否看出來

以上就是本文的所有內容,但願對你們的學習有所幫助

end


歡迎你們點贊,留言,轉發,轉載,感謝你們的相伴與支持

萬水千山老是情,點個【在看】行不行

*聲明:本文於網絡整理,版權歸原做者全部,如來源信息有誤或侵犯權益,請聯繫咱們刪除或受權事宜。


本文分享自微信公衆號 - python教程(pythonjc)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索