Anyone good at converting/porting scripts from perl to python?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Anyone good at converting/porting scripts from perl to python?

"Rikard Johnels"
I have been trying to get Gimp Perl to run without success for a while now.
I have decided to try another way..
The script i want to run is as follows;

#!/usr/bin/perl

use Gimp qw( :auto );
use Gimp::Fu;

register        "center_guide",
                "Creates h- & v-guides at the center of the image.",
                "Physical center = width/2 and height/2; Optical center = the
Golden Mean.",
                "Claes G Lindblad <claesg\@algonet.se>",
                "Claes G Lindblad",
                "990323",
                "<Image>/Center Guide",
                "*",
                [
                [PF_RADIO,
                        "center",
                        "center",
                         0,
                        [Physical => 0, Optical => 1]
                ]
                ],
        sub {
                my ($img, $layer, $center) = @_;

                $w = $img->width();
                $h = $img->height();
                $hc = int($h/2 + 0.5);
                $vc = int($w/2 + 0.5);

                if ($center == 1) {
                        $hc = int(($h / 2.6179) + 0.5);
                };
                $bit_bucket = $img->add_hguide($hc);
                $bit_bucket = $img->add_vguide($vc);
                gimp_drawable_update($layer, 0, 0, $w, $h);
        };
exit main;



How do i convert it to python?
Python i DO have working...

(I did try to mail the author, but i got a bounced mail...)


--

         /Rikard

" Sharing knowledge is the most fundamental act of friendship.
Because it is a way you can give something without loosing something."
                -R. Stallman

---------------------------------------------------------------
Rikard Johnels          email   : [hidden email]
                        Mob     : +46 763 19 76 25
                        PGP     : 0x461CEE56
---------------------------------------------------------------
_______________________________________________
Gimp-user mailing list
[hidden email]
http://lists.xcf.berkeley.edu/mailman/listinfo/gimp-user
Reply | Threaded
Open this post in threaded view
|

Re: Anyone good at converting/porting scripts from perl to python?

Carol Spears
On Thu, Jun 02, 2005 at 03:39:30AM +0200, Rikard Johnels wrote:
> I have been trying to get Gimp Perl to run without success for a while now.
> I have decided to try another way..
> The script i want to run is as follows;
>
just rewrite it.

pyslice has code that uses guides.  you can search the gimps python
guide calls via the python console.

just rewrite it.

i have found scripting with python and gimp to be fun and rewarding and
a source of instant gratification.

ask specific questions here if you run into trouble.

i cannot imagine rewriting perl into python since i can read python and
cannot read perl.  how do you make something that is obfiscuated into
something that is not?  i say skip it and rewrite it, simply.

carol

_______________________________________________
Gimp-user mailing list
[hidden email]
http://lists.xcf.berkeley.edu/mailman/listinfo/gimp-user
Reply | Threaded
Open this post in threaded view
|

Re: Anyone good at converting/porting scripts from perl to python?

Joao S. O. Bueno Calligaris
In reply to this post by "Rikard Johnels"
On Wednesday 01 June 2005 22:39, Rikard Johnels wrote:
> I have been trying to get Gimp Perl to run without success for a
> while now. I have decided to try another way..
> The script i want to run is as follows;
>

I do not know much perl, but it is quite straightforward.
I suppose you know that in python identation does matter, and is used
to separate blocks, instead of {  &  }  or begin & end.

That said, the script bellow becomes:

(I will let the perl lines commented on the script. you may erase then
later)

#!/usr/bin/python

#use Gimp qw( :auto );
#use Gimp::Fu;
# I don't know what this "auto:" means,
#but we normally just import gimpfu in python scripts/

from gimpfu import *

#register        "center_guide",
# the register call in python must come after the function definition
#itself. Ok - this is  thasy perl way, in which the function was
#defined inside the funcion call for register. Python is  a clean
#language - the only way of doing this is with short functions
#that can be written as lambdas.

#        sub {
#                my ($img, $layer, $center) = @_;
def center_guide (img, layer, center):
     #beware of indentation from this point on

     #$w = $img->width();
     #$h = $img->height();
     #$hc = int($h/2 + 0.5);
     #$vc = int($w/2 + 0.5);

     w = img.width
     h = img.height
     hc = int (h / 2.0 + 0.5)
     vc = int (w / 2.0 + 0.5)

     #if ($center == 1) {
     #      $hc = int(($h / 2.6179) + 0.5);
     #    };
     if center == 1:
          hc = int (h / 2.6179 + 0.5)

     #          $bit_bucket = $img->add_hguide($hc);
     #          $bit_bucket = $img->add_vguide($vc);
     #          gimp_drawable_update($layer, 0, 0, $w, $h);
     img.add_hguide (hc)
     img.add_vguide (vc)
     gimp.displays_flush()

     #   };

