| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | # Convert "arbitrary" image files to rgb files (SGI's image format). | 
					
						
							|  |  |  | # Input may be compressed. | 
					
						
							|  |  |  | # The uncompressed file type may be PBM, PGM, PPM, GIF, TIFF, or Sun raster. | 
					
						
							|  |  |  | # An exception is raised if the file is not of a recognized type. | 
					
						
							|  |  |  | # Returned filename is either the input filename or a temporary filename; | 
					
						
							|  |  |  | # in the latter case the caller must ensure that it is removed. | 
					
						
							|  |  |  | # Other temporary files used are removed by the function. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import tempfile | 
					
						
							|  |  |  | import pipes | 
					
						
							|  |  |  | import imghdr | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | table = {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-12-17 15:25:27 +00:00
										 |  |  | t = pipes.Template() | 
					
						
							| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | t.append('fromppm $IN $OUT', 'ff') | 
					
						
							|  |  |  | table['ppm'] = t | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-12-17 15:25:27 +00:00
										 |  |  | t = pipes.Template() | 
					
						
							| 
									
										
										
										
											1993-10-11 12:39:15 +00:00
										 |  |  | t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--') | 
					
						
							| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | t.append('fromppm $IN $OUT', 'ff') | 
					
						
							|  |  |  | table['pnm'] = t | 
					
						
							|  |  |  | table['pgm'] = t | 
					
						
							|  |  |  | table['pbm'] = t | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-12-17 15:25:27 +00:00
										 |  |  | t = pipes.Template() | 
					
						
							| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | t.append('fromgif $IN $OUT', 'ff') | 
					
						
							|  |  |  | table['gif'] = t | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-12-17 15:25:27 +00:00
										 |  |  | t = pipes.Template() | 
					
						
							| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | t.append('tifftopnm', '--') | 
					
						
							|  |  |  | t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--') | 
					
						
							|  |  |  | t.append('fromppm $IN $OUT', 'ff') | 
					
						
							|  |  |  | table['tiff'] = t | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-12-17 15:25:27 +00:00
										 |  |  | t = pipes.Template() | 
					
						
							| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | t.append('rasttopnm', '--') | 
					
						
							| 
									
										
										
										
											1993-10-11 12:39:15 +00:00
										 |  |  | t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--') | 
					
						
							| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | t.append('fromppm $IN $OUT', 'ff') | 
					
						
							|  |  |  | table['rast'] = t | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-12-17 15:25:27 +00:00
										 |  |  | t = pipes.Template() | 
					
						
							| 
									
										
										
										
											1993-06-23 09:30:50 +00:00
										 |  |  | t.append('djpeg', '--') | 
					
						
							| 
									
										
										
										
											1993-10-11 12:39:15 +00:00
										 |  |  | t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--') | 
					
						
							| 
									
										
										
										
											1993-06-23 09:30:50 +00:00
										 |  |  | t.append('fromppm $IN $OUT', 'ff') | 
					
						
							|  |  |  | table['jpeg'] = t | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-12-17 15:25:27 +00:00
										 |  |  | uncompress = pipes.Template() | 
					
						
							| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | uncompress.append('uncompress', '--') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-18 14:59:33 +00:00
										 |  |  | class error(Exception): | 
					
						
							|  |  |  | 	pass | 
					
						
							| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def torgb(filename): | 
					
						
							|  |  |  | 	temps = [] | 
					
						
							|  |  |  | 	ret = None | 
					
						
							|  |  |  | 	try: | 
					
						
							|  |  |  | 		ret = _torgb(filename, temps) | 
					
						
							|  |  |  | 	finally: | 
					
						
							|  |  |  | 		for temp in temps[:]: | 
					
						
							| 
									
										
										
										
											2000-12-12 23:11:42 +00:00
										 |  |  | 			if temp != ret: | 
					
						
							| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | 				try: | 
					
						
							|  |  |  | 					os.unlink(temp) | 
					
						
							|  |  |  | 				except os.error: | 
					
						
							|  |  |  | 					pass | 
					
						
							|  |  |  | 				temps.remove(temp) | 
					
						
							|  |  |  | 	return ret | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _torgb(filename, temps): | 
					
						
							|  |  |  | 	if filename[-2:] == '.Z': | 
					
						
							| 
									
										
										
										
											2002-08-09 16:38:32 +00:00
										 |  |  | 		(fd, fname) = tempfile.mkstemp() | 
					
						
							|  |  |  | 		os.close(fd) | 
					
						
							| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | 		temps.append(fname) | 
					
						
							|  |  |  | 		sts = uncompress.copy(filename, fname) | 
					
						
							|  |  |  | 		if sts: | 
					
						
							|  |  |  | 			raise error, filename + ': uncompress failed' | 
					
						
							|  |  |  | 	else: | 
					
						
							|  |  |  | 		fname = filename | 
					
						
							|  |  |  | 	try: | 
					
						
							|  |  |  | 		ftype = imghdr.what(fname) | 
					
						
							|  |  |  | 	except IOError, msg: | 
					
						
							|  |  |  | 		if type(msg) == type(()) and len(msg) == 2 and \ | 
					
						
							|  |  |  | 			type(msg[0]) == type(0) and type(msg[1]) == type(''): | 
					
						
							|  |  |  | 			msg = msg[1] | 
					
						
							| 
									
										
										
										
											2000-12-12 23:11:42 +00:00
										 |  |  | 		if type(msg) is not type(''): | 
					
						
							| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | 			msg = `msg` | 
					
						
							|  |  |  | 		raise error, filename + ': ' + msg | 
					
						
							|  |  |  | 	if ftype == 'rgb': | 
					
						
							|  |  |  | 		return fname | 
					
						
							| 
									
										
										
										
											2000-12-12 23:11:42 +00:00
										 |  |  | 	if ftype is None or not table.has_key(ftype): | 
					
						
							| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | 		raise error, \ | 
					
						
							|  |  |  | 			filename + ': unsupported image file type ' + `ftype` | 
					
						
							| 
									
										
										
										
											2002-08-10 00:17:59 +00:00
										 |  |  | 	(fd, temp) = tempfile.mkstemp() | 
					
						
							| 
									
										
										
										
											2002-08-09 16:38:32 +00:00
										 |  |  | 	os.close(fd) | 
					
						
							| 
									
										
										
										
											1992-10-18 17:09:59 +00:00
										 |  |  | 	sts = table[ftype].copy(fname, temp) | 
					
						
							|  |  |  | 	if sts: | 
					
						
							|  |  |  | 		raise error, filename + ': conversion to rgb failed' | 
					
						
							|  |  |  | 	return temp |