Last active
February 9, 2021 08:28
-
-
Save diazvictor/eb81371520d8c47ab79646a976c0751b to your computer and use it in GitHub Desktop.
PngImageView - Converting An Image To Base64 And Vice Versa For Display In An Image Using LGI (Lua + Gtk)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!-- Generated with glade 3.22.1 --> | |
<interface> | |
<requires lib="gtk+" version="3.20"/> | |
<object class="GtkFileFilter" id="filter_png"> | |
<mime-types> | |
<mime-type>image/png</mime-type> | |
</mime-types> | |
</object> | |
<object class="GtkWindow" id="window"> | |
<property name="can_focus">False</property> | |
<property name="resizable">False</property> | |
<property name="window_position">center</property> | |
<property name="default_width">400</property> | |
<property name="default_height">600</property> | |
<child type="titlebar"> | |
<object class="GtkHeaderBar"> | |
<property name="visible">True</property> | |
<property name="can_focus">False</property> | |
<property name="title" translatable="yes">TITULO</property> | |
<property name="subtitle" translatable="yes">SUBTITULO</property> | |
</object> | |
</child> | |
<child> | |
<object class="GtkBox"> | |
<property name="visible">True</property> | |
<property name="can_focus">False</property> | |
<property name="orientation">vertical</property> | |
<property name="spacing">2</property> | |
<child> | |
<object class="GtkFileChooserButton" id="input_file"> | |
<property name="visible">True</property> | |
<property name="can_focus">False</property> | |
<property name="margin_left">2</property> | |
<property name="margin_right">2</property> | |
<property name="margin_top">2</property> | |
<property name="margin_bottom">2</property> | |
<property name="title" translatable="yes"/> | |
</object> | |
<packing> | |
<property name="expand">False</property> | |
<property name="fill">True</property> | |
<property name="position">0</property> | |
</packing> | |
</child> | |
<child> | |
<object class="GtkImage" id="img"> | |
<property name="visible">True</property> | |
<property name="can_focus">False</property> | |
<property name="margin_left">2</property> | |
<property name="margin_right">2</property> | |
<property name="margin_top">2</property> | |
<property name="margin_bottom">2</property> | |
<property name="stock">gtk-missing-image</property> | |
</object> | |
<packing> | |
<property name="expand">True</property> | |
<property name="fill">True</property> | |
<property name="position">1</property> | |
</packing> | |
</child> | |
</object> | |
</child> | |
</object> | |
</interface> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/lua5.1 | |
--[[-- | |
@package | |
@filename PngImageView.lua | |
@version 1.0 | |
@author Díaz Urbaneja Víctor Eduardo Diex <victor.vector008@gmail.com> | |
@date 22.05.2020 17:20:01 -04 | |
]] | |
local base64 = require('base64') -- github.com/iskolbin/lbase64 | |
local lgi = require('lgi') | |
local Gtk = lgi.require('Gtk', '3.0') | |
local Gio = lgi.Gio | |
local GdkPixbuf = lgi.GdkPixbuf | |
local assert = lgi.assert | |
local builder = Gtk.Builder() | |
assert(builder:add_from_file('main_window.ui'), "Error loading the file") | |
local ui = builder.objects | |
ui.input_file:add_filter(ui.filter_png) | |
function ui.input_file:on_selection_changed() | |
-------------------------------------------------------------------------------- | |
local dir_img = ui.input_file:get_filename() | |
local file = io.open(dir_img, 'rb') | |
local image_file = file:read('*a') | |
local image_encode = base64.encode(image_file) | |
local image_decode = base64.decode(image_encode) | |
-------------------------------------------------------------------------------- | |
local stream = Gio.MemoryInputStream.new_from_data(image_decode) | |
local image = GdkPixbuf.Pixbuf.new_from_stream(stream) | |
image = image:scale_simple(320, 320, 'BILINEAR') | |
ui.img:set_from_pixbuf(image) | |
end | |
function ui.window:on_destroy() | |
Gtk.main_quit() | |
end | |
ui.window:show_all() | |
Gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment