def go(self): try: FIPYOld = FreeImagePy.freeimage() import FreeImagePy as FIPY F = FIPY.Image() except FreeImagePy.FreeImagePy_LibraryNotFound, ex: self.printMsg("Error:\n %s " % str(ex)) raw_input("Press ok for exit...") return
def go(self): try: FIPY = FreeImagePy.freeimage() except FreeImagePy.FreeImagePy_LibraryNotFound, ex: self.printMsg("Error:\n %s " % str(ex)) raw_input("Press ok for exit...") return
def setUp(self): self.freeimage = FreeImagePy.freeimage() self.bitmap = self.freeimage.genericLoader(imagePath) if self.bitmap == 0: raise IOError, "Unable to read test file" exception_value = "My value" self.exception_value = exception_value class MyException(Exception): pass self.MyException = MyException class RootFile(object): def __init__(self): self.posn = 0 class HasSeek(RootFile): def __init__(self): super(HasSeek, self).__init__() def seek(self, offset, whence): if whence == 0: self.posn = offset elif whence == 1: self.posn += offset else: raise IOError, "Unrecognized whence value %d" % whence class HasTell(RootFile): def __init__(self): super(HasTell, self).__init__() def tell(self): return self.posn class HasRead(RootFile): def __init__(self): super(HasRead, self).__init__() def read(self, n): raise IOError, "Not implemented" class NoWrite(HasSeek, HasTell): pass self.NoWrite = NoWrite class RaiseMyException(HasSeek, HasTell): def write(self, s): raise MyException, exception_value self.RaiseMyException = RaiseMyException class RaiseMemoryError(HasSeek, HasTell): def write(self, s): raise MemoryError, exception_value self.RaiseMemoryError = RaiseMemoryError
class goTest: def __init__(self): self.f = open("output.txt", "w") def printMsg(self, msg): print msg self.f.write("%s \n" % str(msg)) def exit(self): self.f.close() def go(self): try: FIPYOld = FreeImagePy.freeimage() import FreeImagePy as FIPY F = FIPY.Image() except FreeImagePy.FreeImagePy_LibraryNotFound, ex: self.printMsg("Error:\n %s " % str(ex)) raw_input("Press ok for exit...") return self.printMsg("Freeimage status: %s %s\n" % F.status) self.printMsg("Message: %s\n" % F.GetCopyrightMessage()) self.printMsg("Version: %s\n" % F.GetVersion()) #File names path imagePath = 'freeimage.jpg' imagePathOutPng = 'out_freeimage.png' imagePathOutRotate1 = 'out_reeimageRotate1.png' imagePathOutRotate2 = 'out_freeimageRotate2.png' newImagePIL = "out_freeimageToPil.png" outMultiPage = "./out_outMultiPage.tif" newMultiPage = "./out_newMultiFromNewMethod.tif" #Load and save manually. On save you can pass me the type, otherwise #I try to understand what image type you want. #Example: # image.png -> file: image, type: png, out: image.png # image.tif -> file image, type: tiff, out: image.tiff (standard compression) # image.tiffg4 -> file image, type: tiff G4, out: image.tiff (with g4 compression) # image.jpeg -> file image, type: jpeg, out: image.jpeg (standard compression) # image.jpeggo -> file image, type: jpeg, out: image.jpeg (good compression) # See extToType for more info F.load(imagePath) F.save(imagePathOutPng) if FIPY.utils.USE_PIL: PIL = F.convertToPil() PIL.save(newImagePIL) self.printMsg("PIL found and image saved (with PIL) on %s" % newImagePIL) else: self.printMsg("No PIL found, skip PIL test") del F #Load, rotate and save F = FIPY.Image(imagePath) newI = F.rotate(RR(-360, 360)) newI.save(imagePathOutRotate1) del newI, F F = FIPY.Image(imagePath) rotated = F.rotate(RR(-360, 360)) rotated.save(imagePathOutRotate2) del F #Help (old) method for convert the image to a multipage image (Old method) self.printMsg( "Return from convert to mutlipage: %s \n" % \ str(FIPYOld.convertToMultiPage((imagePath, imagePathOutRotate1, imagePathOutRotate2), outMultiPage, FreeImagePy.FIF_TIFF)) ) F = FIPY.Image() F.new((1000, 1000), multiBitmap=newMultiPage) F.appendPage(imagePath) #append from file name F.deletePage( 0 ) #delete the first page that freeimage NEED to have for work with a multi-page F.appendPage(bitmap=rotated, insert=0) #and/or insert from bitmap loaded del rotated #and split the image into a lot of singles image... (file to load, output format, output ext, filename start) self.printMsg( "Return from convert to single pages: %s \n" % \ str( F.convertToPages('output_convertToPages.png') ) ) #Load a file from a buffer f = open(imagePathOutPng, 'rb') F = FIPY.Image() F.fromBuffer(f.read()) F.save("output_fromBuffer.png") F.exit() #Metadata F = FIPY.Image(imagePath) self.printMsg("Metadata into image: %s" % F.metadata) del F #Close FIPYOld.DeInitialise() self.printMsg("Time passed: %f sec\n" % time.clock()) raw_input("Do you like FreeImage? Press ok.")
def setUp(self): self.freeimage = FreeImagePy.freeimage() exception_value = "My value" self.exception_value = exception_value class MyException(Exception): pass self.MyException = MyException class RootFile(object): def __init__(self): self.posn = 0 class HasSeek(RootFile): def __init__(self): super(HasSeek, self).__init__() def seek(self, offset, whence): if whence == 0: self.posn = offset elif whence == 1: self.posn += offset else: raise IOError, "Unrecognized whence value %d" % whence class HasTell(RootFile): def __init__(self): super(HasTell, self).__init__() def tell(self): return self.posn class HasRead(RootFile): def __init__(self): super(HasRead, self).__init__() def read(self, n): raise IOError, "Not implemented" class NoTell(HasSeek, HasRead): pass self.NoTell = NoTell class NoSeek(HasTell, HasRead): pass self.NoSeek = NoSeek class NoRead(HasSeek, HasTell): pass self.NoRead = NoRead class RaiseMyException(HasSeek, HasTell): def read(self, n): raise MyException, exception_value self.RaiseMyException = RaiseMyException class RaiseMemoryError(HasSeek, HasTell): def read(self, n): raise MemoryError, exception_value self.RaiseMemoryError = RaiseMemoryError class DummyFreeImage(object): def LoadFromHandle(self, type, io_p, handle, flags): io = io_p._obj io.tell_proc(None) io.seek_proc(None, 0, 0) io.read_proc(None, 0, 0, None) return 0 self.dummy_freeimage = DummyFreeImage()
try: self.freeimage.loadFromFile(self.NoRead(), FreeImagePy.FIF_JPEG) self.fail("No exception raised for missing read") except FreeImagePy.IOAttributeError, e: s = str(e) self.failUnless( s == "Unsupported file operation read", "Incorrect error message returned for no read\n" "'%s'\n" % s) except Exception, e: self.fail("No read: Expecting %s; got %s" % (self.MyException, e.__class__)) # No seek method try: FreeImagePy.FileIO(self.dummy_freeimage, self.NoSeek()).load(0) except FreeImagePy.IOAttributeError, e: s = str(e) self.failUnless( s == "Unsupported file operation seek", "Incorrect error message returned for no seek\n" "'%s'\n" % s) except Exception, e: self.fail("No seek: Expecting %s; got %s" % (self.MyException, e.__class__)) # No tell method try: FreeImagePy.FileIO(self.dummy_freeimage, self.NoTell()).load(0) except FreeImagePy.IOAttributeError, e: s = str(e)