Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help.Wanted('Pixel region gimpPy')
Hi. Having a rough time understanding have to manipulate individual pixel's trough gimp python.
So far i have found this constructor drawable.get_pixel_rgn(x, y, w, h, [dirty, [shadow]) an been able to set a variable to it and call member from sayd variable but that about it.
Found a Gnome site with descriptive information.
Did try to install numpy into gimp with no luck. But don't think i need that module for now.
What my end goal comprise of is to be able to edit individual pixel RGB values along side it's alpha/opacity.
A little information on have to set rgb if that is with a 3-tuple or an hue int, with how/were opacity is set would be nice also Smile
Seen ?
Did read it, but did not understand where to install pip or numpy relevant to gimp. I have pip installed on python but pretty sure that is not the gimp's python, but the general one.
I run ubuntu desktop.
Did try "sudo apt install python numpy" from home directory with no luck.
Added the script to plug-ins folder with no luck. ~Know i need the numpy module first.

The post that makes most sense of what should be done, but this is beyond what i understand. Also seam to be for windows
Also on (K)Ubuntu desktop. If you installed Gimp from a .deb, then it uses the standard Python runtime from your desktop and a general pip install numpy (system level with sudo or user level) should work. If you installed a snap or a flatpak, then you may have to install numpy for your specific Python runtime (assuming one has been packaged).

Starting Gimp in a terminal makes all the error output appear in the terminal. Very helpful to figure out what went wrong.

Don't forget to make the Python script executable...
Hi. I have installed gimp through 'Ubuntu software center', guesses that is a snap install
Gimp version is 2.10.18
Python version for ubuntu: Python 2.7.17
Python version for gimp: 2.7.?

When running gimp through terminal the out put is:

Quote:ln: failed to create symbolic link '/home/userName/snap/gimp/252/.config/gtk-2.0/gtkfilechooser.ini': File exists
/snap/gimp/252/usr/bin/gimp: Gimp-Widgets-WARNING: parse_iso_codes: error parsing '/build/gimp/parts/gimp/install/usr/share/xml/iso-codes/iso_639.xml': No such file or directory

Traceback (most recent call last):
  File "/home/userName/snap/gimp/252/.config/GIMP/2.10/plug-ins/", line 7, in <module>
    import numpy as np
ImportError: No module named numpy
/snap/gimp/252/usr/bin/gimp: LibGimpBase-WARNING: gimp: gimp_wire_read(): error

Found a .deb package for Gimp 2.8.22 and tryed to install it via aptitude, but it was not found:
    sudo aptitude install gimp_2.8.22-1_amd64.deb #Unsuccessful
Runned this command from terminal and installed numpy. Which is a weird because i had already done that. But :
    sudo -H pip install numpy
    Terminal~:Successfully installed numpy-1.16.6

Would be very helpful if i should get a bit assistance installing gimp.deb that work with the rest of softwares or adding numpy to gimp environment.
Thank in advance.
Simplest solution if you are on a reasonably recent Ubuntu (18.04 or later) is add this PPA to you software sources (fimm instructions on the page).

Then you have a full-featured Gimp that is normally integrated with your system (uses the standard Python environment). You can also installa a couple of other useful packages from there( GMIC, gimp-plugin-registry (resynthesize...)).
Quote:...Would be very helpful if i should get a bit assistance installing gimp.deb that work with the rest of softwares ...

Always useful to check the distro version in a console use
uname -a

The package you downloaded is for bionic Are you using bionic (Ubuntu 18.04) ? The default Gimp for bionic is already Gimp 2.8.22 and you would install it
sudo apt install gimp

Installing local packages such as gimp_2.8.22-1_amd64.deb Use dpkg but you will need all the other gimp packages; gimp-data , libgimp2.0 libgegl-0.3 (I think)
With all the packages in a folder, run in a terminal. Output will tell you if anything is missing. Dodgy because you can (will) end up with broken packages.
sudo dpkg  -i  *.deb

To be honest, I use kubuntu 18.04 and the first thing I installed was the old package manager synaptic and generally install via that.

For Gimp 2.10 as Ofnuts post, use the PPA The instructions are top of the web page. You might need to first install 'add-apt-repository' then it is straight forward.

For some screenshots (out of date, but still applicable what ever 'buntu / Gimp version) see:
Thanks for replays. How now deleted snap gimp and installed apt gimp, did use 'sudo apt install gimp' For me using gimp 2.8 is fine.
Now able to import numpy without an error message and got numpyPlug-in working. My Ubuntu version is 18.04.4
Did look at the code of this plug-in to try and replicate how one goes about editing a pixel with no luck. Are getting an error message TypeError: string is wrong length.
dir() or help() was not able to support me further.
Getting closer to the end goal whoho, and learn allot thanks to you guys  Big Grin
Now my questions are:
  • What is the structure to set one pixel ie. pixelRegion[1,1] = ?
  • Can alpha be set along color (hue) in rp[1,1] ?
  • Is it correct that rp[1,1] takes a string ?
Forgetting numpy for a while...

The data to set a pixel in the pixel region is exactly the same as the one you get from the pixel region. Indexing a pixel region returns a string, but it's best converted to a bytearray. For instance, assuming a blue pixel at (0,0):

>>> region[0,0]
>>> pixel=bytearray(region[0,0])
>>> pixel

To alter the pixel, for instance to set its green channel:

>>> pixel[1]=255
>>> region[0,0]=str(pixel)

You can also set values from scratch, a half-transparent yellow pixel:

>>> region[0,0]=str(bytearray([255,255,0,127]))

Looking at the results, don't trust your eyes too much, there are obviously display refresh issues (the layer looks unchanged, but if slightly alter its opacity, is suddenly displays the changes...).

This said, iterating pixels is slow, a simple image has nearly a million pixels. The big advantage of numpy is that the operations are performed on the whole array with machine code. The median filter example is about twice as fast as the median filter in GMIC.
Hi. Thank you two i finally managed to edit pixels a and with half a day create a little plugin  Big Grin
Supper helpful that you gave a pointer to run the gimp through terminal for error code. Getting the gimp to work at 'core' level moves my core too, orchestra code is the best Cool 
Bellow is my master piece. If you would like to try it, try it at different image widths like 180, 360 361, 60 for weird effect. The hue is based on an number that goes up to 360 and start at zero again. Remember to use an alpha layered image. Smile

#!/usr/bin/env python

from gimpfu import *

def FindColor(hue, opac):
   if hue < 60:
       return list((255, int(255*(hue/60.0)), 0, opac))
   elif hue >= 60 and hue < 120:
       return list((255-int(255*((hue%60)/60.0)), 255, 0, opac))
   elif hue >= 120 and hue < 180:
       return list((0, 255, int(255*((hue%60)/60.0)), opac))
   elif hue >= 180 and hue < 240:
       return list((0, 255-int(255*((hue%60)/60.0)), 255, opac))
   elif hue >= 240 and hue < 300:
       return list((int(255*((hue%60)/60.0)), 0, 255, opac))
   elif hue >= 300:
       return list((255, 0, 255-int(255*((hue%60)/60.0)), opac))

def hueGrade(image, drawable, alphaVal):
   gimp.progress_init("Hue chart'n " + + "...")

   srcRgn = drawable.get_pixel_rgn(0, 0, image.width, image.height, True, False)

   y = image.width*image.height
   for x in range(y):
       srcRgn[(int(x%image.width), int(x/image.width))] = str(bytearray(FindColor(x%360, int(alphaVal))))
       gimp.progress_update(1.0 * x / y)


   "Takes an RGBA and produces a hue that covers the whole image",
   "Goes along width then down generating a hue based on steped pixel sum; Hue goes from 0 to 359 then repeats",
   "FloppaDisk", "FloppaDisk", "2020",
   "Hue chart",
   "RGBA", # type of image it works on (*, RGB, RGB*, RGBA, GRAY etc...)
       (PF_IMAGE, "image", "takes current image", None)
       , (PF_DRAWABLE, "drawable", "Input layer", None)
       , (PF_SLIDER, "alphaVal", "Alpha value", 255, [0, 255, 10])
   hueGrade, menu="<Image>/Filters/Extra")  # second item is menu location


Forum Jump: