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实现简单的相似图片搜索的教程
Apr 23 Python
python3实现暴力穷举博客园密码
Jun 19 Python
python Pygame的具体使用讲解
Nov 03 Python
python通过微信发送邮件实现电脑关机
Jun 20 Python
python中退出多层循环的方法
Nov 27 Python
详解python3中用HTMLTestRunner.py报ImportError: No module named 'StringIO'如何解决
Aug 27 Python
Django使用uwsgi部署时的配置以及django日志文件的处理方法
Aug 30 Python
Python测试线程应用程序过程解析
Dec 31 Python
通过python调用adb命令对App进行性能测试方式
Apr 23 Python
详解Django配置JWT认证方式
May 09 Python
在python中list作函数形参,防止被实参修改的实现方法
Jun 05 Python
Python列表的索引与切片
Apr 07 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 获取全局变量的代码
2011/04/21 PHP
关于js与php互相传值的介绍
2013/06/25 PHP
关于使用key/value数据库redis和TTSERVER的心得体会
2013/06/28 PHP
php几个预定义变量$_SERVER用法小结
2014/11/07 PHP
PHP使用递归生成文章树
2015/04/21 PHP
PHP 用session与gd库实现简单验证码生成与验证的类方法
2016/11/15 PHP
php使用Jpgraph创建折线图效果示例
2017/02/15 PHP
2017年最新PHP经典面试题目汇总(上篇)
2017/03/17 PHP
thinkPHP5.1框架使用SemanticUI实现分页功能示例
2019/08/03 PHP
Jquery实现视频播放页面的关灯开灯效果
2013/05/27 Javascript
JS利用cookie记忆当前位置的防刷新导航效果
2015/10/15 Javascript
node+koa实现数据mock接口的方法
2017/09/20 Javascript
node.js学习之断言assert的使用示例
2017/09/28 Javascript
vue中如何动态绑定图片,vue中通过data返回图片路径的方法
2018/02/07 Javascript
ES6中Set和Map数据结构,Map与其它数据结构互相转换操作实例详解
2019/02/28 Javascript
vue 中 beforeRouteEnter 死循环的问题
2019/04/23 Javascript
微信小程序跳转到其他网页(外部链接)的实现方法
2019/09/20 Javascript
使用p5.js实现动态GIF图片临摹重现
2019/10/23 Javascript
vue 检测用户上传图片宽高的方法
2020/02/06 Javascript
js实现时间日期校验
2020/05/26 Javascript
js验证账户名是否重复
2020/05/26 Javascript
用实例说明python的*args和**kwargs用法
2013/11/01 Python
Python减少循环层次和缩进的技巧分析
2016/03/15 Python
python 获取指定文件夹下所有文件名称并写入列表的实例
2018/04/23 Python
python简单贪吃蛇开发
2019/01/28 Python
Python字符串逆序输出的实例讲解
2019/02/16 Python
详解python中docx库的安装过程
2019/11/08 Python
Python 排序最长英文单词链(列表中前一个单词末字母是下一个单词的首字母)
2020/12/14 Python
详解pandas映射与数据转换
2021/01/22 Python
css3 实现元素弧线运动的示例代码
2020/04/24 HTML / CSS
Nice Kicks网上商店:ShopNiceKicks.com
2018/12/25 全球购物
一句话工作感言
2014/03/01 职场文书
个人贷款担保书
2014/04/01 职场文书
求职简历自荐信怎么写
2015/03/26 职场文书
简单聊聊TypeScript只读修饰符
2022/04/06 Javascript
MySQL的表级锁,行级锁,排它锁和共享锁
2022/07/15 MySQL