wtorek, 3 lutego 2015

GIMP - how to export image to base64

That was somewhat extremely surprising to me that GIMP does not provide Base64 exporter out of the box (but I am not GIMP expert(maybe there is some easy way ;)). Nonetheless as it seems to be quite straightforward to implement, I faced the challenge. After short googling and refreshing deep buried Python knowledge it became simple, amazingly simple. Here you are some details in three easy steps: 0. Open "Python Console" (don't be afraid, it does not bite you, or choke either ;)
Go to "Filters" menu -> "Python-Fu". The python console should appear.
1. Take an image
img = gimp.image_list()[0]  // assuming you'd got just one image opened
2. Read all bytes
bytes = open(img.filename, "rb").read()
3. Encode bytes with 'base64' algorithm
bytes.encode('base64')
And finally "one-liner":
open(gimp.image_list()[0].filename, "rb").read().encode('base64')
bytes.encode('base64') puts some new-line characters, alternatively you can use the following:
import base64
base64.b64encode(open(gimp.image_list()[0].filename, "rb").read())
Reference: Python Scripting Official Doc

3 komentarze:

  1. Thanks for this great post! Inspired by this (and in the need for such solution at my work) I've written today this plugin: https://github.com/sirmacik/gimp-plugin-base64_file_encoder

    Hope others will find it useful too.

    Cheers
    Marcin

    OdpowiedzUsuń
  2. Thanks for inspiring sirmacik who insired me https://github.com/CrandellWS/base64-gimp-plugin/releases

    Dzięki za inspirującą sirmacik którzy mnie insired https://github.com/CrandellWS/base64-gimp-plugin/releases

    OdpowiedzUsuń