python如何在终端里面显示一张图片


Posted in Python onAugust 17, 2016

Linux终端里面可谓是奇妙无限,很多优秀的软件都诞生在终端里面。相较之下,Windows本身的理念和Linux就不一致,所以,你懂得。
下面,我们不妨先思考一下,如何在终端里面显示一张图片?

在终端里面显示,肯定就不像在看图软件里那样的细腻了,我们只是以字符代替某一点的像素,把大致的轮廓显示出来罢了。

编码

既然思路很清晰了,下面就来编码了。

# coding:utf-8
import sys

reload(sys)
sys.setdefaultencoding('utf8')
#  __author__ = '郭 璞'
#  __date__ = '2016/8/4'
#  __Desc__ = 一个可以将图片转换成终端字符形式的小工具

from time import *
import Image
class ImageTool():

  def __init__(self):
    print 'Initialization Completed! @',ctime()

  def getChars(self,image_pixels,image_width,image_height):
    replace_chars = 'ABCDEFGHIJKLMNO '
    terminal_chars = ''
    for h in xrange(image_height):
      for w in xrange(image_width):
        point_pixel = image_pixels[w,h]
        terminal_chars +=replace_chars[int(sum(point_pixel)/3.0/256.0*16)]
      terminal_chars+='\n'
    return terminal_chars

  def formatImage(self,imagename,image_width,image_height):
    img = Image.open(imagename,'rb')
    if img.mode != 'RGB':
      img = img.convert('RGB')
    w,h = img.size
    rw = image_width*1.0/w
    rh = image_height*1.0/h
    r = rw if rw<rh else rh
    rw = int(r*w)
    rh = int(r*h)
    img = img.resize((rw,rh),Image.ANTIALIAS)
    return img

  def entrance(self,image_path,out_width,out_height):
    image = self.formatImage(imagename=image_path,image_width=out_width,image_height=out_height)
    image_pixels = image.load()
    out_width ,out_height = image.size
    terminal_chars = self.getChars(image_pixels=image_pixels,image_width=out_width,image_height=out_height)

if __name__ == "__main__":
  tool = ImageTool()
  imagename = sys.argv[1]
  w = int(sys.argv[2])
  h = int(sys.argv[3])
  tool.entrance(imagename,w,h)

运行

运行程序很简单,我们按照命令行参数来执行即可。

python Image2Chars.py target_image_name output_width output_height

注意,图片名称是包含完整的路径的图片名称

结果
 •素材图片

python如何在终端里面显示一张图片

•终端显示效果

python如何在终端里面显示一张图片

文字类型的看起来还凑活,细腻类型的图片就不太好了。这就是因为我们转换像素的时候的粒度有点大了的缘故。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python Tkinter基础控件用法
Sep 03 Python
利用Python批量压缩png方法实例(支持过滤个别文件与文件夹)
Jul 30 Python
python之virtualenv的简单使用方法(必看篇)
Nov 25 Python
[原创]Python入门教程1. 基本运算【四则运算、变量、math模块等】
Oct 28 Python
python创建文件时去掉非法字符的方法
Oct 31 Python
解决Python下imread,imwrite不支持中文的问题
Dec 05 Python
python3爬虫获取html内容及各属性值的方法
Dec 17 Python
对python pandas读取剪贴板内容的方法详解
Jan 24 Python
Python实现的矩阵转置与矩阵相乘运算示例
Mar 26 Python
使用虚拟环境打包python为exe 文件的方法
Aug 29 Python
Python matplotlib绘制图形实例(包括点,曲线,注释和箭头)
Apr 17 Python
Spring http服务远程调用实现过程解析
Jun 11 Python
动感网页相册 python编写简单文件夹内图片浏览工具
Aug 17 #Python
Python入门教程之运算符与控制流
Aug 17 #Python
python 循环while和for in简单实例
Aug 16 #Python
Python自动化测试ConfigParser模块读写配置文件
Aug 15 #Python
Python自动化测试Eclipse+Pydev 搭建开发环境
Aug 15 #Python
详解python的几种标准输出重定向方式
Aug 15 #Python
如何将python中的List转化成dictionary
Aug 15 #Python
You might like
PHP设置一边执行一边输出结果的代码
2013/09/30 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(三)
2014/06/23 PHP
php 无限级分类,超级简单的无限级分类,支持输出树状图
2014/06/29 PHP
php可应用于面包屑导航的迭代寻找家谱树实现方法
2015/02/02 PHP
windows server 2008/2012安装php iis7 mysql环境搭建教程
2016/06/30 PHP
PHP  实现等比压缩图片尺寸和大小实例代码
2016/10/08 PHP
php 三大特点:封装,继承,多态
2017/02/19 PHP
php封装db类连接sqlite3数据库的方法实例
2017/12/19 PHP
JS 实现Table相同行的单元格自动合并示例代码
2013/08/27 Javascript
javascript实现画不相交的圆
2015/04/07 Javascript
javascript截取字符串小结
2015/04/28 Javascript
vue2.0开发实践总结之入门篇
2016/12/06 Javascript
自带气泡提示的vue校验插件(vue-verify-pop)
2017/04/07 Javascript
vue input输入框关键字筛选检索列表数据展示
2020/10/26 Javascript
微信小程序实现张图片合成为一张并下载
2019/07/16 Javascript
JS模拟浏览器实现全局搜索功能
2019/09/11 Javascript
深入理解javascript prototype的相关知识
2019/09/19 Javascript
[03:07]DOTA2英雄基础教程 冰霜诅咒极寒幽魂
2013/12/06 DOTA
[02:19]DOTA选手解说齐贺岁
2018/02/11 DOTA
Python字符串格式化
2015/06/15 Python
Python实现小数转化为百分数的格式化输出方法示例
2017/09/20 Python
python re模块findall()函数实例解析
2018/01/19 Python
让Django支持Sql Server作后端数据库的方法
2018/05/29 Python
python调用pyaudio使用麦克风录制wav声音文件的教程
2019/06/26 Python
对Django url的几种使用方式详解
2019/08/06 Python
Python如何使用字符打印照片
2020/01/03 Python
Python如何爬取qq音乐歌词到本地
2020/06/01 Python
Pycharm编辑器功能之代码折叠效果的实现代码
2020/10/15 Python
Javascript 高级手势使用介绍
2013/04/21 HTML / CSS
美丽的珠宝配饰:SmallThings
2019/09/04 全球购物
数据管理员的自我评价分享
2013/11/15 职场文书
军训心得体会
2013/12/31 职场文书
药品采购员岗位职责
2014/02/08 职场文书
打架赔偿协议书范本
2014/10/26 职场文书
2015年个人实习工作总结
2014/12/12 职场文书
Python中X[:,0]和X[:,1]的用法
2021/05/10 Python