summaryrefslogtreecommitdiffstats
path: root/build/pypng/pipasgrey
diff options
context:
space:
mode:
authorThomas Groman <tgroman@nuegia.net>2020-04-20 20:56:28 -0700
committerThomas Groman <tgroman@nuegia.net>2020-04-20 20:56:28 -0700
commit508d270a4d78d491bbe1c67c309c404f547da58a (patch)
treed16e2a906501dcda7b4d268579896f03e125f553 /build/pypng/pipasgrey
parentf9cab004186edb425a9b88ad649726605080a17c (diff)
downloadwebbrowser-508d270a4d78d491bbe1c67c309c404f547da58a.tar
webbrowser-508d270a4d78d491bbe1c67c309c404f547da58a.tar.gz
webbrowser-508d270a4d78d491bbe1c67c309c404f547da58a.tar.lz
webbrowser-508d270a4d78d491bbe1c67c309c404f547da58a.tar.xz
webbrowser-508d270a4d78d491bbe1c67c309c404f547da58a.zip
added Comm Build System
Diffstat (limited to 'build/pypng/pipasgrey')
-rw-r--r--build/pypng/pipasgrey73
1 files changed, 73 insertions, 0 deletions
diff --git a/build/pypng/pipasgrey b/build/pypng/pipasgrey
new file mode 100644
index 0000000..2b3727f
--- /dev/null
+++ b/build/pypng/pipasgrey
@@ -0,0 +1,73 @@
+#!/usr/bin/env python
+# $URL: http://pypng.googlecode.com/svn/trunk/code/pipasgrey $
+# $Rev: 187 $
+
+# pipasgrey
+
+# Convert image to grey (L, or LA), but only if that involves no colour
+# change.
+
+def asgrey(out, inp, quiet=False):
+ """Convert image to greyscale, but only when no colour change. This
+ works by using the input G channel (green) as the output L channel
+ (luminance) and checking that every pixel is grey as we go. A non-grey
+ pixel will raise an error, but if `quiet` is true then the grey pixel
+ check is suppressed.
+ """
+
+ from array import array
+
+ import png
+
+ r = png.Reader(file=inp)
+ _,_,pixels,info = r.asDirect()
+ if info['greyscale']:
+ w = png.Writer(**info)
+ return w.write(out, pixels)
+ planes = info['planes']
+ targetplanes = planes - 2
+ alpha = info['alpha']
+ width = info['size'][0]
+ typecode = 'BH'[info['bitdepth'] > 8]
+ # Values per target row
+ vpr = width * (targetplanes)
+ def iterasgrey():
+ for i,row in enumerate(pixels):
+ row = array(typecode, row)
+ targetrow = array(typecode, [0]*vpr)
+ # Copy G (and possibly A) channel.
+ green = row[0::planes]
+ if alpha:
+ targetrow[0::2] = green
+ targetrow[1::2] = row[3::4]
+ else:
+ targetrow = green
+ # Check R and B channel match.
+ if not quiet and (
+ green != row[0::planes] or green != row[2::planes]):
+ raise ValueError('Row %i contains non-grey pixel.' % i)
+ yield targetrow
+ info['greyscale'] = True
+ del info['planes']
+ w = png.Writer(**info)
+ w.write(out, iterasgrey())
+
+def main(argv=None):
+ from getopt import getopt
+ import sys
+ if argv is None:
+ argv = sys.argv
+ argv = argv[1:]
+ opt,argv = getopt(argv, 'q')
+ quiet = False
+ for o,v in opt:
+ if o == '-q':
+ quiet = True
+ if len(argv) > 0:
+ f = open(argv[0], 'rb')
+ else:
+ f = sys.stdin
+ return asgrey(sys.stdout, f, quiet)
+
+if __name__ == '__main__':
+ main()