国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python 實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)的示例(靜態(tài)圖片,gif皆可)

瀏覽:22日期:2022-07-06 10:58:30

字符畫(huà)是一種由字母、標(biāo)點(diǎn)或其他字符組成的圖畫(huà),它產(chǎn)生于互聯(lián)網(wǎng)時(shí)代,在聊天軟件中使用較多,本文我們看一下如何將自己喜歡的圖片轉(zhuǎn)成字符畫(huà)。

靜態(tài)圖片

首先,我們來(lái)演示將靜態(tài)圖片轉(zhuǎn)為字符畫(huà),功能實(shí)現(xiàn)主要用到的 Python 庫(kù)為 OpenCV,安裝使用 pip install opencv-python 命令即可。

功能實(shí)現(xiàn)的基本思路為:利用聚類將像素信息聚為 3 或 5 類,顏色最深的一類用數(shù)字密集度表示,陰影的一類用橫杠(-)表示,明亮部分用空白表示。

主要代碼實(shí)現(xiàn)如下:

def img2strimg(frame, K=5): if type(frame) != np.ndarray: frame = np.array(frame) height, width, *_ = frame.shape frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame_array = np.float32(frame_gray.reshape(-1)) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) flags = cv2.KMEANS_RANDOM_CENTERS # 得到 labels(類別)、centroids(矩心) compactness, labels, centroids = cv2.kmeans(frame_array, K, None, criteria, 10, flags) centroids = np.uint8(centroids) # labels 的數(shù)個(gè)矩心以隨機(jī)順序排列,所以需要簡(jiǎn)單處理矩心 centroids = centroids.flatten() centroids_sorted = sorted(centroids) # 獲得不同 centroids 的明暗程度,0 為最暗 centroids_index = np.array([centroids_sorted.index(value) for value in centroids]) bright = [abs((3 * i - 2 * K) / (3 * K)) for i in range(1, 1 + K)] bright_bound = bright.index(np.min(bright)) shadow = [abs((3 * i - K) / (3 * K)) for i in range(1, 1 + K)] shadow_bound = shadow.index(np.min(shadow)) labels = labels.flatten() # 將 labels 轉(zhuǎn)變?yōu)閷?shí)際的明暗程度列表 labels = centroids_index[labels] # 解析列表 labels_picked = [labels[rows * width:(rows + 1) * width:2] for rows in range(0, height, 2)] canvas = np.zeros((3 * height, 3 * width, 3), np.uint8)# 創(chuàng)建長(zhǎng)寬為原圖三倍的白色畫(huà)布 canvas.fill(255) y = 8 for rows in labels_picked: x = 0 for cols in rows: if cols <= shadow_bound:cv2.putText(canvas, str(random.randint(2, 9)), (x, y), cv2.FONT_HERSHEY_PLAIN, 0.45, 1) elif cols <= bright_bound:cv2.putText(canvas, '-', (x, y), cv2.FONT_HERSHEY_PLAIN, 0.4, 0, 1) x += 6 y += 6 return canvas

原圖如下:

Python 實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)的示例(靜態(tài)圖片,gif皆可)

效果圖如下:

Python 實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)的示例(靜態(tài)圖片,gif皆可)

GIF 動(dòng)圖

接下來(lái)我們演示將 GIF 轉(zhuǎn)為字符畫(huà),功能實(shí)現(xiàn)主要用到的 Python 庫(kù)為 imageio、Pillow,安裝使用 pip install imageio/Pillow 命令即可。

功能實(shí)現(xiàn)的基本思路如下:

將 gif 圖片的每一幀拆分為靜態(tài)圖片將所有靜態(tài)圖片變?yōu)樽址?huà)將所有字符畫(huà)重新合成 gif主要代碼實(shí)現(xiàn)如下:

