Python图片转字符画
PIL安装很麻烦,推荐下载exe直接安装
PIL官网:http://pythonware.com/products/pil/
但现在里面下载链接访问不了,我把32位和64位版本上传到博客园以供下载
PILwin32:http://files.cnblogs.com/files/pcat/PILwin32.zip
PILwin64:http://files.cnblogs.com/files/pcat/PILwin64.zip
#coding=utf-8
from PIL.Image import Image
import argparse #命令行输入参数处理
parser=argparse.ArgumentParser() parser.add_argument('file') #输入文件
parser.add_argument('-o','--output')
parser.add_argument('--width',type=int,default=80)
parser.add_argument('--heigth',type=int,default=80) args=parser.parse_args() IMG=args.file
WIDTH=args.width
HEIGHT=args.heigth
OUTPUT=args.output ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ") #将256灰度映射到70个字符上 def get_char(r,g,b,alpha=256):
if alpha==0:
return ' '
length=len(ascii_char)
gray=int(0.2126 * r + 0.7152 * g + 0.0722 * b) unit=(256.0+1)/length
return ascii_char[int(gray/unit)] im = Image.open(IMG)
im = im.resize((WIDTH,HEIGHT), Image.NEAREST) txt="" for i in range(HEIGHT):
for j in range(WIDTH):
txt+=get_char(*im.getpixel((j,i))) print txt #字符画输出文件
if OUTPUT:
with open(OUTPUT,'w') as f:
f.write(txt)
else:
with open("output.txt",'w') as f:
f.write(txt) 链接:https://www.shiyanlou.com/courses/370/labs/1191/document
Python图片转字符画的更多相关文章
- Python 图片转字符画
Python 图片转字符画 一.课程介绍 1. 课程来源 原创 2. 内容简介 本课程讲述怎样使用 Python 将图片转为字符画 3. 前置课程 Python编程语言 Linux 基础入门(新版) ...
- Python 图片转字符画 学习笔记
Python 图片转字符画 学习笔记 标签(空格分隔): Python 声明:此文章和所有代码是学习笔记,非原创,原文教程地址:https://www.shiyanlou.com/courses/37 ...
- [笔记] Python 图片转字符画
一.介绍 用Python 代码完成图片转字符画 二.python 环境 Python 3.6.6 pillow 5.1.0 Python 图像处理库, 需要另外安装 三.原理 gray = 0.21 ...
- python图片转字符画(转)
先上代码: from PIL import Image import argparse #命令行输入参数处理 parser = argparse.ArgumentParser() parser.add ...
- 教程,Python图片转字符堆叠图
Python 图片转字符画 一.实验说明 1. 环境登录 无需密码自动登录, 2. 环境介绍 本实验环境采用带桌面的UbuntuLinux环境,实验中会用到桌面上的程序: LX终端(LXTermina ...
- 使用Python生成ASCII字符画
使用Python生成ASCII字符画 在很多的网站主页中或者程序的注释中会有一些好看的字符注释画.显得很牛逼的样子 例如: 知乎 _____ _____ _____ _____ /\ \ /\ \ / ...
- python学习---50行代码实现图片转字符画2
from PIL import Image codeLib = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<> ...
- python学习---50行代码实现图片转字符画1
转自:https://blog.csdn.net/mm1030533738/article/details/78447714 项目链接: https://www.shiyanlou.com/cours ...
- 20个python项目--图片转字符画
转自实验楼:https://www.shiyanlou.com/courses/370/learning/?id=1191 代码: # -*- coding:utf-8 -*- from PIL im ...
随机推荐
- 在Hive中使用Avro
作者:过往记忆 | 新浪微博:左手牵右手TEL | 可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明博客地址:http://www.iteblog.com/文章标题:<在Hiv ...
- Struts06---通配符的使用
01.创建对应的login.jsp页面 <%@ page language="java" import="java.util.*" pageEncodin ...
- LeetCode OJ:Product of Array Except Self(除己之外的元素乘积)
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...
- json 和 table控件
<!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content= ...
- 8.var目录下的文件和目录详解
1./var目录下的文件和目录详解. /var (该目录存放的是不断扩充且经常修改的目录,包括各种日志文件或者pid文件,存放linux的启动日志和正在运行的程序目录(变化的目录:一般是日志文件,ca ...
- MySQL 进入 导入
命令行进入时 不能用 ‘;’ 结尾
- PHP 去掉文文文件中的回车与空格
文本文件fff.txt中去除回车与空格: $aa = file_get_contents('./fff.txt'); $bb = str_replace(array("\r\n", ...
- POJ1797 Heavy Transportation
解题思路:典型的Kruskal,不能用floyed(会超时),上代码: #include<cstdio> #include<cstring> #include<algor ...
- bzoj 1500 维修序列
Written with StackEdit. Description 请写一个程序,要求维护一个数列,支持以下 \(6\) 种操作: 请注意,格式栏 中的下划线' _ '表示实际输入文件中的空格 I ...
- c#和c++互操作(平台调用相关)
[DllImport("ScreenCaptureLib.dll", CallingConvention = CallingConvention.Cdecl)] public st ...