register   ("center_guide",
            "Creates h- & v-guides at the center of the image.",
            "Physical center = width/2 and height/2; Optical center =
the Golden Mean.",
             "Claes G Lindblad <claesg\@algonet.se>",
             "Claes G Lindblad, pythonified by Joao S. O. Bueno
Calligaris",
              "990323",
              "<Image>/Center Guides",
              "*",
              [
                [PF_RADIO,
                        "center",
                        "center",
                         0,
                         (("Physical", 0), ("Optical", 1))
                ]
                     
              ],
              [],
              center_guide
              )  
               

#exit main;
main ()



>

>
> How do i convert it to python?
> Python i DO have working...
>
> (I did try to mail the author, but i got a bounced mail...)
_______________________________________________
Gimp-user mailing list
[hidden email]
http://lists.xcf.berkeley.edu/mailman/listinfo/gimp-user
Reply | Threaded
Open this post in threaded view
|

Re: Anyone good at converting/porting scripts from perl to python?

"Rikard Johnels"
On Thursday 02 June 2005 04.13, Joao S. O. Bueno Calligaris wrote:

> #!/usr/bin/python
>
> #use Gimp qw( :auto );
> #use Gimp::Fu;
> # I don't know what this "auto:" means,
> #but we normally just import gimpfu in python scripts/
>
> from gimpfu import *
>
> #register        "center_guide",
> # the register call in python must come after the function definition
> #itself. Ok - this is  thasy perl way, in which the function was
> #defined inside the funcion call for register. Python is  a clean
> #language - the only way of doing this is with short functions
> #that can be written as lambdas.
>
> #        sub {
> #                my ($img, $layer, $center) = @_;
> def center_guide (img, layer, center):
>      #beware of indentation from this point on
>
>      #$w = $img->width();
>      #$h = $img->height();
>      #$hc = int($h/2 + 0.5);
>      #$vc = int($w/2 + 0.5);
>
>      w = img.width
>      h = img.height
>      hc = int (h / 2.0 + 0.5)
>      vc = int (w / 2.0 + 0.5)
>
>      #if ($center == 1) {
>      #      $hc = int(($h / 2.6179) + 0.5);
>      #    };
>      if center == 1:
>           hc = int (h / 2.6179 + 0.5)
>
>      #          $bit_bucket = $img->add_hguide($hc);
>      #          $bit_bucket = $img->add_vguide($vc);
>      #          gimp_drawable_update($layer, 0, 0, $w, $h);
>      img.add_hguide (hc)
>      img.add_vguide (vc)
>      gimp.displays_flush()      
>
>      #   };
>
> register   ("center_guide",
>             "Creates h- & v-guides at the center of the image.",
>             "Physical center = width/2 and height/2; Optical center =
> the Golden Mean.",
>              "Claes G Lindblad <claesg\@algonet.se>",
>              "Claes G Lindblad, pythonified by Joao S. O. Bueno
> Calligaris",
>               "990323",
>               "<Image>/Center Guides",
>               "*",
>               [
>                 [PF_RADIO,
>                         "center",
>                         "center",
>                          0,
>                          (("Physical", 0), ("Optical", 1))
>                 ]
>                      
>               ],
>               [],
>               center_guide
>               )  
>                
>
> #exit main;
> main ()

Very nice! Thank you Very much!

Works as expected. (almost)
The image is flagged edited if i use the guides.
How do i set something so it isn't edited? (Its just a guide, no change in the
image)
Also, i was thinking of extending the script to use a choice "Thirds" too.
(the rule of thirds), but i failed to get the hang of how to use the if
statement...

I am not much of a programmer (as i have stated before), so i need all the
"babytalk help" i can get...

I do understand more and more for each time.. Maybe in time when i get my new
SGI machine ill be able to write my own code...

--

         /Rikard

" Sharing knowledge is the most fundamental act of friendship.
Because it is a way you can give something without loosing something."
                -R. Stallman

---------------------------------------------------------------
Rikard Johnels          email   : [hidden email]
                        Mob     : +46 763 19 76 25
                        PGP     : 0x461CEE56
---------------------------------------------------------------
_______________________________________________
Gimp-user mailing list
[hidden email]
http://lists.xcf.berkeley.edu/mailman/listinfo/gimp-user
Reply | Threaded
Open this post in threaded view
|

Re: Anyone good at converting/porting scripts from perl to python?

Sven Neumann
Hi,

Rikard Johnels <[hidden email]> writes:

> Works as expected. (almost) The image is flagged edited if i use the
> guides.  How do i set something so it isn't edited? (Its just a
> guide, no change in the image)

Guides are part of the image and stored in XCF. So that is the correct
behaviour. gimp-image-clean-all will unset the flag if you insist on
violating the rules.


Sven
_______________________________________________
Gimp-user mailing list
[hidden email]
http://lists.xcf.berkeley.edu/mailman/listinfo/gimp-user
Reply | Threaded
Open this post in threaded view
|

Re: Anyone good at converting/porting scripts from perl to python?

