Permalink
Browse files

added PNG support (only save for the moment and very hacky)

decided that the internal storage for pixels should be array.array
(still need to update the current code to actually respect that)
actually managed to load/flip/save a file!
  • Loading branch information...
1 parent 209b7ac commit 4af01571adcfd904665581b43b709be633bc7c13 @ojii committed Jan 24, 2012
View
12 README.rst
@@ -3,3 +3,15 @@ notpil
######
An alternative for the unmaintained PIL.
+
+
+***********
+Development
+***********
+
+Pixels are stored as flat array (``array.array``). This is mostly for
+performance, but also because it seems to be the preferred format for existing
+pure Python image decoders/encoders.
+
+This is why ``Image`` has a ``pixelsize`` attribute. That attribute specifies
+how "long" a pixel is.
View
BIN checked.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN checked_flipped.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
10 notpil/image.py
@@ -12,7 +12,7 @@ def __init__(self, width, height, pixels, mode):
self.pixels = pixels
self.mode = mode
self.pixelsize = self.mode.length
-
+
# hacks
self.palette = None
self.image = self.pixels
@@ -26,14 +26,18 @@ def resize(self, width, height):
target = Image.empty(width, height, self.mode)
incubator_geometry.resize(target, self, incubator_geometry.nearest_filter)
return target
-
+
def flip_vertically(self):
target = Image.empty(self.width, self.height, self.mode)
geometry.flip_vertically(self, target)
return target
-
+
def save(self, fileobj, format):
format_object = get_format(format)
if not format_object:
raise FormatNotSupported(format)
format_object.save(self, fileobj)
+
+ def save_to_path(self, filepath, format):
+ with open(filepath, 'wb') as fobj:
+ self.save(fobj, format)
View
4 notpil/incubator/formats/__init__.py
@@ -1,5 +1,7 @@
from notpil.incubator.formats.gif import GIF
+from notpil.incubator.formats.png import PNG
INCUBATOR_FORMATS = {
- 'gif': GIF
+ 'gif': GIF,
+ 'png': PNG,
}
View
22 notpil/incubator/formats/png.py
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+from notpil.colors import RGBA
+from notpil.incubator.formats.png_raw import Writer
+from itertools import chain
+
+def flat_pixels_iter(pixels):
+ for row in pixels:
+ yield chain(*row)
+
+class PNG:
+ @staticmethod
+ def open(fileobj):
+ raise NotImplementedError()
+
+ @staticmethod
+ def save(image, fileobj):
+ writer = Writer(
+ width=image.width,
+ height=image.height,
+ alpha=image.mode is RGBA
+ )
+ writer.write_packed(fileobj, flat_pixels_iter(image.pixels))
View
3,785 notpil/incubator/formats/png_raw.py
3,785 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.

0 comments on commit 4af0157

Please sign in to comment.