PyGobject(八十四)GdkPixbuf.Pixbuf

GdkPixbuf.Pixbuf

这是gdk-pixbuf库的主要结构。它被用于表示图像。它包含关于图像的像素数据、宽度和高度等信息。

这里写图片描述

Methods

方法修饰词方法名及参数
staticfrom_pixdata (pixdata, copy_pixels)
staticget_file_info (filename)
staticget_file_info_async (filename, cancellable, callback, *user_data)
staticget_file_info_finish (async_result)
staticget_formats ()
staticnew (colorspace, has_alpha, bits_per_sample, width, height)
staticnew_from_bytes (data, colorspace, has_alpha, bits_per_sample, width, height, rowstride)
staticnew_from_data (data, colorspace, has_alpha, bits_per_sample, width, height, rowstride, destroy_fn, *destroy_fn_data)
staticnew_from_file (filename)
staticnew_from_file_at_scale (filename, width, height, preserve_aspect_ratio)
staticnew_from_file_at_size (filename, width, height)
staticnew_from_inline (data, copy_pixels)
staticnew_from_resource (resource_path)
staticnew_from_resource_at_scale (resource_path, width, height, preserve_aspect_ratio)
staticnew_from_stream (stream, cancellable)
staticnew_from_stream_async (stream, cancellable, callback, *user_data)
staticnew_from_stream_at_scale (stream, width, height, preserve_aspect_ratio, cancellable)
staticnew_from_stream_at_scale_async (stream, width, height, preserve_aspect_ratio, cancellable, callback, *user_data)
staticnew_from_stream_finish (async_result)
staticnew_from_xpm_data (data)
staticsave_to_stream_finish (async_result)
add_alpha (substitute_color, r, g, b)
apply_embedded_orientation ()
composite (dest, dest_x, dest_y, dest_width, dest_height, offset_x, offset_y, scale_x, scale_y, interp_type, overall_alpha)
composite_color (dest, dest_x, dest_y, dest_width, dest_height, offset_x, offset_y, scale_x, scale_y, interp_type, overall_alpha, check_x, check_y, check_size, color1, color2)
composite_color_simple (dest_width, dest_height, interp_type, overall_alpha, check_size, color1, color2)
copy ()
copy_area (src_x, src_y, width, height, dest_pixbuf, dest_x, dest_y)
fill (pixel)
flip (horizontal)
get_bits_per_sample ()
get_byte_length ()
get_colorspace ()
get_has_alpha ()
get_height ()
get_n_channels ()
get_option (key)
get_options ()
get_pixels ()
get_rowstride ()
get_width ()
new_subpixbuf (src_x, src_y, width, height)
read_pixel_bytes ()
read_pixels ()
rotate_simple (angle)
saturate_and_pixelate (dest, saturation, pixelate)
save_to_bufferv (type, option_keys, option_values)
save_to_callbackv (save_func, user_data, type, option_keys, option_values)
savev (filename, type, option_keys, option_values)
scale (dest, dest_x, dest_y, dest_width, dest_height, offset_x, offset_y, scale_x, scale_y, interp_type)
scale_simple (dest_width, dest_height, interp_type)

Virtual Methods

Properties

NameTypeFlagsShort Description
bits-per-sampleintr/w/coThe number of bits per sample
colorspaceGdkPixbuf.Colorspacer/w/coThe colorspace in which the samples are interpreted
has-alphaboolr/w/coWhether the pixbuf has an alpha channel
heightintr/w/coThe number of rows of the pixbuf
n-channelsintr/w/coThe number of samples per pixel
pixel-bytesGLib.Bytesr/w/coReadonly pixel data
pixelsintr/w/coA pointer to the pixel data of the pixbuf
rowstrideintr/w/coThe number of bytes between the start of a row and the start of the next row
widthintr/w/coThe number of columns of the pixbuf

Signals

NameShort Description

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/19
# section 132
# 
# author: xiaosanyu
# website: yuxiaosan.tk \
#          http://blog.csdn.net/a87b01c14
# created: 16/7/19

TITLE = "Pixbuf"
DESCRIPTION = """
A GdkPixbuf represents an image, normally in RGB or RGBA format.
Pixbufs are normally used to load files from disk and perform
image scaling.

This demo is not all that educational, but looks cool. It was written
by Extreme Pixbuf Hacker Federico Mena Quintero. It also shows
off how to use GtkDrawingArea to do a simple animation.

Look at the Image demo for additional pixbuf usage examples.
"""

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GdkPixbuf, GLib
import os
from math import *

