home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / img / test / testimg.py < prev    next >
Encoding:
Text File  |  2000-06-23  |  7.8 KB  |  334 lines

  1. #
  2. # Test script to test various image modules.
  3. #
  4. import sys
  5. sys.path.append('..')
  6.  
  7. import imgformat
  8. import imgppm
  9. import imgpgm
  10. import imgpbm
  11. import imgtiff
  12. try:
  13.     import imgsgi
  14. except ImportError:
  15.     imgsgi = None
  16. import imgjpeg
  17. import imggif
  18.  
  19. warning = 0
  20.  
  21. def checksize(r):
  22.     if (r.width, r.height) != (width, height):
  23.     print '** W/H change: wanted', (width,height), 'got', (r.width, r.height)
  24.     sys.exit(1)
  25.  
  26. def checkreader(r, wtddata):
  27.     global warning
  28.     checksize(r)
  29.     d = r.read()
  30.     if len(d) <> len(wtddata):
  31.     print '** Datalength change: wanted',len(wtddata),'got',len(d)
  32.     sys.exit(1)
  33.     if d <> wtddata:
  34.     print '** (warning) data changed'
  35.     warning = 1
  36.     for i in range(len(d)):
  37.         if d[i] <> wtddata[i]:
  38.         print '   First at byte', i
  39.         print `wtddata[i:i+4]`, `d[i:i+4]`
  40.         break
  41. #
  42. # Part one - Test ppm reader/writer
  43. #
  44. print '- Read ppm original image'
  45. r = imgppm.reader('in-rgb-t2b.ppm')
  46. width, height = r.width, r.height
  47. rgb_t2b_data = r.read()
  48. del r
  49.  
  50. print '- Write ppm original image'
  51. w = imgppm.writer('out-rgb-t2b.ppm')
  52. w.width, w.height, w.format = width, height, imgformat.rgb
  53. w.write(rgb_t2b_data)
  54. del w
  55.  
  56. print '- Write ppm upsidedown image'
  57. w = imgppm.writer('out-rgb-b2t.ppm')
  58. w.width, w.height, w.format = width, height, imgformat.rgb_b2t
  59. w.write(rgb_t2b_data)
  60. del w
  61.  
  62. print '- Read ppm upsidedown image'
  63. r = imgppm.reader('out-rgb-b2t.ppm')
  64. checksize(r)
  65. rgb_b2t_data = r.read()
  66. del r
  67.  
  68. #
  69. # part 2 - test pgm module
  70. #
  71.  
  72. print '- Read pgm original image'
  73. r = imgpgm.reader('in-grey-t2b.pgm')
  74. checksize(r)
  75. grey_t2b_data = r.read()
  76. del r
  77.  
  78. print '- Write pgm original image'
  79. w = imgpgm.writer('out-grey-t2b.pgm')
  80. w.width, w.height, w.format = width, height, imgformat.grey
  81. w.write(grey_t2b_data)
  82. del w
  83.  
  84. print '- Write pgm upsidedown image'
  85. w = imgpgm.writer('out-grey-b2t.pgm')
  86. w.width, w.height, w.format = width, height, imgformat.grey_b2t
  87. w.write(grey_t2b_data)
  88. del w
  89.  
  90. print '- Read pgm upsidedown image'
  91. r = imgpgm.reader('out-grey-b2t.pgm')
  92. checksize(r)
  93. grey_b2t_data = r.read()
  94. del r
  95.  
  96. #
  97. # step 3 - Test sgi module
  98. #
  99. if imgsgi:
  100.     print '- Write sgi rgb image'
  101.     w = imgsgi.writer('out-rgb-t2b.rgb')
  102.     w.width, w.height, w.format = width, height, imgformat.rgb
  103.     w.write(rgb_t2b_data)
  104.     del w
  105.     
  106.     print '-  (checking)'
  107.     r = imgsgi.reader('out-rgb-t2b.rgb')
  108.     checkreader(r, rgb_t2b_data)
  109.     del r
  110.     
  111.     print '- Write sgi rgb image upsidedown'
  112.     w = imgsgi.writer('out-rgb-b2t.rgb')
  113.     w.width, w.height, w.format = width, height, imgformat.rgb
  114.     w.write(rgb_b2t_data)
  115.     del w
  116.     
  117.     print '-  (checking)'
  118.     r = imgsgi.reader('out-rgb-b2t.rgb')
  119.     r.format = imgformat.rgb_b2t
  120.     checkreader(r, rgb_t2b_data)
  121.     del r
  122.     
  123.     print '- Write sgi grey image (cannot check)'
  124.     w = imgsgi.writer('out-grey-t2b.rgb')
  125.     w.width, w.height, w.file_format = width, height, imgformat.grey
  126.     w.write(rgb_t2b_data)
  127.     del w
  128.     
  129.     print '- Write sgi grey image upsidedown (cannot check)'
  130.     w = imgsgi.writer('out-grey-b2t.rgb')
  131.     w.width, w.height, w.file_format = width, height, imgformat.grey_b2t
  132.     w.write(rgb_t2b_data)
  133.     del w
  134. else:
  135.     print '- ** No SGI support available'
  136.  
  137. #
  138. # Step 4 - JPEG writer
  139. #
  140. print '- Write jpeg rgb image (cannot check)'
  141. w = imgjpeg.writer('out-rgb-t2b.jpg')
  142. w.width, w.height, w.format = width, height, imgformat.rgb
  143. w.write(rgb_t2b_data)
  144. del w
  145.  
  146. print '- Read jpeg rgb image and copy to ppm'
  147. r = imgjpeg.reader('out-rgb-t2b.jpg')
  148. checksize(r)
  149. w = imgppm.writer('out-rgb-t2b-viajpeg.ppm')
  150. w.width, w.height = width, height
  151. w.write(r.read())
  152. del w
  153. del r
  154.  
  155. # These don't always work: the C module want xgrey
  156. print '- Write jpeg grey image (cannot check)'
  157. w = imgjpeg.writer('out-grey-t2b.jpg')
  158. if imgformat.grey in w.format_choices:
  159.     w.width, w.height, w.format = width, height, imgformat.grey
  160.     w.write(grey_t2b_data)
  161.     del w
  162.  
  163.     print '- Read jpeg grey image and copy to pgm'
  164.     r = imgjpeg.reader('out-grey-t2b.jpg')
  165.     r.format = imgformat.grey
  166.     checksize(r)
  167.     w = imgpgm.writer('out-rgb-t2b-viajpeg.pgm')
  168.     w.width, w.height = width, height
  169.     w.write(r.read())
  170.     del r
  171. del w
  172.  
  173.  
  174. #
  175. # step 5 - TIFF writer
  176. #
  177. print '- Write tiff rgb image'
  178. w = imgtiff.writer('out-rgb-t2b.tiff')
  179. w.width, w.height, w.format = width, height, imgformat.rgb
  180. w.write(rgb_t2b_data)
  181. del w
  182.  
  183. print '  (check)'
  184. r = imgtiff.reader('out-rgb-t2b.tiff')
  185. checkreader(r, rgb_t2b_data)
  186. del r
  187.  
  188. print '- Write tiff grey image'
  189. w = imgtiff.writer('out-grey-t2b.tiff')
  190. w.width, w.height, w.format = width, height, imgformat.grey
  191. w.write(grey_t2b_data)
  192. del w
  193.  
  194. print '  (check)'
  195. r = imgtiff.reader('out-grey-t2b.tiff')
  196. checkreader(r, grey_t2b_data)
  197. del r
  198.  
  199. print '- Read tiff grey image and copy to rgb ppm'
  200. r = imgtiff.reader('out-grey-t2b.tiff')
  201. if r.format <> imgformat.grey:
  202.     print '* It is not a greyscale image!'
  203. r.format = imgformat.rgb
  204. checksize(r)
  205. w = imgppm.writer('out-grey-t2b-rgbviatiff.ppm')
  206. w.width, w.height = width, height
  207. w.write(r.read())
  208. del w
  209. del r
  210.  
  211. print '- Read tiff rgb image and copy to grey pgm'
  212. r = imgtiff.reader('out-rgb-t2b.tiff')
  213. if r.format <> imgformat.rgb:
  214.     print '* It is not a rgb image!'
  215. r.format = imgformat.grey
  216. checksize(r)
  217. w = imgpgm.writer('out-grey-t2b-greyviatiff.pgm')
  218. w.width, w.height = width, height
  219. w.write(r.read())
  220. del w
  221. del r
  222.  
  223. #
  224. # Step 6 - GIF reader
  225. #
  226. print '- Read GIF image and copy to GIF'
  227. r = imggif.reader('in-map-t2b.gif')
  228. checksize(r)
  229. w = imggif.writer('out-map-t2b.gif')
  230. w.width, w.height, w.colormap = r.width, r.height, r.colormap
  231. gifdata = r.read()
  232. w.write(gifdata)
  233. gifmap = r.colormap
  234. del r
  235. del w
  236.  
  237. print '- Re-read GIF image, compare and re-write'
  238. r = imggif.reader('out-map-t2b.gif')
  239. checksize(r)
  240. newdata = r.read()
  241. if newdata <> gifdata:
  242.     print '* Re-read GIF data differs!', (gifdata[:16], newdata[:16])
  243.     warning = 1
  244. if gifmap._map_as_string <> r.colormap._map_as_string:
  245.     print '* Re-read GIF colormap differs!: ', (gifmap._map_as_string[:16],
  246.                         r.colormap._map_as_string[:16])
  247.     warning = 1
  248. w = imggif.writer('out-map-t2b-2.gif')
  249. w.width, w.height, w.colormap = r.width, r.height, r.colormap
  250. w.write(newdata)
  251. del r
  252. del w
  253. del gifmap
  254. del gifdata
  255. del newdata
  256.  
  257. print '- Read GIF image, map, and copy to ppm'
  258. r = imggif.reader('in-map-t2b.gif')
  259. checksize(r)
  260. w = imgppm.writer('out-rgb-t2b-viagif.ppm')
  261. w.width, w.height = r.width, r.height
  262. gifdata = r.read()
  263. rgbdata = r.colormap.map(gifdata, r.width, imgformat.colormap, imgformat.rgb)
  264. w.write(rgbdata)
  265. del r
  266. del w
  267. del rgbdata
  268. del gifdata
  269.  
  270. print '- Read GIF image upsidedown, map, and copy to ppm'
  271. r = imggif.reader('in-map-t2b.gif')
  272. r.format = imgformat.colormap_b2t
  273. checksize(r)
  274. w = imgppm.writer('out-rgb-b2t-viagif.ppm')
  275. w.width, w.height = r.width, r.height
  276. gifdata = r.read()
  277. rgbdata = r.colormap.map(gifdata, r.width, imgformat.colormap, imgformat.rgb)
  278. w.write(rgbdata)
  279. del r
  280. del w
  281. del rgbdata
  282. del gifdata
  283.  
  284. #
  285. # Step 7 - Test pict writer
  286. #
  287. ##print '- Write PICT rgb image'
  288. ##w = imgpict.writer('out-rgb-t2b.pict')
  289. ##w.width, w.height, w.format = width, height, imgformat.rgb
  290. ##w.write(rgb_t2b_data)
  291. ##del w
  292. #
  293. # Part 8 - Test pbm reader/writer
  294. #
  295. print '- Read pbm original image'
  296. r = imgpbm.reader('in-icon.pbm')
  297. width, height = r.width, r.height
  298. mono_data = r.read()
  299. del r
  300.  
  301. print '- Write pbm original image'
  302. w = imgpbm.writer('out-icon.pbm')
  303. w.width, w.height, w.format = width, height, imgformat.pbmbitmap
  304. w.write(mono_data)
  305. del w
  306.  
  307. print '- Re-read pbm image'
  308. r = imgpbm.reader('out-icon.pbm')
  309. checkreader(r, mono_data)
  310.  
  311. print '- Dither grey to mono and write'
  312. try:
  313.     from imgop import dither
  314. except ImportError:
  315.     print '** skipped (no ditherer)'
  316.     dither = None
  317. if dither:
  318.     r = imgpgm.reader('in-grey-t2b.pgm')
  319.     w = imgpbm.writer('out-mono-t2b.pbm')
  320.     d = dither(r.read(), r.width, r.height, r.format, imgformat.pbmbitmap)
  321.     w.width, w.height, w.format = r.width, r.height, imgformat.pbmbitmap
  322.     w.write(d)
  323.  
  324. print '- Read PBM file as PGM and write'
  325. r = imgpgm.reader('in-icon.pbm')
  326. w = imgpgm.writer('out-icon.pgm')
  327. w.width, w.height, w.format = r.width, r.height, r.format
  328. w.write(r.read())
  329.  
  330. if warning:
  331.     sys.exit(1)
  332.     
  333.  
  334.