Joao S. O. Bueno Calligaris
In reply to this post by "Rikard Johnels"
On Thursday 02 June 2005 03:25, Rikard Johnels wrote:
> On Thursday 02 June 2005 04.13, Joao S. O. Bueno Calligaris wrote:
>
> Very nice! Thank you Very much!
>
> Works as expected. (almost)
> The image is flagged edited if i use the guides.
> How do i set something so it isn't edited? (Its just a guide, no
> change in the image)
Sorry - for the GIMP that is changing the image.There is no way to
clean the dirty flag, but saving the image.
If it is that important, one workaround would be to save the image
to /dev/null  - but it is a lot of hasle.


> Also, i was thinking of extending the script to use a choice
> "Thirds" too. (the rule of thirds), but i failed to get the hang of
> how to use the if statement...
Just below the line that calculates the "golden mean" insert a
    elif center==2:
        hc = h / 3
        hv = w / 3
and a corresponding option pair in the "register" function

>
> I am not much of a programmer (as i have stated before), so i need
> all the "babytalk help" i can get...

I'd recomend a visit to www.python.org and to the python tutorial
therein. It wll take you from 20 to 30 minutes, and you will be able
to read the scripts later, and top go back to the python reference to
write your own stuff.

For gimp scripts, there are no changes, but the call to the register
function, and the ability to use any of the hundreds of gimp
functions displayed on the GIMP's PDB browser.

>
> I do understand more and more for each time.. Maybe in time when i
> get my new SGI machine ill be able to write my own code...
Heh....

Regards,

        JS
        -><-
_______________________________________________
Gimp-user mailing list
[hidden email]
http://lists.xcf.berkeley.edu/mailman/listinfo/gimp-user
Reply | Threaded
Open this post in threaded view
|

Re: Anyone good at converting/porting scripts from perl to python?

Joao S. O. Bueno Calligaris
In reply to this post by Sven Neumann
On Thursday 02 June 2005 06:51, Sven Neumann wrote:

> Hi,
>
> Rikard Johnels <[hidden email]> writes:
> > Works as expected. (almost) The image is flagged edited if i use
> > the guides.  How do i set something so it isn't edited? (Its just
> > a guide, no change in the image)
>
> Guides are part of the image and stored in XCF. So that is the
> correct behaviour. gimp-image-clean-all will unset the flag if you
> insist on violating the rules.
>

Sorry Rikard.
Just insert a:
        pdb.gimp_image_clean_all (img)  
call just after the add_vguide line.
>
> Sven
> _______________________________________________
> Gimp-user mailing list
> [hidden email]
> http://lists.xcf.berkeley.edu/mailman/listinfo/gimp-user
_______________________________________________
Gimp-user mailing list
[hidden email]
http://lists.xcf.berkeley.edu/mailman/listinfo/gimp-user
Reply | Threaded
Open this post in threaded view
|

Re: Anyone good at converting/porting scripts from perl to python?

Bugzilla from horkana@maths.tcd.ie
In reply to this post by "Rikard Johnels"

On Thu, 2 Jun 2005, Rikard Johnels wrote:

> Date: Thu, 2 Jun 2005 03:39:30 +0200
> From: Rikard Johnels <[hidden email]>
> To: [hidden email]
> Subject: [Gimp-user] Anyone good at converting/porting scripts from perl
>     to python?
>
> I have been trying to get Gimp Perl to run without success for a while now.
> I have decided to try another way..


> The script i want to run is as follows;

> register        "center_guide",
>                 "Creates h- & v-guides at the center of the image.",
>                 "Physical center = width/2 and height/2; Optical center = the
> Golden Mean.",


> How do i convert it to python?
> Python i DO have working...
>
> (I did try to mail the author, but i got a bounced mail...)

I wrote a few scripts for manipulating guides using scheme (one to add a
guide at specified pixel position and another to add guides based on a
percentage of the image size, some of them are included in Gimp 2.2 see
Image, Guides, ...).  Maybe if you are repeating the same task a lot these
are to generalised?

I suppose could rewrite the Centre Guides script in Python (nevermind,
Jaoa has done it already).  Thing is I would have rewritten it i Scheme
already except that I was not convinced anyone would actually want to use
this script.

I would be very interested to know why exactly is the centre guides script
useful?
More importantly, what bigger problem are you trying to solve?

You mention you are also interested in extending the script to divide the
image into thirds, which makes me seriously think there is something
bigger going on you aren't telling us about.

With more information we might be able to offer an even better answer, and
solve the bigger problem rather than only treating the symptoms.

Sincerely

Alan Horkan

Inkscape http://inkscape.org
Abiword http://www.abisource.com
Dia http://gnome.org/projects/dia/
Open Clip Art http://OpenClipArt.org

Alan's Diary http://advogato.org/person/AlanHorkan/

_______________________________________________
Gimp-user mailing list
[hidden email]
http://lists.xcf.berkeley.edu/mailman/listinfo/gimp-user