通过python将图片生成字符画
基础知识:
1.python基础知识 快速学习链接:https://www.shiyanlou.com/courses/214
2.linux命令行操作 快速学习链接:https://www.shiyanlou.com/courses/1
3.pillow库的使用 快速学习链接:http://pillow.readthedocs.io/en/latest/index.html(英文) http://www.cnblogs.com/apexchu/p/4231041.html(中文)
4.argparse库的使用 快速学习链接:http://blog.ixxoo.me/argparse.html
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pillow库:
导入Image类: from PIL import Image
在python解释器下
>>>from PIL import Image
>>>help(Image)
查看帮助文档
Image的文件位置 /usr/lib64/python2.7/site-packages/PIL/Image.py
使用Image类
To load an image from a file, use the open() function in the Image module:
从文件里加载图片,使用Image模块里的open()函数:
>>> from PIL import Image
>>> im = Image.open("lena.ppm")
If successful, this function returns an Image object. 如果成功,这个函数返回一个Image对象。
If the file cannot be opened, an IOError exception is raised. 如果失败,引发一个IOError.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PIL.Image.open(fp, mode='r')-
Opens and identifies the given image file.
This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the
load()method). Seenew().Parameters: - fp – A filename (string), pathlib.Path object or a file object. The file object must implement
read(),seek(), andtell()methods, and be opened in binary mode. - mode – The mode. If given, this argument must be “r”.
Returns: An
Imageobject.Raises: IOError – If the file cannot be found, or the image cannot be opened and identified.
- fp – A filename (string), pathlib.Path object or a file object. The file object must implement
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Image.getpixel(xy)-
Returns the pixel value at a given position.
Parameters: xy – The coordinate, given as (x, y). Returns: The pixel value. If the image is a multi-layer image, this method returns a tuple.
通过给的位置值返回像素值。
>>> from PIL import Image
>>> im = Image.open('cat.jpg')
>>> im.getpixel((1,2))
(107, 81, 22) 返回了该坐标对应的rgb像素三元祖
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Image.resize(size, resample=0)-
Returns a resized copy of this image.
Parameters: - size – The requested size in pixels, as a 2-tuple: (width, height).
- resample – An optional resampling filter. This can be one of
PIL.Image.NEAREST,PIL.Image.BOX,PIL.Image.BILINEAR,PIL.Image.HAMMING,PIL.Image.BICUBICorPIL.Image.LANCZOS. If omitted, or if the image has mode “1” or “P”, it is setPIL.Image.NEAREST. See: Filters.
Returns: An
Imageobject.
重设图片的大小,返回一个Image对象。
5种resample: PIL.Image.NEAREST, PIL.Image.BOX, PIL.Image.BILINEAR, PIL.Image.HAMMING, PIL.Image.BICUBIC or PIL.Image.LANCZOS
>>> im = Image.open(IMG)
>>> im = im.resize((WIDTH,HEIGHT), Image.NEAREST)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
NEAREST- Pick one nearest pixel from the input image. Ignore all other input pixels.
BOX-
Each pixel of source image contributes to one pixel of the destination image with identical weights. For upscaling is equivalent of
NEAREST. This filter can only be used with theresize()andthumbnail()methods.New in version 3.4.0.
BILINEAR- For resize calculate the output pixel value using linear interpolation on all pixels that may contribute to the output value. For other transformations linear interpolation over a 2x2 environment in the input image is used.
HAMMING-
Produces more sharp image than
BILINEAR, doesn’t have dislocations on local level like withBOX. This filter can only be used with theresize()andthumbnail()methods.New in version 3.4.0.
BICUBIC- For resize calculate the output pixel value using cubic interpolation on all pixels that may contribute to the output value. For other transformations cubic interpolation over a 4x4 environment in the input image is used.
LANCZOS-
Calculate the output pixel value using a high-quality Lanczos filter (a truncated sinc) on all pixels that may contribute to the output value. This filter can only be used with the
resize()andthumbnail()methods.New in version 1.1.3.
Filters comparison table
| Filter | Downscaling quality | Upscaling quality | Performance |
|---|---|---|---|
NEAREST |
⭐⭐⭐⭐⭐ | ||
BOX |
⭐ | ⭐⭐⭐⭐ | |
BILINEAR |
⭐ | ⭐ | ⭐⭐⭐ |
HAMMING |
⭐⭐ | ⭐⭐⭐ | |
BICUBIC |
⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐ |
LANCZOS |
⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐ |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Image.load()-
Allocates storage for the image and loads the pixel data. In normal cases, you don’t need to call this method, since the Image class automatically loads an opened image when it is accessed for the first time. This method will close the file associated with the image.
Returns: An image access object. Return type: PixelAccess Class or PIL.PyAccess
>>> im = im.load()
>>> im
<PixelAccess object at 0x7f20ebdeb990>
>>> print im[1,2]
(107, 81, 22)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Image.convert(mode=None, matrix=None, dither=None, palette=0, colors=256)-
Returns a converted copy of this image. For the “P” mode, this method translates pixels through the palette. If mode is omitted, a mode is chosen so that all information in the image and the palette can be represented without a palette.
The current version supports all possible conversions between “L”, “RGB” and “CMYK.” The matrix argument only supports “L” and “RGB”.
When translating a color image to black and white (mode “L”), the library uses the ITU-R 601-2 luma transform:
L = R * 299/1000 + G * 587/1000 + B * 114/1000
The default method of converting a greyscale (“L”) or “RGB” image into a bilevel (mode “1”) image uses Floyd-Steinberg dither to approximate the original image luminosity levels. If dither is NONE, all non-zero values are set to 255 (white). To use other thresholds, use the
point()method.Parameters: - mode – The requested mode. See: Modes.
- matrix – An optional conversion matrix. If given, this should be 4- or 12-tuple containing floating point values.
- dither – Dithering method, used when converting from mode “RGB” to “P” or from “RGB” or “L” to “1”. Available methods are NONE or FLOYDSTEINBERG (default).
- palette – Palette to use when converting from mode “RGB” to “P”. Available palettes are WEB or ADAPTIVE.
- colors – Number of colors to use for the ADAPTIVE palette. Defaults to 256.
Return type: Returns: An
Imageobject.
The following example converts an RGB image (linearly calibrated according to ITU-R 709, using the D65 luminant) to the CIE XYZ color space:
rgb2xyz = (
0.412453, 0.357580, 0.180423, 0,
0.212671, 0.715160, 0.072169, 0,
0.019334, 0.119193, 0.950227, 0 )
out = im.convert("RGB", rgb2xyz)
-------------------------------------------------------------------------------------------------------------------------------------------
通过python将图片生成字符画的更多相关文章
- python识别图片生成字符模式
此python文件来自D7哥, 放在这里备份. 用法 python3 PIL\&argparse.py 1.jpg -o test.txt --width 300 --height 300 p ...
- 20个python项目--图片转字符画
转自实验楼:https://www.shiyanlou.com/courses/370/learning/?id=1191 代码: # -*- coding:utf-8 -*- from PIL im ...
- Python 【图片转字符画】
一.安装的第三方模块 $ sudo pip3 install --upgrade pip $ sudo pip3 install pillow //window pip3 install pillow ...
- Python实现图片转字符画
from PIL import Image def get_char(r, g, b, alpha=256): ascii_char = '''$@B%8&WM#*oahkbdpqwmZO0Q ...
- Python 图片转字符画
Python 图片转字符画 一.课程介绍 1. 课程来源 原创 2. 内容简介 本课程讲述怎样使用 Python 将图片转为字符画 3. 前置课程 Python编程语言 Linux 基础入门(新版) ...
- python生成字符画
python生成字符画 这个idea来自于实验楼,非常适合练习PIL的像素处理,更重要的是非常有意思. 环境配置 依赖的第三方库就是PIL(Python Image Library),可以直接使用pi ...
- [笔记] Python 图片转字符画
一.介绍 用Python 代码完成图片转字符画 二.python 环境 Python 3.6.6 pillow 5.1.0 Python 图像处理库, 需要另外安装 三.原理 gray = 0.21 ...
- python学习---50行代码实现图片转字符画2
from PIL import Image codeLib = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<> ...
- python小项目(-)图片转字符画
# -*- coding: utf-8 -*- from PIL import Image codeLib = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrj ...
随机推荐
- 爱上MVC3~MVC+ZTree实现对树的CURD及拖拽操作
回到目录 上一讲中,我们学习了如何使用zTree对一棵大树(大数据量的树型结构的数据表,呵呵,名称有点绕,但说的是事实)进行异步加载,今天这讲,我们来说说,如何去操作这棵大树,无非就是添加子节点,删除 ...
- fir.im Weekly - 除了写代码,还需要了解什么
雾霾天,宜撸代码.吹牛,不宜出门约会(¬_¬)ノ 本期 fir.im Weekly 亦如往期,收集了优秀的 iOS/Android 开发资源,GitHub 源码.前端方面的热点分享.除了代码,也许你 ...
- Android 神兵利器—— Git 常用命令
总结的Android 工具类文章: Android 神兵利器-- Adb 常用命令 Android 神兵利器-- Git 常用命令 在项目研发时,经常使用Git,基本的命令有六个,通过下面的图片我们可 ...
- KnockoutJS 3.X API 第四章 数据绑定(3) 控制流if绑定和ifnot绑定
if绑定目的 if绑定一般是格式是data-bind=if:attribute,if后所跟属性或表达式的值应为bool值(也可以是非bool值,当非空字符串时则为真),if绑定的作用与visible绑 ...
- KendoUI系列:ComboBox
1.基本使用 1>.创建Input <input id="color" placeholder="Select Color..." /> &l ...
- 【博客美化】05.添加GitHub链接
博客园美化相关文章目录: [博客美化]01.推荐和反对炫酷样式 [博客美化]02.公告栏显示个性化时间 [博客美化]03.分享按钮 [博客美化]04.自定义地址栏logo [博客美化]05.添加Git ...
- Linux下如何删除Oracle
一. 停止Oracle数据库服务 shutdown immediate 二. 停止监听服务 lsnrctl stop 三. 用dbca卸载数据库实例 四. 删除相关文件 -->> 如果只 ...
- Kruskal算法(二)之 C++详解
本章是克鲁斯卡尔算法的C++实现. 目录 1. 最小生成树 2. 克鲁斯卡尔算法介绍 3. 克鲁斯卡尔算法图解 4. 克鲁斯卡尔算法分析 5. 克鲁斯卡尔算法的代码说明 6. 克鲁斯卡尔算法的源码 转 ...
- (转)JS模块化编程之AMD规范
模块的规范 原文地址 先想一想,为什么模块很重要? 因为有了模块,我们就可以更方便地使用别人的代码,想要什么功能,就加载什么模块. 但是,这样做有一个前提,那就是大家必须以同样的方式编写模块,否则你有 ...
- 轻松自动化---selenium-webdriver(python) (六)
本节知识点: 操作对象: · click 点击对象 · send_keys 在对象上模拟按键输入 · clear 清除对象的内容,如果可以的话 WebElement 另一些常用方法: · text ...