Trying to use the img2xxx utils in the wx\tools directory

Which image formats are supported by the img2xxx tools?

Using -h doesn't show much, and looking at the sources doesn't explain
which
input formats are supported. The only "implication" is that .png
output format is supported, just from looking at the img2png.py
filename.

img2img.main(sys.argv[1:], wx.BITMAP_TYPE_PNG, ".png", __doc__)

I'm trying to convert some jpg files to png, then 2 .py, but it's not
happening.

Why? Why not simply convert them direct i.e. img2py [options]
(yourfilename).jpg outname.py - provided that the file extension is one that
is recognised, (i.e. .jpg or .jpeg for jpeg files), you shouldn't have a
problem.

Because it isn't working for some reason.
I have several jpegs in the same directory where the img2xxx.py
scripts are.

When I execute img2py, the doc string is displayed but the output file
isn't created

c:\Users\Me\Documents\Python>img2py.py -c 1.jpg ds.py

img2py.py -- Convert an image to PNG format and embed it in a Python
               module with appropriate code so it can be loaded into
               a program at runtime. The benefit is that since it is
               Python source code it can be delivered as a .pyc or
               'compiled' into the program using freeze, py2exe, etc.

Usage:

    img2py.py [options] image_file python_file

Options:

    -m <#rrggbb> If the original image has a mask or transparency
defined
                   it will be used by default. You can use this
option to
                   override the default or provide a new mask by
specifying
                   a colour in the image to mark as transparent.

    -n <name> Normally generic names (getBitmap, etc.) are used
for the
                   image access functions. If you use this option you
can
                   specify a name that should be used to customize the
access
                   fucntions, (getNameBitmap, etc.)

    -c Maintain a catalog of names that can be used to
reference
                   images. Catalog can be accessed via catalog and
                   index attributes of the module.
                   If the -n <name> option is specified then <name>
                   is used for the catalog key and index value,
otherwise
                   the filename without any path or extension is used
                   as the key.

    -a This flag specifies that the python_file should be
appended
                   to instead of overwritten. This in combination
with -n will
                   allow you to put multiple images in one Python
source file.

    -i Also output a function to return the image as a
wxIcon.

    -f Generate code compatible with the old function
interface.
                   (This option is ON by default in 2.8, use -F to
turn off.)

You can also import this module from your Python scripts, and use its
img2py()
function. See its docstring for more info.

Hi,

···

On 09/09/2010 16:14, cappy2112 wrote:

Why? Why not simply convert them direct i.e. img2py [options]
(yourfilename).jpg outname.py - provided that the file extension is one that
is recognised, (i.e. .jpg or .jpeg for jpeg files), you shouldn't have a
problem.

Because it isn't working for some reason.
I have several jpegs in the same directory where the img2xxx.py
scripts are.

