Code of TutorialP2

Code of 'TutorialP2'
->show how to import/export from/to PIL image

import FiltersDllPy as FDP import Image import time from ctypes import *

## Filters image format is Little Endian (x86 / MS Windows, Linux) : BGR(A) order (0xAARRGGBB) ## but PIL image format is Big Endian (PPC / Linux, MaxOSX) : (A)RGB order (0xRRGGBBAA) def createImageFromImagePIL(aImagePIL): result = f.CreateImage(aImagePIL.size[0], aImagePIL.size[1]) imagePIL_RGBA = imagePIL.convert('RGBA') #make sure that the format is RGBA imagePIL_BGRA = Image.fromstring('RGBA', imagePIL_RGBA.size, imagePIL_RGBA.tostring, 'raw', 'BGRA') s = imagePIL_BGRA.tostring memmove(result[0].Bits, s, len(s)) return result
 * 1) convert 'PIL' image to 'Filters' image

## Filters image format is Little Endian (x86 / MS Windows, Linux) : BGR(A) order (0xAARRGGBB) ## but PIL image format is Big Endian (PPC / Linux, MaxOSX) : (A)RGB order (0xRRGGBBAA) def createImagePILFromImage(aImage): imageSize = aImage[0].Width, aImage[0].Height s = create_string_buffer(aImage[0].Width*aImage[0].Height*4) memmove(s, aImage[0].Bits, len(s)) imagePIL = Image.fromstring('RGBA', imageSize, s, 'raw', 'BGRA') return imagePIL
 * 1) convert 'Filters' image to 'PIL' image

f = FDP.FiltersDllPy f.Initialize

imagePIL = Image.open('C:/DEV/FiltersTutorial/Bin/lenna_color.bmp') print 'imagePIL : width=[' + str(imagePIL.size[0]) +'], height=[' + str(imagePIL.size[1]) + ']'
 * 1) get a image in PIL format from PIL library

t = time.time fImage = createImageFromImagePIL(imagePIL) duration = ((time.time-t)*1000) print 'createImageFromImagePIL duration : %3.0f' % duration + 'ms'
 * 1) convert it to Filters image format


 * 1) use it in Filters library...

print 'save fImage' f.saveImage(fImage, 'C:/DEV/FiltersTutorial/Bin/filtersImageFromPIL.bmp')
 * 1) for example we save it

print 'process fImage to fImageCanny with filterCanny' fImageCanny = f.CreateImageLike(fImage) par = ( ('inImage', fImage), ('outImage', fImageCanny), ('threshold1', 130), ('threshold2', 255), ) filterCanny = f.createFilterRun('filterCanny', par) print 'save fImageCanny' f.saveImage(fImageCanny, 'C:/DEV/FiltersTutorial/Bin/filtersImageFromPIL_canny.bmp')
 * 1) for example we process it with [filterCanny]
 * 1) don't forget that we have provided [outImage] as a parameter,
 * 2) so it's not a image created by [filterCanny], and this filter just fill it
 * 3) then we can directly use it

print 'process fImage to fImageRotation with filterRotation' par = ( ('inImage', fImage), ('angle', 75.0), ('interpolationMode', 1), #basic fast ('autoAdjustSize', True), ("missingPixelColorMode", "BLACK"), ) filterRotation = f.createFilterRun('filterRotation', par) print 'save fImageRotation' fImageRotation = f.GetOutputImage(filterRotation, 'outImage')
 * 1) now for example we can process it with [filterRotation]
 * 1) don't forget that we DON'T provided [outImage] as a parameter
 * 2) because [filterRotation] must create it (because it calcul the correct size)
 * 3) so IT'S a image created by [filterRotation]
 * 4) then we MUST obtain it with the function [GetOutputImage]

f.saveImage(fImageRotation, 'C:/DEV/FiltersTutorial/Bin/filtersImageFromPIL_rotation.bmp')
 * 1) print 'fImageRotation : width=[' + str(FDP.TFBitmap32(fImageRotation).Width) +'], height=[' + str(fImageRotation.Height) + ']'


 * 1) for other example, and until a real documentation,
 * 2) you can have a look on the code of 'FiltersTest' at :
 * 3) http://filters.sourceforge.net/wikifilters/index.php/Code_of_FiltersTest

t = time.time imagePIL = createImagePILFromImage(fImageRotation) duration = ((time.time-t)*1000) print 'createImagePILFromImage duration : %3.0f' % duration + 'ms'
 * 1) convert it to PIL image format

imagePIL.show
 * 1) use it in PIL library...

print 'free memory' f.FreeImage(fImage) f.FreeImage(fImageCanny) f.DeleteFilter(filterCanny) f.DeleteFilter(filterRotation)

print 'press [ENTER] to finish...' ch=raw_input

f.UnInitialize