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 相关文章推荐
利用Psyco提升Python运行速度
Dec 24 Python
Python中的深拷贝和浅拷贝详解
Jun 03 Python
python+selenium实现登录账户后自动点击的示例
Dec 22 Python
python实现关键词提取的示例讲解
Apr 28 Python
Caffe均值文件mean.binaryproto转mean.npy的方法
Jul 09 Python
Python实现从SQL型数据库读写dataframe型数据的方法【基于pandas】
Mar 18 Python
详解Python列表赋值复制深拷贝及5种浅拷贝
May 15 Python
Python 余弦相似度与皮尔逊相关系数 计算实例
Dec 23 Python
python几种常用功能实现代码实例
Dec 25 Python
IntelliJ 中配置 Anaconda的过程图解
Jun 01 Python
Python APScheduler执行使用方法详解
Dec 10 Python
python urllib和urllib3知识点总结
Feb 08 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发送AT指令实例代码
2016/05/26 PHP
laravel框架查询数据集转为数组的两种方法
2019/10/10 PHP
修改jQuery.Autocomplete插件 支持中文输入法 避免TAB、ENTER键失效、导致表单提交
2009/10/11 Javascript
js url传值中文乱码之解决之道
2009/11/20 Javascript
js 页面刷新location.reload和location.replace的区别小结
2009/12/24 Javascript
判断浏览器的javascript版本的代码
2010/09/03 Javascript
JS 退出系统并跳转到登录界面的实现代码
2013/06/29 Javascript
浮动的div自适应居中显示的js代码
2013/12/23 Javascript
javascript格式化json显示实例分析
2015/04/21 Javascript
jQuery仿360导航页图标拖动排序效果代码分享
2015/08/24 Javascript
javascript从作用域链谈闭包
2020/07/29 Javascript
在js里怎么实现Xcode里的callFuncN方法(详解)
2016/11/05 Javascript
浅谈JavaScript 代码整洁之道
2018/10/23 Javascript
基于JS实现前端压缩上传图片的实例代码
2019/05/14 Javascript
微信小程序mpvue点击按钮获取button值的方法
2019/05/29 Javascript
vant IndexBar实现的城市列表的示例代码
2019/11/20 Javascript
基于小程序请求接口wx.request封装的类axios请求
2020/07/02 Javascript
Vue执行方法,方法获取data值,设置data值,方法传值操作
2020/08/05 Javascript
Python交换变量
2008/09/06 Python
python mysqldb连接数据库
2009/03/16 Python
EM算法的python实现的方法步骤
2018/01/02 Python
Laravel+Dingo/Api 自定义响应的实现
2019/02/17 Python
python中property属性的介绍及其应用详解
2019/08/29 Python
Python数据分析pandas模块用法实例详解
2019/11/20 Python
Pycharm导入anaconda环境的教程图解
2020/07/31 Python
德国童装购物网站:NICKI´S.com
2018/04/20 全球购物
欧洲著名的二手奢侈品网站:Vestiaire Collective
2020/03/07 全球购物
EJB发布WEB服务一般步骤
2012/10/31 面试题
PHP引擎php.ini参数优化深入讲解
2021/03/24 PHP
质检部职责
2013/12/28 职场文书
教师求职信范文
2014/05/24 职场文书
2014幼儿园教师师德师风演讲稿
2014/09/10 职场文书
结婚典礼主持词
2015/06/29 职场文书
签约仪式致辞
2015/07/30 职场文书
2016教师党员学习心得体会
2016/01/21 职场文书
MySQL去除密码登录告警的方法
2022/04/20 MySQL