I have a little script which does it (not my invention but can't remember where I got it from).

#!/usr/bin/env python
#----------------------------------------------------------------------

"""
This is a way to save the startup time when running img2py on lots of
files...
"""
import sys
from wx.tools import img2py
from wx import EmptyIcon

headerStuff = '''#Boa:PyImgResource:----------------------------------------------------
# This file was generated by encode_mybitmaps.py
#
from wx.lib.embeddedimage import PyEmbeddedImage
import cStringIO
'''

imageFile = file("../myimages.py", "w+")
imageFile.write(headerStuff)
imageFile.close()

command_lines = [
         "-a -u -f -i -n bottleCopy bottleCopy.png ../myimages.py",
         "-a -u -f -i -n glassChamp glassChamp.ico ../myimages.py",
         "-a -u -f -i -n pdficon pdficon_large.gif ../myimages.py",
         "-a -u -f -i -n vinote vinote.jpg ../myimages.py",
     ]

if __name__ == "__main__":
     for line in command_lines:
         args = line.split()
         img2py.main(args)

Hope this helps
Werner

Which image formats are supported by the img2xxx tools?

Using -h doesn't show much, and looking at the sources doesn't explain
which
input formats are supported. The only "implication" is that .png
output format is supported, just from looking at the img2png.py
filename.

The answer is all the image formats that were supported by your build of
wx which is quite a few..

PNG, JPeG, GIF, BMP, TIFF, and a few more obscure formats.

Why? Why not simply convert them direct i.e. img2py [options]
(yourfilename).jpg outname.py - provided that the file extension is one that
is recognised, (i.e. .jpg or .jpeg for jpeg files), you shouldn't have a
problem.

IIRC, it actually looks at the file data to determine image type. But it sounds like it is failing for cappy before it's getting that far.

Because it isn't working for some reason.
I have several jpegs in the same directory where the img2xxx.py
scripts are.

When I execute img2py, the doc string is displayed but the output file
isn't created

If it's showing the docstring then there was either a -h in the args list or there was an exception while parsing the args with getopt. Double-check your command line, ensure the input files exist, check the version of the img2py script, etc.

I'm not sure which one it was but there was a version that didn't open the input files with the binary flag, so they could get truncated on Windows if the data had an EOF character in the stream. So you may want to update the wx.tools package from SVN

···

On 9/9/10 12:25 AM, GadgetSteve@live.co.uk wrote:
On 9/9/10 7:14 AM, cappy2112 wrote:

--
Robin Dunn
Software Craftsman

I have a little script which does it (not my invention but can't
remember where I got it from).

Thanks Werner, but I'm not having a problem with a director full of
jpegs, the img2p2 script
won't even convert a single jpeg. The command line I've posted shows
one file as the input file

If it's showing the docstring then there was either a -h in the args

There is no -h in the command line I'm using. I've also posted the cmd
line in my original post.

Double-check your command line, ensure the input files exist, check the

The script only processes one file at a time, and is in the same
directory as the img2py script.

version of the img2py script, etc.

I've just installed wxPython for Python 2.6

The version of img2py is -> # RCS-ID: $Id: img2py.py 61389
2009-07-10 22:25:35Z RD $

I'm not sure which one it was but there was a version that didn't open
the input files with the binary flag, so they could get truncated on

Thats odd. Are not all image files binary files?

So you may want to update the wx.tools package from SVN

I'll try that once I figure out how to.

If it's showing the docstring then there was either a -h in the args

There is no -h in the command line I'm using. I've also posted the cmd
line in my original post.

Double-check your command line, ensure the input files exist, check the

The script only processes one file at a time, and is in the same
directory as the img2py script.

version of the img2py script, etc.

I've just installed wxPython for Python 2.6

The version of img2py is -> # RCS-ID: $Id: img2py.py 61389
2009-07-10 22:25:35Z RD $

I'm not sure which one it was but there was a version that didn't open
the input files with the binary flag, so they could get truncated on

Thats odd. Are not all image files binary files?

Yes, (except for those that are not, like XPM.) But that was not the issue in this case. On Windows the open() function will by default open the file in text mode, which will, because of the legacy imposed upon us by Bill and Paul many years ago, cause all \r\n sequences to be converted to \n and if there is a byte with a value of 26 (the ASCII EOF char) the file will be truncated at that point. To avoid this you have to tell Python to open the file in binary mode, and the previous version of the img2py code left out the 'b' in the mode parameter. Of course on posix based platforms there is no difference between text and binary files from the programming perspective and so this bug escaped into the wild in one of the wxPython releases because rebuilding the demo's images.py module always happens on the Mac or Linux for me.

So you may want to update the wx.tools package from SVN

I'll try that once I figure out how to.

Don't bother, it looks like you've got the newest already. wxTrac has been migrated to GitHub Issues - wxWidgets

Have you tried stepping through the code with a debugger to help you see where/why it is failing? It's really fairly simple code so it shouldn't be too hard to figure out why it is not working for you. Perhaps there is some MS issue with the file named '1.jpg' and it is trying to turn that into a file handle or something...

···

On 9/9/10 12:41 PM, cappy2112 wrote:

--
Robin Dunn
Software Craftsman

Don't bother, it looks like you've got the newest already.wxTrac has been migrated to GitHub Issues - wxWidgets

Have you tried stepping through the code with a debugger to help you see
where/why it is failing? It's really fairly simple code so it shouldn't

Yes, oddly enough when I do that (with Wing IDE), the file gets
processed normally.
I know that doesn't make sense.

I'm going to sprinkle some print statements in the code to see what's
happening, but I have to wait until I get home from work in a few
hours.

be too hard to figure out why it is not working for you. Perhaps there
is some MS issue with the file named '1.jpg' and it is trying to turn
that into a file handle or something...

I'll try a different filename

Have you tried stepping through the code with a debugger to help you see
where/why it is failing? It's really fairly simple code so it shouldn't
be too hard to figure out why it is not working for you. Perhaps there
is some MS issue with the file named '1.jpg' and it is trying to turn
that into a file handle or something...

Now that I know these scripts support input image formats other
than .png ...

Ok- here's what I've found:
When I execute img2py.py from the command prompt in Windows 7, I'm
doing it like this
img2py -c 1.jpg ds.py

instead of

python img2py.py -c 1.jpg ds.py

That causes

main(sys.argv[1:]) to pass to main, and makes the following code
display the docstring

if not args or ("-h" in args):
        print __doc__
        return

When I run img2py.py like this

python img2py.py -c 1.jpg ds.py

it works just fine.

I've just recently upgraded from Python 2.5 to 2.6, but wouldn't
expect a change of this nature since it would break everything.

I'm pretty sure I used to run scripts without putting 'python' on the
command line.

In this case it is probably running the img2py.bat batch file in ...\Python26\Scripts, assuming that folder is on your PATH. So after you've verified what exactly is being run, the next thing to look at is if the command-line args are being lost in the batch file or when it passes them on to its invocation of python.exe.

···

On 9/9/10 6:32 PM, cappy2112 wrote:

Ok- here's what I've found:
  When I execute img2py.py from the command prompt in Windows 7, I'm
doing it like this
img2py -c 1.jpg ds.py

That causes

main(sys.argv[1:]) to pass to main, and makes the following code
display the docstring

--
Robin Dunn
Software Craftsman

In this case it is probably running the img2py.bat batch file in
...\Python26\Scripts, assuming that folder is on your PATH. So after
you've verified what exactly is being run, the next thing to look at is
if the command-line args are being lost in the batch file or when it
passes them on to its invocation of python.exe.

Ok- mystery solved.

I had copied the img2py.py file into the directory where my jpegs
were, and was running it from there, without putting
python on the command line. If I leave off the .py the batch file
picks up all the args and passes them to main.

Thanks