[tools] Added pil to view image files.

This commit is contained in:
Andrew Hamilton 2017-03-28 22:53:03 +02:00
parent 6edcfbb746
commit e1742f6959
20 changed files with 198 additions and 4 deletions

View file

@ -42,3 +42,4 @@ Extensions | Tools
.tar.gz .tgz | [tar_gz](http://www.gnu.org/software/tar/manual/tar.html)
.tar.bz2 | [tar_bz2](http://www.gnu.org/software/tar/manual/tar.html)
.a .so | [nm](https://linux.die.net/man/1/nm)
.png .jpg .gif .bmp .ppm .tiff .tga | [pil](http://python-pillow.github.io/) • [pil_half](http://python-pillow.github.io/)

View file

@ -401,6 +401,15 @@ class Placeholder:
return self.widget.appearance(dimensions)
class Fixed:
def __init__(self, appearance):
self.appearance_min_ = appearance
def appearance_min(self):
return self.appearance_min_
##########################

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 381 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 B

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

Binary file not shown.

View file

@ -0,0 +1,16 @@
(B
(B (B
(B (B (B (B
(B (B (B
(B (B (B (B
(B (B (B (B (B
(B (B
(B (B
(B (B (B
(B (B (B (B
(B (B (B (B
(B (B (B (B
(B (B (B (B (B
(B (B (B (B (B (B
(B (B (B (B
(B

View file

@ -0,0 +1,16 @@
(B
(B (B
(B (B
(B (B
(B (B
(B (B
(B (B
(B (B
(B (B
(B (B
(B (B
(B (B
(B (B
(B (B
(B

View file

@ -0,0 +1,16 @@
(B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B (B (B (B (B (B (B (B
(B (B (B (B (B (B (B (B (B (B (B (B

View file

@ -0,0 +1,16 @@
(B
(B (B
(B (B (B (B
(B (B (B
(B (B (B (B
(B (B (B (B (B
(B (B
(B (B
(B (B (B
(B (B (B (B
(B (B (B (B
(B (B (B (B
(B (B (B (B (B
(B (B (B (B (B (B
(B (B (B (B
(B

View file

@ -0,0 +1,16 @@
(B
(B (B
(B (B (B (B
(B (B (B
(B (B (B (B
(B (B (B (B (B
(B (B
(B (B
(B (B (B
(B (B (B (B
(B (B (B (B
(B (B (B (B
(B (B (B (B (B
(B (B (B (B (B (B
(B (B (B (B
(B

View file

@ -0,0 +1,16 @@
(B
(B (B
(B (B (B (B
(B (B (B
(B (B (B (B
(B (B (B (B (B
(B (B
(B (B
(B (B (B
(B (B (B (B
(B (B (B (B
(B (B (B (B
(B (B (B (B (B
(B (B (B (B (B (B
(B (B (B (B
(B

View file

@ -0,0 +1,16 @@
(B
(B (B
(B (B (B (B
(B (B (B
(B (B (B (B
(B (B (B (B (B
(B (B
(B (B
(B (B (B
(B (B (B (B
(B (B (B (B
(B (B (B (B
(B (B (B (B (B
(B (B (B (B (B (B
(B (B (B (B
(B

View file

@ -0,0 +1,8 @@
(B▀▀▀▀▀▀(B▀▀▀▀(B▀▀▀▀▀▀(B
(B▀▀▀(B▀(B▀▀(B▀▀▀▀(B▀▀(B▀(B▀▀▀(B
(B▀▀(B▀(B▀▀▀▀▀▀▀▀▀▀(B▀(B▀(B▀(B
(B▀(B▀▀▀▀▀▀▀▀▀▀▀▀▀▀(B▀(B
(B▀(B▀(B▀▀▀▀▀▀▀▀▀▀▀▀(B▀(B▀(B
(B▀(B▀(B▀(B▀▀▀▀▀▀▀▀▀▀(B▀(B▀(B▀(B
(B▀▀▀(B▀(B▀(B▀▀▀▀▀▀(B▀(B▀(B▀(B▀▀(B
(B▀▀▀▀▀(B▀(B▀(B▀▀▀▀(B▀▀▀▀▀(B

View file

@ -3,6 +3,7 @@
# Licensed under the Artistic License 2.0.
import collections
import itertools
import os
import weakref
@ -102,10 +103,7 @@ class CharStyle:
def _join_lists(lists):
result = []
for list_ in lists:
result.extend(list_)
return result
return list(itertools.chain.from_iterable(lists))
class TermStr(collections.UserString):

View file

@ -22,6 +22,7 @@ import tempfile
import time
import traceback
import PIL.Image
import pygments
import pygments.lexers
import pygments.styles
@ -646,6 +647,59 @@ php5_syntax.dependencies = {"php"}
php5_syntax.url = "https://en.wikipedia.org/wiki/PHP"
def _pil_pixels(pil_image):
data = list(pil_image.getdata())
width = pil_image.width
return [data[row_index*width:(row_index+1)*width]
for row_index in range(pil_image.height)]
MAX_IMAGE_SIZE = 80
def _resize_image(image, new_width):
scale = new_width / image.width
return image.resize((int(image.width * scale), int(image.height * scale)),
PIL.Image.ANTIALIAS)
def pil(path):
with open(path, "rb") as image_file:
with PIL.Image.open(image_file).convert("RGB") as image:
if image.width > (MAX_IMAGE_SIZE // 2):
image = _resize_image(image, MAX_IMAGE_SIZE // 2)
text = " " * 2 * image.width
result = []
for row in _pil_pixels(image):
row_style = []
for pixel in row:
style = termstr.CharStyle(bg_color=pixel)
row_style.extend([style, style])
result.append(termstr.TermStr(text, tuple(row_style)))
return Status.normal, fill3.Fixed(result)
pil.dependencies = {"python3-pil"}
pil.url = "python3-pil"
def pil_half(path):
with open(path, "rb") as image_file:
with PIL.Image.open(image_file).convert("RGB") as image:
if image.width > MAX_IMAGE_SIZE:
image = _resize_image(image, MAX_IMAGE_SIZE)
text = "" * image.width
rows = _pil_pixels(image)
if image.height % 2 == 1:
rows.append([None] * image.width)
result = fill3.Fixed([
termstr.TermStr(text, tuple(termstr.CharStyle(
fg_color=top_pixel, bg_color=bottom_pixel)
for top_pixel, bottom_pixel in zip(rows[index], rows[index+1])))
for index in range(0, image.height, 2)])
return Status.normal, result
pil_half.dependencies = {"python3-pil"}
pil_half.url = "python3-pil"
#############################
@ -768,6 +822,9 @@ def _generic_tools():
return [contents, metadata]
IMAGE_EXTENSIONS = ["png", "jpg", "gif", "bmp", "ppm", "tiff", "tga"]
TOOLS_FOR_EXTENSIONS = \
[
(["py"], [python_syntax, python_unittests, pydoc, mypy, python_coverage,
@ -789,6 +846,7 @@ TOOLS_FOR_EXTENSIONS = \
(["tar.gz", "tgz"], [tar_gz]),
(["tar.bz2"], [tar_bz2]),
(["a", "so"], [nm]),
(IMAGE_EXTENSIONS, [pil, pil_half])
]

View file

@ -212,6 +212,14 @@ class ToolsTestCase(unittest.TestCase):
def test_php5_syntax(self):
self._test_tool(tools.php5_syntax, [("root.php", tools.Status.ok)])
def test_pil(self):
for extension in tools.IMAGE_EXTENSIONS:
self._test_tool(tools.pil, [("circle." + extension,
tools.Status.normal)])
def test_pil_half(self):
self._test_tool(tools.pil_half, [("circle.png", tools.Status.normal)])
class LruCacheWithEvictionTestCase(unittest.TestCase):