You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1.3 MiB

PIL(low) talk

The "classic" Python Image Library (or PIL) is described in the (classic) handbook: http://www.effbot.org/imagingbook/pil-index.htm

In fact, the current library that people tend to use is called Pillow, but as a "friendly fork" it tries to acts just like the old PIL library, so you don't even notice (and your code still uses the name PIL). The Pillow project also maintains it's own documenation at:

https://pillow.readthedocs.io/en/stable/reference/index.html

In [1]:
from PIL import Image
In [3]:
im = Image.open("a.jpg")
In [4]:
im
Out[4]:
In [6]:
im = Image.open("cyber.jpg")
In [7]:
im.size
Out[7]:
(500, 488)
In [8]:
im.mode
Out[8]:
'RGB'
In [9]:
im.thumbnail( (320, 320) )
In [10]:
im.size
Out[10]:
(320, 312)
In [11]:
im
Out[11]:
In [12]:
from urllib.request import urlopen
In [13]:
f = urlopen("https://upload.wikimedia.org/wikipedia/commons/1/10/NOLAPunchCards1938.jpg")
In [14]:
key2 = Image.open(f)
In [15]:
key2.size
Out[15]:
(600, 458)
In [21]:
key2.convert("1")
Out[21]:
In [18]:
key2.convert?
Signature: key2.convert(mode=None, matrix=None, dither=None, palette=0, colors=256)
Docstring:
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 greyscale (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 values larger than 128 are set to 255 (white),
all other values to 0 (black). To use other thresholds, use the
:py:meth:`~PIL.Image.Image.point` method.

When converting from "RGBA" to "P" without a **matrix** argument,
this passes the operation to :py:meth:`~PIL.Image.Image.quantize`,
and **dither** and **palette** are ignored.

:param mode: The requested mode. See: :ref:`concept-modes`.
:param matrix: An optional conversion matrix.  If given, this
   should be 4- or 12-tuple containing floating point values.
:param 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).
   Note that this is not used when **matrix** is supplied.
:param palette: Palette to use when converting from mode "RGB"
   to "P".  Available palettes are WEB or ADAPTIVE.
:param colors: Number of colors to use for the ADAPTIVE palette.
   Defaults to 256.
:rtype: :py:class:`~PIL.Image.Image`
:returns: An :py:class:`~PIL.Image.Image` object.
File:      ~/.local/lib/python3.7/site-packages/PIL/Image.py
Type:      method
In [19]:
key2.thumbnail((1024, 1024))
In [22]:
key2.save("keypunch.png")
In [36]:
key3 = Image.open( urlopen("https://upload.wikimedia.org/wikipedia/commons/thumb/4/48/IBM26.jpg/1024px-IBM26.jpg") )
In [37]:
key3.size
Out[37]:
(1024, 683)
In [25]:
key3.convert("1", dither=Image.NONE)
Out[25]:
In [35]:
key3
In [38]:
key3.convert("L")
Out[38]:
In [33]:
#key3 = key3.thumbnail((64, 64))
In [39]:
key3 is None
Out[39]:
False
In [40]:
from PIL import ImageDraw
In [41]:
key3.save("keypunch_gray.png")
In [42]:
key2.convert?
Signature: key2.convert(mode=None, matrix=None, dither=None, palette=0, colors=256)
Docstring:
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 greyscale (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 values larger than 128 are set to 255 (white),
all other values to 0 (black). To use other thresholds, use the
:py:meth:`~PIL.Image.Image.point` method.

When converting from "RGBA" to "P" without a **matrix** argument,
this passes the operation to :py:meth:`~PIL.Image.Image.quantize`,
and **dither** and **palette** are ignored.

:param mode: The requested mode. See: :ref:`concept-modes`.
:param matrix: An optional conversion matrix.  If given, this
   should be 4- or 12-tuple containing floating point values.
:param 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).
   Note that this is not used when **matrix** is supplied.
:param palette: Palette to use when converting from mode "RGB"
   to "P".  Available palettes are WEB or ADAPTIVE.
:param colors: Number of colors to use for the ADAPTIVE palette.
   Defaults to 256.
:rtype: :py:class:`~PIL.Image.Image`
:returns: An :py:class:`~PIL.Image.Image` object.
File:      ~/.local/lib/python3.7/site-packages/PIL/Image.py
Type:      method
In [46]:
import aalib
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-46-912cb9847582> in <module>
----> 1 import aalib

ModuleNotFoundError: No module named 'aalib'
In [ ]:
import aalib
In [44]:
screen = aalib.AsciiScreen(width=640, height=480)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-44-f44aedee4649> in <module>
----> 1 screen = aalib.AsciiScreen(width=640, height=480)

NameError: name 'aalib' is not defined
In [47]:
f = urlopen('https://www.python.org/static/favicon.ico')
In [48]:
im = Image.open(f)
In [49]:
im
Out[49]:
In [50]:
im = im.convert('L').resize(screen.virtual_size)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-50-235f2c5973fa> in <module>
----> 1 im = im.convert('L').resize(screen.virtual_size)

NameError: name 'screen' is not defined
In [ ]:
im
In [ ]:
screen.virtual_size
In [ ]:
screen.put_image((0, 0), im)
In [ ]:
print (screen.render())
In [ ]:
resized = key2.convert("L").resize(screen.virtual_size)
screen.put_image((0, 0), resized)
In [ ]:
screen.render()
In [ ]:
resized
In [ ]: