How to Read in Images via Batches Python

Python programming


  • i – PIL: Python Imaging Library
  • two – Loading an prototype
  • 3 – Saving an paradigm
  • 4 – Reading the pixels
  • 5 – Image Processing
  • 6 – Calculation a Watermark
  • 7 – Downloads

This is the first tutorial about Python and GeeXLab. The purpose of this series is to do an overview of the Python libraries that may be useful for GeeXLab demos AND that piece of work with GeeXLab.

GeeXLab has a very simple way of working. Roughly speaking, a demo is made up of an initialization script (INIT, executed one time) and a per frame script (FRAME, executed every frame). These scripts can be programmed either in Lua or in Python. In a script you tin code what you want with both languages. At that place is no restriction: GeeXLab can be seen as a virtual machine for Lua and Python. That'due south why, most the Python packages available will work with GeeXLab.

And to interact with GeeXLab scene data (textures, GLSL shaders, meshes, etc.) there is the Host-API which is simply GeeXLab API for Lua and Python. GeeXLab Host-API functions description is available Here.

This quick description of GeeXLab scripting is important for this series of tutorials nigh Python libraries considering I'm going to focuse on the functionalities offered by those libraries and I'll try to limit the apply of GeeXLab functions to the bare minimum. That way, yous will exist able to quickly re-use all code snippets in your own python projects.

That said, let'due south talk about the first Python lib: PIL.

1 – PIL: Python Imaging Library

PIL or Python Imaging Library is a package that exposes many functions to manipulate images from a Python script. PIL official homepage is HERE. The current version of PIL is PIL 1.one.seven and is bachelor for Python 2.three up to Python two.7. I'll utilize PIL one.1.7 for Python ii.half-dozen in this commodity.

Under Windows (XP, Vista or Seven) the installation of PIL is rather simple: just launch the PIL Windows installer and you're ok. Of course you need a valid Python 2.vi.six installation before.

PIL documentation is available here:

  • Online PIL md
  • PDF PIL doc

ii – Loading an image

Here is a small INIT script that uses PIL to load an image and display it. If you need it, PIL version can be establish in the Paradigm.VERSION variable.

import HYP_Utils import sys  from PIL import Image  scriptDir = HYP_Utils.GetDemoDir()  PIL_Version = Image.VERSION  img_filename = "%south/bloom.jpg" % scriptDir im = Image.open(img_filename) im.show()        

Under Windows, the Image.testify() function saves the image to a temporary file and calls the default epitome viewer utility. On my arrangement, Irfanview is called to brandish the prototype:

GeeXLab, Python, PIL
Demo_Python_PIL_01.xml

3 – Saving an image

Just call the Image.save() function. You desire to save to JPEG format? Just add the .jpg extension to your image filename… Same thing for the other formats.

Formats supported in reading AND writing: *.bmp, *.gif, *.jpg, *.msp, *.pcx, *.png, *.ppm, *.tiff and .xbm.

Hither is a simple JPG to BMP converter:

import HYP_Utils from PIL import Image  scriptDir = HYP_Utils.GetDemoDir()  PIL_Version = Paradigm.VERSION  img_filename = "%s/bloom.jpg" % scriptDir im = Image.open(img_filename) im.relieve("%s/flower.bmp" % scriptDir)        

4 – Reading the pixels

There are two functions that brand it possible to read the pixmap (or pixel data): Image.getpixel() and Prototype.getdata().

Image.getpixel() returns the value of a single pixel. Just give a tuple with the Ten and Y coordinates and getpixel() returns a 3-tuple RGB for a RGB epitome or a unmarried value for a luminance image. Image.getdata() returns the complete pixmap. You need the Python function list() to create the pixmap listing of RGB tuples.

Here is a code snippet that loads an image with PIL, creates a texture object with GeeXLab Python API and fills the texture with image pixels.

import HYP_Utils import HYP_Texture import HYP_Material import sys  from PIL import Epitome  scriptDir = HYP_Utils.GetDemoDir()  PIL_Version = Prototype.VERSION  img_filename = "%s/flower.jpg" % scriptDir im = Image.open(img_filename)  imageW = im.size[0] imageH = im.size[ane]  TEXTURE_2D = 2 RGB_BYTE = ii texId = HYP_Texture.Create(TEXTURE_2D, RGB_BYTE, imageW, imageH, 0)  matId = HYP_Material.GetId("plane1_mat") HYP_Material.AddTexture(matId, texId)  if (im.mode == "RGB"):   for y in range(0, imageH):     for 10 in range(0, imageW):       offset = y*imageW + x       xy = (10, y)       rgb = im.getpixel(xy)       HYP_Texture.SetValueTex2DByteRgb(texId, offset, rgb[0], rgb[1], rgb[2])  elif (imout.mode == "50"):   for y in range(0, imageH):     for x in range(0, imageW):       get-go = y*imageW + x       xy = (ten, y)       rgb = im.getpixel(xy)       HYP_Texture.SetValueTex2DByteRgb(texId, commencement, rgb, rgb, rgb)        