# 拆分 gif 將每一幀處理成字符畫(huà)def gif2pic(file, ascii_chars, isgray, font, scale): ’’’ file: gif 文件 ascii_chars: 灰度值對(duì)應(yīng)的字符串 isgray: 是否黑白 font: ImageFont 對(duì)象 scale: 縮放比例 ’’’ im = Image.open(file) path = os.getcwd() if(not os.path.exists(path+'/tmp')): os.mkdir(path+'/tmp') os.chdir(path+'/tmp') # 清空 tmp 目錄下內(nèi)容 for f in os.listdir(path+'/tmp'): os.remove(f) try: while 1: current = im.tell() name = file.split(’.’)[0]+’_tmp_’+str(current)+’.png’ # 保存每一幀圖片 im.save(name) # 將每一幀處理為字符畫(huà) img2ascii(name, ascii_chars, isgray, font, scale) # 繼續(xù)處理下一幀 im.seek(current+1) except: os.chdir(path)# 將不同的灰度值映射為 ASCII 字符def get_char(ascii_chars, r, g, b): length = len(ascii_chars) gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b) return ascii_chars[int(gray/(256/length))]# 將圖片處理成字符畫(huà)def img2ascii(img, ascii_chars, isgray, font, scale): scale = scale # 將圖片轉(zhuǎn)換為 RGB 模式 im = Image.open(img).convert(’RGB’) # 設(shè)定處理后的字符畫(huà)大小 raw_width = int(im.width * scale) raw_height = int(im.height * scale) # 獲取設(shè)定的字體的尺寸 font_x, font_y = font.getsize(’ ’) # 確定單元的大小 block_x = int(font_x * scale) block_y = int(font_y * scale) # 確定長(zhǎng)寬各有幾個(gè)單元 w = int(raw_width/block_x) h = int(raw_height/block_y) # 將每個(gè)單元縮小為一個(gè)像素 im = im.resize((w, h), Image.NEAREST) # txts 和 colors 分別存儲(chǔ)對(duì)應(yīng)塊的 ASCII 字符和 RGB 值 txts = [] colors = [] for i in range(h): line = ’’ lineColor = [] for j in range(w): pixel = im.getpixel((j, i)) lineColor.append((pixel[0], pixel[1], pixel[2])) line += get_char(ascii_chars, pixel[0], pixel[1], pixel[2]) txts.append(line) colors.append(lineColor) # 創(chuàng)建新畫(huà)布 img_txt = Image.new(’RGB’, (raw_width, raw_height), (255, 255, 255)) # 創(chuàng)建 ImageDraw 對(duì)象以寫(xiě)入 ASCII draw = ImageDraw.Draw(img_txt) for j in range(len(txts)): for i in range(len(txts[0])): if isgray:draw.text((i * block_x, j * block_y), txts[j][i], (119,136,153)) else:draw.text((i * block_x, j * block_y), txts[j][i], colors[j][i]) img_txt.save(img)# 讀取 tmp 目錄下文件合成 gifdef pic2gif(dir_name, out_name, duration): path = os.getcwd() os.chdir(dir_name) dirs = os.listdir() images = [] num = 0 for d in dirs: images.append(imageio.imread(d)) num += 1 os.chdir(path) imageio.mimsave(out_name + ’_ascii.gif’,images,duration = duration)

原圖如下:

Python 實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)的示例(靜態(tài)圖片,gif皆可)

黑白效果圖如下:

Python 實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)的示例(靜態(tài)圖片,gif皆可)

彩色效果圖如下:

Python 實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)的示例(靜態(tài)圖片,gif皆可)

總結(jié)

本文我們利用 Python 演示了將靜態(tài)圖和 GIF 轉(zhuǎn)為字符畫(huà)的方法,大家如果有興趣的話,可以將自己喜歡的圖轉(zhuǎn)一下,如果對(duì)轉(zhuǎn)換效果不滿意,還可以修改代碼,改成自己滿意的效果。

示例代碼:py-ascii

以上就是Python 實(shí)現(xiàn)圖片轉(zhuǎn)字符畫(huà)的示例(靜態(tài)圖片,gif皆可)的詳細(xì)內(nèi)容,更多關(guān)于python 圖片轉(zhuǎn)字符畫(huà)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
主站蜘蛛池模板: 三级视频网站在线观看 | 窝窝午夜看片七次郎青草视频 | 久草视频在线观 | 91精品国产免费网站 | 成人看免费一级毛片 | 国产精品美女免费视频大全 | 国产一区第一页 | 4tube高清性欧美 | 久久精品全国免费观看国产 | 亚洲欧美一区二区视频 | 黑人巨大videos极度另类 | 在线欧洲成人免费视频 | 国产手机国产手机在线 | 国产a一级 | 日本不卡一二三区 | 欧美另类自拍 | 久久橹 | 欧美在线视频一区二区 | 玖玖啪 | 久久久影院 | 一区二区三区精品国产 | 99久久成人国产精品免费 | 99精品国产高清一区二区三区香蕉 | 久草草视频在线观看免费高清 | 亚洲成人综合网站 | a毛片在线还看免费网站 | 亚洲国产精久久久久久久春色 | 亚洲一区二区三区在线视频 | 欧美一级大片免费看 | 色综合久久加勒比高清88 | 欧美一级毛片无遮无挡 | 九九视频在线观看视频23 | 一级a做爰片欧欧美毛片4 | 久久久久久综合一区中文字幕 | 国产a国产片国产 | 国内自拍第一页 | 免费播放国产性色生活片 | 看三级网站 | 99视频精品全国在线观看 | 美国毛片网站 | 精品国产91在线网 |