Jump to content

Use data:image/jpeg;base64 as filetype for icon


Recommended Posts

Hello,

 

I can't find in the forum anything similar to my bellow question, so I hope this is not a yet again asked question ...

I was trying to set 64bit encoded data image at the place of an image name in Workflow result display, but this does not work. Is there a was to do it or it's just not possible at all yet ?

 

Example:

 

<items>
<item uid="123456" arg="http://google.com" valid="YES">
<title>My row title</title>
<subtitle>some kind of subtitle</subtitle>
<icon type="filetype">
data:image/jpeg;base64, /9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg SlBFRyB2NjIpLCBxdWFsaXR5ID0gNzUK/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0a Hx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIy MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgAeABk AwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMF 

...

Vo1IOPeiin1EDDc5x24rT0uUJMschwrHg+hoorGezNINpqx1SOxijUMxAOHJwM8nH6VqRQRMSQoV cD60UVmjtjsJf3aWFo8q8ADgY6n0rgb65mvbh5pmLMepJoorSOxz1pMqxtty2BhefrVOe+luMj7i nsKKKt7HOQBBjpRRRUAf/9k=
</icon>
</item>
</items>
 
If this is not possible, does someone see a possible workaround if my image sources are 64encoded data ?
 
Olivier.
Edited by _oho
Link to comment
Thank you for your note RodgerWW.
 

Yes, I believe I will not have much other choices than do the conversion locally.

Actually I wanted to avoid too much dependencies from local side and provide from backend all the content needed to display my result from the workflow without need of some kind of local processing.

 

I have just did convert from backend side my base64 jpeg image to a base64 png image hoping that Alfred would accept a base64 image if it were png.

I believe there are no other options to provide with a local png as file only (in top of application icon option) - right ?
 
I will then now work on do the conversion locally from Alfred Workflow. Unfortunately I don't think I will succeed in doing this without need of dependencies like libjpeg, libpng and other Python stuff like PIL/Pillow or some kind of PHP ImageMagic ... ;(
 
_oho
Link to comment

Thank!

 

I'll have a look, it's still possible that I did something too complicated, but I couldn't find on Python an easy way to convert base64 jpeg to png file.

For info only (might be useful for others) I did something like :

def Base64ToPng(base64DataTxt,filename,destination_path):
  import base64
  import re
  from PIL import Image
  from io import BytesIO
  base64JustDataTxt = re.sub(r'data\:image\/jpeg;base64, ', "", base64DataTxt)
  #logging.error('base64JustDataTxt:\n' + base64JustDataTxt)
  im = Image.open(BytesIO(base64.b64decode(base64JustDataTxt)))
  im.save(destination_path + '/' + filename + '.png', 'PNG')
  return destination_path + '/' + filename + '.png'

Then I needed here PIL in particular (that was not that easy to install).

 

I'm currently porting my backend process to local python process within Alfred. Almost done ...

Once done, I'll double check to see if I can make it without extra dependencies ...

 

O.

Edited by _oho
Link to comment

Hum..., cool I didn't know sips at all!

And it look really handful and simple as from command line.

Thank for the tip.

 

This said, in my very particular case, the original image is a 64bits encoded jpeg data text.

Quickly looking at sips, I don't see any way to use as input a base64 image. Can we ?

Could it be that I have missed something ?

 

O.

Link to comment

I'd never heard of it either: I Googled "convert jpeg to png OS X command line".

 

I think you'd have to save the decoded data to JPEG files on disk, run sips to generate the PNGs, then delete the JPEGs again.

 

AFAIK, sips uses the filename to determine the type of image it's working with, so I guess piping the data to stdin won't work, either.

Link to comment

Thanks guys for putting me on the right track (and pushing me not to be too lazy).

There is indeed a way to manage it from command line without having to setup painful image management lib dependencies (like PIL in python - for instance).

 

I have just verified that the following command line from python does the trick (convert a base64 bit encoded jpeg data text to a local png file):

os.system("echo " + data + " | perl -MMIME::Base64 -ne 'print decode_base64($_)' > out.jpeg && sips -s format png out.jpeg --out out.png")

supposing that you have:

import os

and data is something like:

data = b'''/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg SlBFRyB2NjIpLCBxdWFsaXR5ID0gNzUK/9sAQwAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0a Hx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQy/9sAQwEJCQkMCwwYDQ0YMiEcITIy MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy/8AAEQgAeABk AwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMF 

...

Vo1IOPeiin1EDDc5x24rT0uUJMschwrHg+hoorGezNINpqx1SOxijUMxAOHJwM8nH6VqRQRMSQoV cD60UVmjtjsJf3aWFo8q8ADgY6n0rgb65mvbh5pmLMepJoorSOxz1pMqxtty2BhefrVOe+luMj7i nsKKKt7HOQBBjpRRRUAf/9k='''

Without doing it from Python, it would be from simple command line something like:

perl -MMIME::Base64 -ne 'print decode_base64($_)' < base64dataJPEG.txt > image.jpeg && sips -s format png image.jpeg --out image.png

_oho

Edited by _oho
Link to comment

Oh man! You're shelling out to Perl to do the decoding? *shudders*

from base64 import b64decode
import subprocess
import os

data = 'FASDFSF...'

with open('out.jpg', 'wb') as file:
    file.write(b64decode(data))

retcode = subprocess.call(['sips', '-s', 'format', 'png', 'out.jpg', '--out', 'out.png'])

if retcode:
    print('Dammit! sips done crashed')

os.unlink('out.jpg')  # delete the temp JPEG

(os.system etc. are kinda deprecated and subprocess is used these days. There's no chance of badly escaped args, though that's not a problem here.)

Edited by deanishe
Link to comment

;)))

 

Ok, crossing my hands on my back, looking at the ground and blushing ...

I have to admit that it was the fastest route based on what google gave me first. I'm a poor marketing guy ;(

 

Your rock, it's even better!

In particularly because I needed to get rid of standard output that prevented generating feedback on Alfred ... :)

I then could easily add stdout= on the subprocess.call:

FNULL = open(os.devnull, 'w')
retcode = subprocess.call(['sips', '-s', 'format', 'png', 'out.jpg', '--out', 'out.png'], stdout=FNULL, stderr=subprocess.STDOUT)

I was fighting using my command line to find a way with os.system to get rid of it, but to be honest it was a nightmare :). Not even sure I could have succeed ...

 

Thanx again. 

I love Alfred and Alfred community ...

 

_oho

Edited by _oho
Link to comment

I'd never heard of it either: I Googled "convert jpeg to png OS X command line".

 

I think you'd have to save the decoded data to JPEG files on disk, run sips to generate the PNGs, then delete the JPEGs again.

 

AFAIK, sips uses the filename to determine the type of image it's working with, so I guess piping the data to stdin won't work, either.

 

Thanks for your suggestion. It is really helpful.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...