pipscalez 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python
  2. # $URL: http://pypng.googlecode.com/svn/trunk/code/pipscalez $
  3. # $Rev: 131 $
  4. # pipscalez
  5. # Enlarge an image by an integer factor horizontally and vertically.
  6. def rescale(inp, out, xf, yf):
  7. from array import array
  8. import png
  9. r = png.Reader(file=inp)
  10. _,_,pixels,meta = r.asDirect()
  11. typecode = 'BH'[meta['bitdepth'] > 8]
  12. planes = meta['planes']
  13. # We are going to use meta in the call to Writer, so expand the
  14. # size.
  15. x,y = meta['size']
  16. x *= xf
  17. y *= yf
  18. meta['size'] = (x,y)
  19. del x
  20. del y
  21. # Values per row, target row.
  22. vpr = meta['size'][0] * planes
  23. def iterscale():
  24. for row in pixels:
  25. bigrow = array(typecode, [0]*vpr)
  26. row = array(typecode, row)
  27. for c in range(planes):
  28. channel = row[c::planes]
  29. for i in range(xf):
  30. bigrow[i*planes+c::xf*planes] = channel
  31. for _ in range(yf):
  32. yield bigrow
  33. w = png.Writer(**meta)
  34. w.write(out, iterscale())
  35. def main(argv=None):
  36. import sys
  37. if argv is None:
  38. argv = sys.argv
  39. xf = int(argv[1])
  40. if len(argv) > 2:
  41. yf = int(argv[2])
  42. else:
  43. yf = xf
  44. return rescale(sys.stdin, sys.stdout, xf, yf)
  45. if __name__ == '__main__':
  46. main()