Python Forum
imagemagick convert python equivalent
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
imagemagick convert python equivalent
#1
I have been looking for a python equivalent to the Imagemagick convert command.

More specifically I would like to convert .jpeg .png .tiff … images to something that would resemble the contents of an .xpm file (without the XPM headers), or in other words something that would approximate an X11 pixmap+palette.

Ideally, what I am looking for is a function that takes the 'path_to_the_filename' of the image on disk and return two values:

1. an X11-style palette in the form of a python dictionary with the key field containing an ASCII identifier and the value field the standard #rrggbb hex value of the associated color, such as for instance:

 
palette = { 
             "]" : "#090909",   
             "^" : "#0E0E0E",
             "/" : "#0A0A0A",
             "(" : "#0F0F0F",
             "_" : "#0C0C0C",
             ":" : "#050505",
             "<" : "#030303",
             "[" : "#020202"           
           }
2. an X11-style pixmap in the form of a list of strings where each pixel is represented by the ASCII identifier defining its color.

Here's for instance a list representing a pixmap with width=60 and height= 8:

pixmap = [
           "ieilllllllpinaev<]>;;>>>>>>;=**&&>,!),~~,)](<mlh888biieeefab",
           "eiekklllkewcmt>+&*-;->;;-;;->-),-~]!~),;;>>,>;[0mfhaiieefaba",
           "iekkklleonm<-.#&=->(5DppGGDGxxqqzooqcqqxDDg:g<$+-<zw9ieabaaa",
           "elkkkkfop3=+>;{1qzoAPJTTTSNWWWTVYWXSPZWJshnc8t_];#^esya99aaa",
           "eliiejc5]&!{zjwWZTZWSWTTPNPPPNWVNPNBLPTngfhnc8k6|!*&(ys8889a",
           "eiifa8:@*]kHAhsAQVTPPQQPNNNJJJJJAnnnuJNusAnccccyvg_'+~4ac8aa",
           "eiea9<&+]5oa889nNNNNNPPPNNJBBBBAusssuJNNVNsnnswJBno4'.-|j889",
           "iefb5!.=2l89a889BLJJNNNNNJJAAABBBAuwAJNNQTNAAAAJVNNF4'#)ma89"
         ]
I would then be able to code something like:

from xxxx import convert
...
palette, pixmap = convert('/home/me/images/example.jpg')
But I couldn't find anything even remotely related to these aspects anywhere.

The only workaround I have for the time being is to invoke the Imagemagick command "convert" like so:

subprocess.check_output(['convert', image, xpm])
In other words I create a temporary .xpm file (file name in the xpm variable) from the .jpg .png or .tiff file whose file name is referenced by the image variable. And then of course I have about 30-40 lines of rather convoluted code that strips the Xpm file headers and extracts the palette to a dictionary (of the form described above) and the asssociated pixmap to a list of strings.

I was wondering if anyone would be aware of any python module/library that would provide a tool to do this in a less clumsy way and dispense with the temporary file.

Any suggestions?

Thank you!
Reply
#2
You can try this tool:

from PIL import Image
from xpm import pil_save
im = Image.open('your_image.jpg')
print(pil_save(im).decode('utf-8'))
Reply
#3
I have just installed the xpm library/module and briefly played with it. Looks promising.

Will let you know what happens.
Reply
#4
I investigated further and some things about the xpm structure created by pil_save() did not look right. So I thought I'd save the xpm to disk and compare it to what I get when I use Imagemagick's convert command.

There are some differences indeed. The header appears to be ok since the file is recognized as an XPM by Imagemagick's "identify" command and the shell's "file" command. But if I try to view the file (eye of gnome… feh… The Gimp…) the image viewers I tried all refused to load the image and complained that there is something wrong with the file.

I proceeded to display the contents of the file in vim, side by side with an equivalent 64x64 .xpm file created from the same .jpg via Imagemagick and saw right away that the pixmap part of the file created by pil_save() is a mess: if I step away from the screen some 10-12 feet or thereabout… the imagemagick converted .xpm sort of looks like the original file in ascii/libcaca representation. I can just about see what it's supposed to represent. The one I created using something like the code below on the other hand is totally unrecognizable.

Just in case there's some kind of gross misunderstanding on my part and to clarify the above, here is the code I used to create the file with pil_save():

...

from PIL import Image
from xpm import pil_save

...

im_orig = Image.open('my_image.jpg')
width_height = (64, 64)
im_r = im_orig.resize(width_height)
im_s = pil_save(im_r)
im_l = im_s.split('/n')

ofile = open('/tmp/im.xpm', 'w')
for line in im_l:
    ofile.write("%s\n" % line)
ofile.close()
Obviously, since I have no experience with PIL (or image encoding and processing in general) this could boil down to what is usually referred to as a "user error".

As you're probably not directly involved in the development/maintenance of the xpm module/library, I guess the way to go at this point would be to open an issue on the xpm github page and see if I can get any help there.

Otherwise I suppose I could stick with using with my ineffecient imagemagick "convert" commands and try to use python's BytesIO… See if I can create "virtual" files in RAM and dispense with creating/deleting disk files all over the place and polluting the file system.

Thanks for you help.
Reply
#5
You can also use one of python's interfaces to imagemagick such as wand
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020