經過python將圖片生成字符畫

基礎知識:html

1.python基礎知識   快速學習連接:https://www.shiyanlou.com/courses/214python

2.linux命令行操做   快速學習連接:https://www.shiyanlou.com/courses/1linux

3.pillow庫的使用     快速學習連接:http://pillow.readthedocs.io/en/latest/index.html(英文) http://www.cnblogs.com/apexchu/p/4231041.html(中文)app

4.argparse庫的使用 快速學習連接:http://blog.ixxoo.me/argparse.htmlpython2.7

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ide

pillow庫:函數

導入Image類:     from PIL import Image學習

  在python解釋器下ui

  >>>from PIL import Imagethis

       >>>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). See new().

Parameters:
  • fp – A filename (string), pathlib.Path object or a file object. The file object must implement read(), seek(), and tell() methods, and be opened in binary mode.
  • mode – The mode. If given, this argument must be 「r」.
Returns:

An Image object.

Raises:

IOError – If the file cannot be found, or the image cannot be opened and identified.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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.BICUBIC or PIL.Image.LANCZOS. If omitted, or if the image has mode 「1」 or 「P」, it is set PIL.Image.NEAREST. See: Filters.
Returns:

An Image object.

重設圖片的大小,返回一個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 the resize() and thumbnail() 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 with BOX. This filter can only be used with the resize() and thumbnail() 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() and thumbnail() 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:

Image

Returns:

An Image object.

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) 

-------------------------------------------------------------------------------------------------------------------------------------------

相關文章
相關標籤/搜索