With Epitome.getdata(), the last lines of the previous script would exist:

pixels = list(im.getdata()) if (im.manner == "RGB"):   for y in range(0, imageH):     for ten in range(0, imageW):       offset = y*imageW + 10       rgb = pixels[offset]       HYP_Texture.SetValueTex2DByteRgb(texId, offset, rgb[0], rgb[i], rgb[2])  elif (imout.style == "L"):   for y in range(0, imageH):     for x in range(0, imageW):       outset = y*imageW + x       rgb = pixels[offset]       HYP_Texture.SetValueTex2DByteRgb(texId, starting time, rgb, rgb, rgb)        

GeeXLab, Python, PIL
Demo_Python_PIL_02.xml

five – Paradigm Processing

You can easily apply common image filters with PIL: blur, emboss, sharpen, etc. Just import the ImageFilter module:

from PIL import Prototype from PIL import ImageFilter  ...  i = Prototype.open(img_filename) im = i.filter(ImageFilter.EMBOSS) #im = i.filter(ImageFilter.FIND_EDGES)  ...        

Predefined filters are: Mistiness, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, Polish, SMOOTH_MORE, and Acuminate.

GeeXLab, Python, PIL
Demo_Python_PIL_03.xml – EMBOSS

GeeXLab, Python, PIL
Demo_Python_PIL_03.xml – FIND_EDGES

At that place is too a module chosen ImageOps that exposes image processing functions such equally colorize(), flip(), grayscale(), invert(), mirror(), solarize(), or posterize().

GeeXLab, Python, PIL
Demo_Python_PIL_04.xml – solarise()

GeeXLab, Python, PIL
Demo_Python_PIL_04.xml – posterize()

6 – Adding a Watermark

ImageDraw and ImageFont give to PIL the capability to write text on an paradigm as well as to draw lines or points. Here is a lawmaking snippet that shows a simple batch converter with PIL: it reads all jpg files of a binder, adds the watermark (a cross and the "GEEXLAB" string) and saves the images with the gxl_ prefix.

import HYP_Utils import os, glob from PIL import Image from PIL import ImageDraw from PIL import ImageFont  scriptDir = HYP_Utils.GetDemoDir()  ft = ImageFont.load("timR24.pil")  bone.chdir(scriptDir) file_list = glob.glob("*.jpg") for f in file_list:   im = Image.open(scriptDir + str(f))   draw = ImageDraw.Draw(im)   draw.line((0, 0) + im.size, fill up=(255, 255, 255))   depict.line((0, im.size[1], im.size[0], 0), fill=(255, 255, 255))   wh = ft.getsize("Chiliad E E X Fifty A B")   draw.text((im.size[0]/2 - wh[0]/2, im.size[1]/2 + 20), "G E E 10 Fifty A B",\   fill=(255, 255, 0), font=ft)   describe.text((im.size[0]/ii - wh[0]/2, im.size[i]/two - 60), "1000 Due east Due east X 50 A B",\           fill=(255, 255, 0), font=ft)   del draw     im.save(scriptDir + "gxl_" + str(f))        

The file timR24.pil comes from a PIL fonts package. You can download it HERE.

GeeXLab, Python, PIL
Demo_Python_PIL_05.xml

At that place is likewise a nice function ImageFont.truetype() only it didn't work on my system because this role relies on the _imagingft.pyd library (actually a DLL) that could not exist loaded due to a Visual C runtime trouble. Here is the mistake in GeeXLab console:

# ERROR: Python – Script [initScene] has a runtime error. Fault line: 29 – Error object: – Error data: DLL load failed: The application has failed to start because its side-by-side configuration is incorrect. Delight see the application event log or utilise the command-line sxstrace.exe tool for more detail.. Script disabled.

This link has some details about this trouble.

vii – Downloads

If you lot want to play with the GeeXLab demos, y'all can find them in the code samples demo pack available Hither. The demos related to PIL are in the Python_PIL/ binder.

Here is a patch for GeeXLab 0.2.5 + Python 2.vi.6 that you must utilize to run the demos. Just unzip the annal and copy the DLL into GeeXLab_Python_Lua/ binder.

PATCH DOWNLOAD: GeeXLabCore.dll

mccarthyyounnoubt.blogspot.com

Source: https://www.geeks3d.com/20100930/tutorial-first-steps-with-pil-python-imaging-library/

Belum ada Komentar untuk "How to Read in Images via Batches Python"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel