Re: Request More Help With XBM Image
On Tue, 01 Mar 2016 12:56:03 -0600, Wildman wrote: > On Tue, 01 Mar 2016 19:26:55 +0100, Peter Otten wrote: > >> An exception is raised because you pass the command as a single argument > > > > I did not realize that how the command was passed would > make such a difference. I guess I am stuck in my old > VB habits for creating variables. You don't have to > say it, this ain't VB! :-) > > After making the changes to "command = ", it works fine > without the shell. At the risk of sounding redundant, > thank you. Ok, after some thought I think I understand what happened. Since I had the command as a single element, bash was required to parse the command therefore my code would not run without shell=true. That makes sense. I may not be the fastest oar in the water but I will eventually reach the shore. Thanks again for all your help. -- GNU/Linux user #557453 The cow died so I don't need your bull! -- https://mail.python.org/mailman/listinfo/python-list
Re: Request More Help With XBM Image
On Tue, 01 Mar 2016 20:30:59 +0100, Christian Gollwitzer wrote: > Am 29.02.16 um 22:51 schrieb Wildman: >> 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. > > I think you are solving the wrong problem. If your aim is to just > display an image using Tkinter, you can use PIL to read the image and > transfer the bits into a Tkinter photoimage object. For an example, look > here: > > http://code.activestate.com/recipes/521918-pil-and-tkinter-to-display-images/ > > (this might need some updating if you are using Python 3) > > Christian Thanks. I have read that page, among many others, during my research. It deals with an actual image file. I am dealing with an image that is not a file. It was redirected to stdout from another process and was never saved to the hard drive. Anyway, with Mr. Otten's help the problem is resolved. -- GNU/Linux user #557453 "Real patriotism is a willingness to challenge the government when it's wrong." -Ron Paul -- https://mail.python.org/mailman/listinfo/python-list
Re: Request More Help With XBM Image
Am 29.02.16 um 22:51 schrieb Wildman: 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. I think you are solving the wrong problem. If your aim is to just display an image using Tkinter, you can use PIL to read the image and transfer the bits into a Tkinter photoimage object. For an example, look here: http://code.activestate.com/recipes/521918-pil-and-tkinter-to-display-images/ (this might need some updating if you are using Python 3) Christian -- https://mail.python.org/mailman/listinfo/python-list
Re: Request More Help With XBM Image
On Tue, 01 Mar 2016 19:26:55 +0100, Peter Otten wrote: > An exception is raised because you pass the command as a single argument I did not realize that how the command was passed would make such a difference. I guess I am stuck in my old VB habits for creating variables. You don't have to say it, this ain't VB! :-) After making the changes to "command = ", it works fine without the shell. At the risk of sounding redundant, thank you. -- GNU/Linux user #557453 "The Constitution only gives people the right to pursue happiness. You have to catch it yourself." -Benjamin Franklin -- https://mail.python.org/mailman/listinfo/python-list
Re: Request More Help With XBM Image
Wildman via Python-list wrote: > On Tue, 01 Mar 2016 09:56:56 +0100, Peter Otten wrote: >> Wildman via Python-list wrote: >>> 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? Just in case the example in my previous post has not made it clear: that was a rhetorical question. You do not need the shell, and in fact shouldn't use it. > 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 An exception is raised because you pass the command as a single argument like in [Python 2.7] >>> subprocess.Popen("ls /usr/share/dict/words") Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/subprocess.py", line 710, in __init__ errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory This looks for an executable called "ls /usr/share/dict/words" (the whole shebang!) where you actually want to run "ls" with the argument "/usr/share/dict/words". Once you split the parts >>> subprocess.Popen(["ls", "/usr/share/dict/words"]) >>> /usr/share/dict/words everything works as expected. The error message in Python 3.4 would have given you a clue, by the way: [Python 3.4] >>> subprocess.Popen("ls /usr/share/dict/words") Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.4/subprocess.py", line 859, in __init__ restore_signals, start_new_session) File "/usr/lib/python3.4/subprocess.py", line 1457, in _execute_child raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: 'ls /usr/share/dict/words' -- https://mail.python.org/mailman/listinfo/python-list
Re: Request More Help With XBM Image
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. -- 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Request More Help With XBM Image
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. -- https://mail.python.org/mailman/listinfo/python-list
Request More Help With XBM Image
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() 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. -- 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 -- https://mail.python.org/mailman/listinfo/python-list