python如何將圖片轉(zhuǎn)換為字符圖片
更新時(shí)間:2020年08月19日 15:19:00 作者:clayanddev
這篇文章主要為大家詳細(xì)介紹了python將圖片轉(zhuǎn)換為字符圖片的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
簡介
一個(gè)簡單的python程序,將圖片轉(zhuǎn)換為字符圖片。
(為了簡便,很多參數(shù)寫死了,自己看著改吧。 (←∀←))
正文
原圖(侵刪)

結(jié)果圖

源碼
[更多細(xì)節(jié)]——>戳這里
#-*- coding: UTF-8 -*-
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import matplotlib.pyplot as plt
import numpy as np
import time
def happyNewYear(srd_img_file_path, dst_img_file_path = None, scale = 2, sample_step = 3):
start_time = int(time.time())
#讀取圖片信息
old_img = Image.open(srd_img_file_path)
pix = old_img.load()
width = old_img.size[0]
height = old_img.size[1]
print ("width:%d, height:%d" % (width, height))
#創(chuàng)建新圖片
canvas = np.ndarray((height*scale, width*scale, 3), np.uint8)
canvas[:, :, :] = 255
new_image = Image.fromarray(canvas)
draw = ImageDraw.Draw(new_image)
#創(chuàng)建繪制對象
font = ImageFont.truetype("consola.ttf", 10, encoding="unic")
char_table = list('happy new year ')
# font = ImageFont.truetype('simsun.ttc', 10)
# char_table = list(u'新年快樂')
#開始繪制
pix_count = 0
table_len = len(char_table)
for y in range(height):
for x in range(width):
if x % sample_step == 0 and y % sample_step == 0:
draw.text((x*scale, y*scale), char_table[pix_count % table_len], pix[x, y], font)
pix_count += 1
# 保存
if dst_img_file_path is not None:
new_image.save(dst_img_file_path)
print("used time : %d second, pix_count : %d" % ((int(time.time()) - start_time), pix_count))
print(pix_count)
new_image.show()
happyNewYear("input.jpg", "output.jpg")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python如何將圖片轉(zhuǎn)換素描畫
- python實(shí)現(xiàn)圖片轉(zhuǎn)換成素描和漫畫格式
- python 將dicom圖片轉(zhuǎn)換成jpg圖片的實(shí)例
- 基于python實(shí)現(xiàn)把圖片轉(zhuǎn)換成素描
- python3用PIL把圖片轉(zhuǎn)換為RGB圖片的實(shí)例
- 利用python和ffmpeg 批量將其他圖片轉(zhuǎn)換為.yuv格式的方法
- Python圖片轉(zhuǎn)換成矩陣,矩陣數(shù)據(jù)轉(zhuǎn)換成圖片的實(shí)例
- Python將圖片轉(zhuǎn)換為字符畫的方法
- python3圖片轉(zhuǎn)換二進(jìn)制存入mysql
- python將YUV420P文件轉(zhuǎn)PNG圖片格式的兩種方法
相關(guān)文章
使用python把xmind轉(zhuǎn)換成excel測試用例的實(shí)現(xiàn)代碼
這篇文章主要介紹了使用python把xmind轉(zhuǎn)換成excel測試用例的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
淺談Pytorch中的自動(dòng)求導(dǎo)函數(shù)backward()所需參數(shù)的含義
今天小編就為大家分享一篇淺談Pytorch中的自動(dòng)求導(dǎo)函數(shù)backward()所需參數(shù)的含義,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Pandas?DataFrame添加一行數(shù)據(jù)的幾種方法
在處理數(shù)據(jù)分析和數(shù)據(jù)科學(xué)項(xiàng)目時(shí),經(jīng)常會(huì)使用到Python中的pandas庫來進(jìn)行數(shù)據(jù)操作和分析,其中DataFrame是pandas庫中最重要的數(shù)據(jù)結(jié)構(gòu)之一,這篇文章主要給大家介紹了關(guān)于Pandas?DataFrame添加一行數(shù)據(jù)的幾種方法,需要的朋友可以參考下2024-08-08
Python 讀取.shp文件并生成圖幅編號(hào)的實(shí)現(xiàn)思路
這篇文章主要介紹了Python 讀取.shp文件并生成圖幅編號(hào),代碼適用于需要處理和分析地理空間數(shù)據(jù)的場景,如城市規(guī)劃、環(huán)境監(jiān)測或自然資源管理,其中它可以幫助用戶讀取特定區(qū)域的Shapefile文件,確定其地理邊界,需要的朋友可以參考下2024-05-05

