Request More Help With XBM Image

Peter Otten __peter__ at web.de
Tue Mar 1 03:56:56 EST 2016


Wildman via Python-list wrote:

> I want to take an image file, convert it to XBM format and
> display it.  Thanks to Mr. Otten I can open and display the
> XBM image without any problems.  The script first calls an
> external program for the image conversion then I can open
> and display it.  Of course, I am left with the XBM file that
> needs to be deleted.  It seemed to me to be a better approach
> to use stdout and pipe thereby eliminating the XBM file
> altogether.  Here is code I have so far but I'm not sure
> what to do next...
> 
> convert = "convert " + fileName + " -resize 48x48! -threshold 55% xbm:-"
> p = subprocess.Popen([convert], stdout=subprocess.PIPE, shell=True)
> xbmFile, err = p.communicate()

Why would you need a shell?

> The variable fileName contains the image file path and name.
> The variable convert contains the complete command.  The last
> argument in the command tells the convert utility to covert
> to an XBM and to direct the output to stdout.  After the above
> code runs xbmFile contains the actual image, which is plain
> text.  (X BitMap (XBM) is a plain text binary image format.)
> 
> My question is how do I take the xbmFile variable and convert
> it to an image object that can be displayed?  The technique
> for displaying an image from a file does not work or at least
> I have not been able to get it to work.

I think Image.open() accepts a file-like object, so

import io
...
command = [
    "convert", fileName,
    "-resize", "48x48!",
    "-threshold", "55%",
    "xbm:-"]
p = subprocess.Popen(command, stdout=subprocess.PIPE)
xbmFile, err = p.communicate()
openImage = Image.open(io.BytesIO(xbmFile))

should work. 




More information about the Python-list mailing list