python字元畫
❶ python 這幾段代碼到底是什麼意思(短代碼) 小弟在學習中
if __name__ == '__main__':
這個是確保執行的本文件的主函數。
python在執行時,會默認給__name__一個值,就是__main__。
舉個栗子:文件p1.py和p2.py,其中在p1中調用了p2.
那麼p2中的if __name__ == '__main__':就不成立,因為在p2環境中,__name__為p2。
我也初學,想問(*im.getpixel((j,i)))這個的意思。。。與君共勉
❷ 用python繪制字元畫
python是數據統計分析軟體,也只能用來對統計分析的結果數據進行繪圖,以展示數據的某種趨勢或數據項的分布(如折線圖、直方圖、餅圖、散點圖)。
你所說的字元畫是一種什麼樣的圖形?什麼樣的畫?
你想要實現什麼樣的目標?
❸ 如何利用Python實現圖片轉字元畫詳解
# -*- coding: utf-8 -*-from PIL import Image
codeLib = '''@B%8&WM#*/\|()1{}[]?-_+~<>i!lI;:,"^`'. '''#生成字元畫所需的字元集count = len(codeLib)def transform1(image_file):
image_file = image_file.convert("L")#轉換為黑白圖片,參數"L"表示黑白模式
codePic = ''
for h in range(0,image_file.size[1]): #size屬性表示圖片的解析度,'0'為橫向大小,'1'為縱向
for w in range(0,image_file.size[0]):
gray = image_file.getpixel((w,h)) #返回指定位置的像素,如果所打開的圖像是多層次的圖片,那這個方法就返回一個元組
codePic = codePic + codeLib[int(((count-1)*gray)/256)]#建立灰度與字元集的映射
codePic = codePic+'\r\n'
return codePicdef transform2(image_file):
codePic = ''
for h in range(0,image_file.size[1]): for w in range(0,image_file.size[0]):
g,r,b = image_file.getpixel((w,h))
gray = int(r* 0.299+g* 0.587+b* 0.114)
codePic = codePic + codeLib[int(((count-1)*gray)/256)]
codePic = codePic+'\r\n'
return codePic
fp = open(u'暴走.jpg','rb')
image_file = Image.open(fp)
image_file=image_file.resize((int(image_file.size[0]*0.75), int(image_file.size[1]*0.5)))#調整圖片大小print u'Info:',image_file.size[0],' ',image_file.size[1],' ',count
tmp = open('tmp.txt','w')
tmp.write(transform1(image_file))
tmp.close()
❹ Python語言在LDLE中怎麼將原點設為海龜形狀
import turtle
turtle.st()
turtle.shape(name='turtle')
# turtle.shape(name=None),返回或設置形狀,最初有以下形狀:「arrow」, 「turtle」, 「circle」, 「square」, 「triangle」, 「classic」。
turtle.done()
❺ 如何用python將圖片轉為字元畫
# coding:utf-8
# 為一張圖片生成對應的字元集圖片
from PIL import Image
import argparse
# 命令行輸入參數處理
parser = argparse.ArgumentParser()
parser.add_argument('file') # 輸入文件
parser.add_argument('-o', '--output') # 輸出文件
parser.add_argument('--width', type=int, default=80) # 輸出字元畫寬
parser.add_argument('--height', type=int, default=80) # 輸出字元畫高
# 獲取參數
args = parser.parse_args()
IMG = args.file
WIDTH = args.width
HEIGHT = args.height
OUTPUT = args.output
ascii_char = list("$@B%8&WM#*/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
# 將256灰度映射到70個字元上
def get_char(r, b, g, alpha=256):
if alpha == 0:
return ' '
length = len(ascii_char)
gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
unit = (256.0 + 1)/length
return ascii_char[int(gray/unit)]
if __name__ == '__main__':
im = Image.open(IMG)
im = im.resize((WIDTH, HEIGHT), Image.NEAREST)
txt = ""
for i in range(HEIGHT):
for j in range(WIDTH):
txt += get_char(*im.getpixel((j, i)))
txt += '\n'
print txt
# 字元畫輸出到文件
if OUTPUT:
with open(OUTPUT,'w') as f:
f.write(txt)
else:
with open("output.txt", 'w') as f:
f.write(txt)
❻ python怎麼根據數據生成圖像
網上有很多的字元畫,看起來很炫酷,下面就告訴你如何用Python做這么炫酷的事,
說下思路吧:
原圖->灰度->根據像素亮度-映射到指定的字元序列中->輸出。
字元越多,字元變化稠密。效果會更好。
如果根據灰度圖的像素亮度范圍製作字元畫,效果會更好。
如果再使用調色板,對字元進行改色,就更像原圖了。
這是原圖:
這是生成的字元畫:
廢話不多說,直接上代碼:
復制代碼 代碼如下:
import Image
chars =" ...',;:clodxkLO0DGEKNWMM"
fn=r'c:\users\liabc\desktop\jianbing.png'
f1=lambda
F:''.join([(k%100!=0) and m or m+'\n' for k,m in enumerate(apply(lambda
x:[chars[x[j,i]%len(chars)] for i in xrange(70) for j in
xrange(100)],(Image.open(F).resize((100,70)).convert("L").load(),)),1)])
f=open(r"c:\users\liabc\desktop\aface.txt","w")
f.write(f1(fn))
f.close()
❼ 怎樣安裝python的圖像處理庫pillow
找到easy_install.exe工具。在windows下安裝Python後,在其安裝路徑下的scripts文件中默認安裝好了easy_install工具。完整路徑如下例:D:\Python27\Scripts\easy_install.exe;其中為我python的安裝路徑,大家可以根據自己的安裝路徑更改。
使用easy_install.exe工具一鍵安裝pip.打開cmd,輸入安裝命令。操作命令如下圖所示:
pip安裝成功後,在cmd下執行pip,將會有如下提示。
再通過pip進行一鍵安裝Pillow。pip類似RedHat裡面的yum,安裝Python包非常方便。操作命令如下圖所示:
5
到這一步就安裝好了。馬上用起來吧,下圖是用這個庫將圖片轉換的字元畫。轉換後有點大,分割成兩張了。
❽ Python字元畫為什麼這樣顯示
記事本對換行符的解析可能會有問題,可以試試用別的編輯器,vscode, notepad++,sublime, atom之類的
❾ 如何用python把圖片字元畫
python字元畫是一個簡單有趣的小程序,非常適合初學者進行學(zhuang)習(bi)。python字元畫生成的原理及其簡單,一句話概括就是----將圖片像素點用不同字元代替,從而將像素組成的圖片轉變成用字元組成的字元畫為了盡可能的使得字元畫展示效果與圖片相同
❿ 用Python怎麼實現這個效果就是用IDLE怎麼實現,急
它這個本質上是個字元串
這種字元串有可能是手工拼接的,但很有可能是用某些專用工具生成的,
參考:字元畫生成器