FreeImagePy examples




Simple example on how to use FreeImagePy - see into the test subdirectory package for more info.

-------  New method, with the Image class that is more "pythonic"

#File names path
imagePath = 'freeimage.jpg'
imagePathOutPng = 'freeimage.png'
imagePathOutRotate1 = 'freeimageRotate1.png'
imagePathOutRotate2 = 'freeimageRotate2.png'
newMultiPage = "./newMultiFromNewMethod.tif"

#Load the library
import FreeImagePy as FIPY
F = FIPY.Image()
from random import randrange as RR

#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)

#Convert to PIL :
pil = 
FIPY.convertToPil(F)
#OR
pil =F.convertToPil()

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

#Create a new multi-page image, and use append and delete methods
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

#Convert to single pages
F.convertToPages('output_convertToPages.png'))

#Create a new image from buffer
f = open(imagePathOutPng, 'rb')
F = FIPY.Image()
F.fromBuffer(f.read())
F.save("output_fromBuffer.png")

-------  Old method
from random import randrange as RR

#Load the library
import FreeImagePy
FIPY = FreeImagePy.freeimage()

#Get some output messages
FIPY.getStatus()
FIPY.GetCopyrightMessage()
FIPY.GetVersion()

#File names path
imagePath = 'freeimage.jpg'
imagePathOutPng = 'freeimage.png'
imagePathOutRotate1 = 'freeimageRotate1.png'
imagePathOutRotate2 = 'freeimageRotate2.png'

#Load and save manually
image = FIPY.genericLoader(imagePath)
FIPY.Save(FreeImagePy.FIF_PNG, image, imagePathOutPng)
FIPY.Unload(image)

#Load, rotate and save, but not unload the image, because I want to use it after
image = FIPY.genericLoader(imagePath) # call the generic loader
image = FIPY.RotateClassic(image, RR(-360, 360)) # -> here I cannot pass close = 1
FIPY.Save(FreeImagePy.FIF_PNG, image, imagePathOutRotate1)

#Load, rotate, save and unload the image
image = FIPY.RotateClassic(image, RR(-360, 360), close=1)  # -> here yes ...
FIPY.Save(FreeImagePy.FIF_PNG, image, imagePathOutRotate2)
FIPY.Unload(image)

#Simple method for load and save the image in one step into current directory
FIPY.loadAndSave(imagePathOutRotate1, './out_from_LoadAndSave_', 'bmp')

#Help method for convert the image to a multipage image
FIPY.convertToMultiPage((imagePath, imagePathOutRotate1, imagePathOutRotate2), './out.tif', FreeImagePy.FIF_TIFF)

#and split the image into a lot of singles image...
FIPY.convertToSinglePages('./out.tif', FreeImagePy.FIF_PNG, '.png', 'outputFromSinglesPages')

#Close the library, do it always when you end to use the libray!!!
FIPY.DeInitialise()