Jul-31-2018, 08:18 PM
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:
Here's for instance a list representing a pixmap with width=60 and height= 8:
The only workaround I have for the time being is to invoke the Imagemagick command "convert" like so:
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!
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!