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中利用原始套接字进行网络编程的示例
May 04 Python
python机器学习实战之K均值聚类
Dec 20 Python
python操作oracle的完整教程分享
Jan 30 Python
python语音识别实践之百度语音API
Aug 30 Python
python3+opencv3识别图片中的物体并截取的方法
Dec 05 Python
python输出带颜色字体实例方法
Sep 01 Python
解决Tensorflow 使用时cpu编译不支持警告的问题
Feb 03 Python
Matplotlib使用字符串代替变量绘制散点图的方法
Feb 17 Python
Python如何根据时间序列数据作图
May 12 Python
python爬虫泛滥的解决方法详解
Nov 25 Python
20行代码教你用python给证件照换底色的方法示例
Feb 05 Python
Python编程super应用场景及示例解析
Oct 05 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 之 写时复制介绍(Copy On Write)
2014/05/13 PHP
php使用自定义函数实现汉字分割替换功能示例
2017/01/30 PHP
一个用js实现的页内搜索代码
2007/05/23 Javascript
基于jQuery的仿flash的广告轮播
2010/11/05 Javascript
JavaScript计算两个日期时间段内日期的方法
2015/03/16 Javascript
举例讲解AngularJS中的模块
2015/06/17 Javascript
再JavaScript的jQuery库中编写动画效果的指南
2015/08/13 Javascript
仿Angular Bootstrap TimePicker创建分钟数-秒数的输入控件
2016/07/01 Javascript
深入分析javascript中console命令
2016/08/14 Javascript
jquery日历插件e-calendar升级版
2016/11/10 Javascript
妙用Angularjs实现表格按指定列排序
2017/06/23 Javascript
详解webpack3如何正确引用并使用jQuery库
2017/08/26 jQuery
利用vscode调试编译后的js代码详解
2018/05/14 Javascript
Vue 子组件与数据传递问题及注意事项
2019/07/11 Javascript
JS常用排序方法实例代码解析
2020/03/03 Javascript
Python安装Imaging报错:The _imaging C module is not installed问题解决方法
2014/08/22 Python
Python 实现文件的全备份和差异备份详解
2016/12/27 Python
python3利用Dlib19.7实现人脸68个特征点标定
2018/02/26 Python
Python基于Flask框架配置依赖包信息的项目迁移部署
2018/03/02 Python
Python实现删除时保留特定文件夹和文件的示例
2018/04/27 Python
pandas 小数位数 精度的处理方法
2018/06/09 Python
Python实现的朴素贝叶斯算法经典示例【测试可用】
2018/06/13 Python
基于Python实现拆分和合并GIF动态图
2019/10/22 Python
基于Pycharm加载多个项目过程图解
2020/01/19 Python
使用python的turtle函数绘制一个滑稽表情
2020/02/28 Python
NEW LOOK官网:英国时装零售巨头之一,快时尚品牌
2017/01/11 全球购物
美国在线购买空气净化器、除湿器、加湿器网站:AllergyBuyersClub
2021/03/16 全球购物
五型班组建设方案
2014/02/10 职场文书
中药学自荐信
2014/06/15 职场文书
在职员工证明书
2014/09/19 职场文书
停车位租赁协议书
2014/09/24 职场文书
入党政审材料范文
2014/12/24 职场文书
2016年大学生就业指导课心得体会
2015/10/09 职场文书
百年校庆宣传标语口号
2015/12/26 职场文书
Python 数据结构之十大经典排序算法一文通关
2021/10/16 Python
Redis安装使用RedisJSON模块的方法
2022/03/23 Redis