Request More Help With XBM Image

Wildman best_lay at yahoo.com
Tue Mar 1 12:52:50 EST 2016


On Tue, 01 Mar 2016 09:56:56 +0100, Peter Otten wrote:

> 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?

I guess it is a Linux thing.  If I don't use it, I get
the below error.  A shell window does not actually open.
I presume it runs in the background while executing the
subprocess command.

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1535, in __call__
    return self.func(*args)
  File "./test.py", line 59, in open_image
    p = subprocess.Popen(command, stdout=subprocess.PIPE)
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

>> 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.

I does work, perfectly.  During my research I didn't run across
anything about using io.  The learning process continues...
I am in your debt again.  Thank you.

-- 
<Wildman> GNU/Linux user #557453
"Be at war with your vices, at peace with your neighbors,
and let every new year find you a better man."
  -Benjamin Franklin



More information about the Python-list mailing list