Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The spec requires me to display an image inside a Gtk2::Textview.

Sometimes the image must be displayed its original size. Sometimes it must be its original size, but surrounded by some empty 'padding'.

The script below is the closest I've been able to get so far. It uses Gtk2::Gdk::Pixbuf->scale to create the 'padding'.

However, the 'padding' is not empty space, as I'd hoped, but a blur of pixels from the edges of the original image.

Is there a simple way to remove the blur, or does this call for a different approach entirely?

#!/usr/bin/perl package scaleme; use strict; use diagnostics; use warnings; use Gtk2 '-init'; use Glib qw(TRUE FALSE); # Display this image my $path = 'change_this_directory/cat.bmp'; # Open a Gtk2 window with a Gtk2::TextView my $window = Gtk2::Window->new('toplevel'); $window->set_title('scaleme'); $window->set_position('center'); $window->set_default_size(800, 600); $window->signal_connect('delete-event' => sub { Gtk2->main_quit(); exit; }); my $frame = Gtk2::Frame->new(); $window->add($frame); my $scrollWin = Gtk2::ScrolledWindow->new(undef, undef); $frame->add($scrollWin); $scrollWin->set_policy('automatic', 'automatic'); $scrollWin->set_border_width(0); my $textView = Gtk2::TextView->new; $scrollWin->add_with_viewport($textView); if (-e $path) { # Display a photo of a cat face my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file($path); my $buffer = $textView->get_buffer(); $buffer->insert_pixbuf( $buffer->get_end_iter(), $pixbuf, ); # Make the overall image 20% bigger, but the cat's face is its # original size (and centred in the middle) my $factor = 0.2; my $w = $pixbuf->get_width(); my $h = $pixbuf->get_height(); my $pixbuf2 = Gtk2::Gdk::Pixbuf->new( 'GDK_COLORSPACE_RGB', FALSE, $pixbuf->get_bits_per_sample(), ($w * (1 + $factor)), ($h * (1 + $factor)), ); $pixbuf->scale( # $dest $pixbuf2, # $destx, $desty 0, 0, # $dest_width, $dest_height ($w * (1 + $factor)), ($h * (1 + $factor)), # $offset_x, $offset_y ($w * ($factor / 2)), ($h * ($factor / 2)), # $scale_x, $scale_y 1, 1, # $interp_type 'GDK_INTERP_NEAREST', ); $buffer->insert_pixbuf( $buffer->get_end_iter(), $pixbuf2, ); } $window->show_all(); Gtk2->main();

Replies are listed 'Best First'.
Re: Scaling an image with Gtk2::Gdk::Pixbuf->scale
by syphilis (Archbishop) on Oct 22, 2017 at 00:25 UTC
    Hi,
    If you don't get good assistance here, try the gtk-perl mailing list.

    I couldn't reproduce anything that matched your description of the problem.
    Instead, I got strange positioning and doubling up of the image - perhaps because the size of the image I was using was different to the one you were using.

    If you do contact the gtk-perl mailing list, I suggest attaching your cat.bmp so that they have exactly the same source image as you're using.
    And if you like to provide a link to that image, I'll grab it and try it out here .... but it's likely that I won't be able to help you much beyond establishing whether or not "it's the same for me" ;-)
    Providing a link to that image might also help others here provide some assistance.

    Cheers,
    Rob

      Thanks, I'll try the mailing list.

      By the way, I tried many different types of images, of different sizes, and got the same effect every time. I didn't notice any strange positioning or doubling up.

      (Looks like there are no Perl Monks Approves HTML tags for links, so I'll use code tags)

      https://imgur.com/a/iqe2t
        https://imgur.com/a/iqe2t

        Looking at that, I can see that I am getting the same as you. (My initial assertion to the contrary is probably just the result of not having properly assessed what you had written.)

        Cheers,
        Rob