background_name = "background.jpg"
image_names = ("apple-red.png",
               "gnome-applets.png",
               "gnome-calendar.png",
               "gnome-foot.png",
               "gnome-gmush.png",
               "gnome-gimp.png",
               "gnome-gsame.png",
               "gnu-keys.png")
images = []
prefix = os.path.realpath(os.path.join(os.path.dirname(__file__), "Data"))
start_time = 0
CYCLE_TIME = 3000000


class PixbufsWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Pixbuf demo")
        self.set_resizable(False)
        self.background = None
        self.back_width = 10
        self.back_height = 10
        rlt = self.load_pixbufs()
        if not rlt[0]:
            dialog = Gtk.MessageDialog(self,
                                       Gtk.DialogFlags.DESTROY_WITH_PARENT,
                                       Gtk.MessageType.ERROR,
                                       Gtk.ButtonsType.CLOSE,
                                       "Failed to load an image: %s" % rlt[1])

            dialog.run()
            dialog.destroy()

        else:
            self.set_size_request(self.back_width, self.back_height)

            self.pixbuf = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, False, 8, self.back_width, self.back_height);

            da = Gtk.DrawingArea.new()

            da.connect("draw", self.draw_cb)

            self.add(da)

            da.add_tick_callback(self.on_tick, da)

    def load_pixbufs(self):
        if self.background:
            return True, ""  # already loaded earlier
        try:
            self.background = GdkPixbuf.Pixbuf.new_from_file(os.path.join(prefix, background_name))
            self.back_width = self.background.get_width()
            self.back_height = self.background.get_height()

            for image in image_names:
                images.append(GdkPixbuf.Pixbuf.new_from_file(os.path.join(prefix, image)))
        except GLib.Error as e:
            return False, e.message

        return True, ""

    def draw_cb(self, drawingarea, cr):
        Gdk.cairo_set_source_pixbuf(cr, self.pixbuf, 0, 0)
        cr.paint()

        return True

    def on_tick(self, da, frame_clock, *data):
        global start_time
        self.background.copy_area(0, 0, self.back_width, self.back_height,
                                  self.pixbuf, 0, 0)

        if start_time == 0:
            start_time = frame_clock.get_frame_time()

        current_time = frame_clock.get_frame_time()
        f = ((current_time - start_time) % CYCLE_TIME) / CYCLE_TIME

        xmid = self.back_width / 2.0
        ymid = self.back_height / 2.0

        radius = min(xmid, ymid) / 2.0

        for i, image in enumerate(images):
            r1 = Gdk.Rectangle()
            r2 = Gdk.Rectangle()
            dest = Gdk.Rectangle()

            ang = 2.0 * GLib.PI * i / len(images) - f * 2.0 * GLib.PI

            iw = image.get_width()
            ih = image.get_height()

            r = radius + (radius / 3.0) * sin(f * 2.0 * GLib.PI)

            xpos = floor(xmid + r * cos(ang) - iw / 2.0 + 0.5)
            ypos = floor(ymid + r * sin(ang) - ih / 2.0 + 0.5)

            k = sin(f * 2.0 * GLib.PI) if i & 1 else cos(f * 2.0 * GLib.PI)
            k = 2.0 * pow(k, 2)
            k = max(0.25, k)

            r1.x = xpos
            r1.y = ypos
            r1.width = iw * k
            r1.height = ih * k

            r2.x = 0
            r2.y = 0
            r2.width = self.back_width
            r2.height = self.back_height
            rlt = Gdk.rectangle_intersect(r1, r2)
            if rlt[0]:
                dest = rlt[1]
                image.composite(self.pixbuf,
                                dest.x, dest.y,
                                dest.width, dest.height,
                                xpos, ypos,
                                k, k,
                                GdkPixbuf.InterpType.NEAREST,
                                max(127, fabs(255 * sin(f * 2.0 * GLib.PI))) if (i & 1) else \
                                    max(127, fabs(255 * cos(f * 2.0 * GLib.PI))))

        da.queue_draw()

        return GLib.SOURCE_CONTINUE


def main():
    win = PixbufsWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()


if __name__ == "__main__":
    main()





代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanxiaochengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值