Skip to content

Instantly share code, notes, and snippets.

@wxg4net
Created October 29, 2013 07:46
Show Gist options
  • Save wxg4net/7210517 to your computer and use it in GitHub Desktop.
Save wxg4net/7210517 to your computer and use it in GitHub Desktop.
paint images in conky
require 'cairo'
require 'imlib2'
--~ require 'cairo_imlib2_helper'
--~ cairo_draw_image('/home/Data/pictres/500px/18.jpg' , cs, 0, 0, 0.2, 0.2)
function conky_images()
if conky_window == nil then return end
local sw = conky_window.width
local sh = conky_window.height
local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, sw, sh)
imlib_set_cache_size(4096 * 1024)
local image = imlib_load_image('/home/pictures/A01.jpg')
if image == nil then return end
imlib_context_set_image(image)
local w = imlib_image_get_width()
local h = imlib_image_get_height()
local scaled_w = w
local scaled_h = h
local premul = imlib_create_image(scaled_w, scaled_h)
imlib_context_set_image(premul)
imlib_context_set_color(0, 0, 0, 255)
imlib_image_fill_rectangle(0, 0, scaled_w, scaled_h)
imlib_blend_image_onto_image(image, 0, 0, 0, w, h, 0, 0, scaled_w, scaled_h)
imlib_image_copy_alpha_to_image(image, 0, 0)
local cfsw = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, scaled_w)
local result = cairo_image_surface_create_for_data( imlib_image_get_data_for_reading_only(),
CAIRO_FORMAT_ARGB32, scaled_w, scaled_h, cfsw)
local cr = cairo_create(cs);
cairo_set_source_surface(cr, result, 0, 0)
cairo_paint_with_alpha(cr, 0.5)
imlib_context_set_image(image)
imlib_free_image()
imlib_context_set_image(premul)
imlib_free_image()
cairo_destroy(cr)
cairo_surface_destroy(result)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment