Shallow Thoughts
Akkana's Musings on Open Source, Science, and Nature.
Sat, 16 Aug 2008
Last night Joao and I were on IRC helping someone who was learning
to write gimp plug-ins. We got to talking about pixel operations and
how to do them in Python. I offered my arclayer.py as an example of
using pixel regions in gimp, but added that C is a lot faster for
pixel operations. I wondered if reading directly from the tiles
(then writing to a pixel region) might be faster.
But Joao knew a still faster way. As I understand it, one major reason
Python is slow at pixel region operations compared to a C plug-in is
that Python only writes to the region one pixel at a time, while C can
write batches of pixels by row, column, etc. But it turns out you
can grab a whole pixel region into a Python array, manipulate it as
an array then write the whole array back to the region. He thought
this would probably be quite a bit faster than writing to the pixel
region for every pixel.
He showed me how to change the arclayer.py code to use arrays,
and I tried it on a few test layers. Was it faster?
I made a test I knew would take a long time in arclayer,
a line of text about 1500 pixels wide. Tested it in the old arclayer;
it took just over a minute to calculate the arc. Then I tried Joao's
array version: timing with my wristwatch stopwatch, I call it about
1.7 seconds. Wow! That might be faster than the C version.
The updated, fast version (0.3) of arclayer.py is on my
arclayer page.
If you just want the trick to using arrays, here it is:
from array import array
[ ... setting up ... ]
# initialize the regions and get their contents into arrays:
srcRgn = layer.get_pixel_rgn(0, 0, srcWidth, srcHeight,
False, False)
src_pixels = array("B", srcRgn[0:srcWidth, 0:srcHeight])
dstRgn = destDrawable.get_pixel_rgn(0, 0, newWidth, newHeight,
True, True)
p_size = len(srcRgn[0,0])
dest_pixels = array("B", "\x00" * (newWidth * newHeight * p_size))
[ ... then inside the loop over x and y ... ]
src_pos = (x + srcWidth * y) * p_size
dest_pos = (newx + newWidth * newy) * p_size
newval = src_pixels[src_pos: src_pos + p_size]
dest_pixels[dest_pos : dest_pos + p_size] = newval
[ ... when the loop is all finished ... ]
# Copy the whole array back to the pixel region:
dstRgn[0:newWidth, 0:newHeight] = dest_pixels.tostring()
Good stuff!
Tags: gimp, python, programming, performance
[
21:02 Aug 16, 2008
More gimp |
permalink to this entry
]
Sat, 12 Apr 2008
I've been helping out with an extracurricular GIMP class that a
local Linux and free software advocate, Christian Einfeldt,
has organized at a middle school in San Francisco.
The class meets on a Saturday once or twice a month, so there's plenty
of time to forget things between sessions, and most of the kids don't
have a lot of prior computer experience (I'm told many of them are
behavior problems or otherwise "at risk", but I sure wouldn't have
guessed that from their exemplary behavior in class.)
Despite the obstacles, the kids have
learned some impressive image editing skills in a very short time!
Lots of them have figured out how to set their Edubuntu desktop background;
I've seen abstract patterns, photographs decorated in various ways
(today one girl was painting a mouth, hair and jewelry on a photograph
of a chimpanzee's face, and it came out looking very funny), photos
of the students themselves pasted into exotic locales, and so on.
It's also an interesting exercise for me in seeing what
beginning users find difficult to understand and what aspects
of GIMP's user interface are difficult to explain.
An additional challenge is that this classroom
has no projector or centrally visible screen. So you can't just
demonstrate how something works; everything must be explained slowly
in words while the students follow along with each step, and then
we have to go through the room helping students as they try to
remember the steps.
One of the first tasks they take on is combining images: start with
a photo of themselves, or of an animal or car, select it and paste it
into another image. What's the easiest way of explaining selection of
arbitrary shapes?
Which method can be explained in less than a minute, and yet they'll
remember how to do it after you leave and move on to the next student?
There are three obvious candidates for a general-purpose selection
tool: the intelligent scissors, the paths tool, and the quickmask.
We had a miscommunication in one of the early classes and didn't
discuss which technique to teach, so I taught some students the
paths tool while Christian was teaching others the iscissors.
I found that both methods had some serious problems.
With Bezier paths, it's easy to click points around your object.
Students get a little flustered the first few times they accidentally
drag rather than click and drag handles appear, but they can get over
that. The part that's difficult comes at the end, where they have to
click Path to Selection, then Feather as a separate step (they don't
need to feather the first time, but eventually they'll need it).
And then there's the problem that the path as well as the selection
remains visible, a distraction that they don't understand.
When I saw that Christian had been teaching some students the
iscissors while I was teaching others paths, I thought, gee, good
idea. Iscissors should be more straightforward, no? Well, no, as it
turns out. New students have great difficulty making an iscissors
selection. They're fine as long as they're clicking their points; the
problem comes when they get to the last point, when in order to make a
selection you must click carefully on your first point, then click
again inside the figure. A lot of students don't understand this no
matter how many times you explain: they don't remember which was their
first point (it doesn't look any different from the others), they
can't see it anyway (it usually doesn't contrast much with the image),
and they can't tell whether they clicked it successfully.
At that point they try to click inside the image and get a spurious
extra point -- and then they
panic and start clicking all over the place, ending up with a mess
that is (as far as I've been able to tell) unrecoverable. The only
fix is to toss out that figure and start over, but even that isn't
easy to do (click on another tool then back on the iscissors tool
button). Basically, the iscissors tool is far too confusing and most
students need to be personally walked through it at least three times
(some of them a lot more than that) before they get it.
Anyone who's read my writing on GIMP probably knows that I'm a
quickmask zealot. I'm a born again quickmask prophet: I used GIMP for
years without really understanding the quickmask, and when I finally
grokked it, it made a huge difference in ease of selection.
I sometimes joke that "the quickmask changed my life", and that's
hyperbole, or course; but it sure did change my GIMP editing.
People seem to fear the quickmask so I usually don't present it
first, but maybe I should. These students are very eager and competent
at painting, and I think they'd take to the quickmask very easily
with far fewer stumbles than the other two methods have given them.
There's one other variant of shaped selection that I didn't list:
the lasso tool in add and subtract mode. The lasso tool is
terrifically hard to use to try to select a whole figure from
an image. You'd have to have a preternaturally steady hand, plus
you can't zoom in and scroll around since the whole figure has to
be completed in one movement. But what you can do is make a rough
selection with the lasso, understanding that you'll have some errors;
then alternate between Add mode and Subtract mode as you use the lasso
on smaller areas to get the selection just right. It's nearly as easy
as the quickmask, and doesn't require a big conceptual shift. The only
reason I'm leery is that I suspect the three modes would confuse a lot
of students -- especially since the mode buttons have no labels,
merely tooltips.
While I'm on the topic, there's another issue that gives the students
trouble besides selection: the floating selection that results from a
paste. There's really no way to explain to a schoolkid why it's there
(heck, maybe some day someone will explain that to me).
And it's useless to try to get them to keep their Layers dialogs
visible. (They don't even keep the toolbox visible most of the time;
it's always covered by image windows. Most of these Edubuntu machines
are working at 800x600 resolution, and there just isn't room on the
screen for the normal GIMP window collection.)
So I try to drill them that "Every time you paste, you have to find
the Layers window and click that button on the bottom left."
Understandably, they often forget that step, then get into trouble because
they can't see all their pasted layer, or some functions are greyed out.
Aside from selection and paste, the students seem to cope with GIMP
remarkably well. Some of them have been exploring the menus for fun
plug-ins, others are trying different patterns to make interesting
backgrounds, and one even discovered how to make interesting effects
with some of the specialized gradients. At the beginning I wondered
if teaching GIMP might not be too ambitious, and maybe something
simple like Tux Paint might be better. But GIMP is working out just
fine except for those few stumbling blocks.
The kids have a refreshing willingness to explore and try things, and
the result is a whole lot of really fun images.
Tags: gimp, education, user interface
[
22:44 Apr 12, 2008
More gimp |
permalink to this entry
]
Tue, 26 Feb 2008
I've wished forever that GIMP could open files and URLs as easily as
Mozilla (and Netscape before it) does by selecting the filename in
another app then middleclicking to "paste" in the toolbox.
(Note: in Mozilla this is controlled by the
middlemouse.contentLoadURL preference, and Ubuntu users have
to enable it explicitly.)
Well, it turns out GIMP has that feature too, and has had it for a
long time. The reason it had never worked for me is that it only
works if you click on one (any) of the tool buttons. I was clicking
in empty areas of the toolbox window, because it feels weird to
click over a button when I don't mean to use that tool.
Now that I know to middleclick on a tool button, middlemouse open
works great for Unix paths, file: URLs and even remote URLs
(assuming you have Open URL working, of course, which on some
systems may require installing gimp-libcurl or gimp-gnomevfs).
Nice! That'll save me some gimp-remote calls.
Tags: gimp
[
15:21 Feb 26, 2008
More gimp |
permalink to this entry
]
Sun, 17 Feb 2008
There's been some discussion on the gimp-developer list about that
unwieldy layer mode option menu you see in both the Layers dialog and
in drawing tool options.
Bill
introduced the topic
by suggesting a redesign of the menu to use
two side-by-side columns instead of one. That makes the menu more
compact and vastly shortens the average mouse movement needed to
change modes.
But Sven didn't like the side by side option, pointing out that it
implies some equivalence to modes that end up listed next to each other.
More discussion ensued, with Bill posting a
screenshot of the unwieldy
menu to illustrate how bad it is
(including the bizarre gtk "the top half of the menu is blank"
misfeature that always looks like a bug but is apparently intentional).
That Mode menu has always bothered me.
Typically when I'm using layer modes, I try lots of them
one by one to see which mode works best.
But that's difficult with the current very tall menu, especially
(as Bill pointed out on IRC)
if you need to jump back and forth between two modes that aren't close
to each other in the menu. And gtk option menu's behavior doesn't help,
where clicking on it pops up the menu but not necessarily with the current
item selected -- sometimes the previous item is selected, so you can't
just arrow down once and assume you'll get the next mode.
That night after going to bed I got to thinking about it.
I realized that the Mode menu problem was similar to the problem
selecting a font from the combo box in the Text tool options --
I usually find it much easier to bring up the Fonts dialog and choose
a font from there. What I really wanted for layer modes was a
"Modes dialog".
And suddenly it came to me that I
could solve most of my problem with a simple "Next mode" script.
Once I had that, I could bind it to a key, or "tear off" the menu it
was in so that it would stay visible and I could click it repeatedly.
It took about ten minutes the following morning to write the script
in python.
Cool!
I posted my solution back to the list, and some discussion ensued on
IRC. Bill pointed out that enabling tear-offs for the existing
Mode option menu (which can be done in two lines of C code)
gives essentially the Fonts dialog I wanted.
Several of us thought that was a great idea.
But when Bill posted to the list, Sven nixed the idea, saying
tear-offs were deprecated. (They're not officially deprecated in GTK,
or at least the GTK documentation doesn't say so and I can't find
anything with google; but in any case Sven apparently doesn't like
tear-offs and won't allow adding any new ones in GIMP.)
Fortunately, gimp-python comes to the rescue here too. Writing a
mode-dialog.py
turned out to be a little trickier than "next mode",
only because it took me a while to realize I needed to call
pdb.gimp_displays_flush() to update the display after
changing the mode of the current layer (thanks Alexia and Bill).
So now I have both "next mode" and a separate mode dialog,
making layer mode operations so much easier!
Tags: gimp
[
11:50 Feb 17, 2008
More gimp |
permalink to this entry
]
Sun, 21 Oct 2007
I ran out of business cards (where do they go? I never seem to find
occasion to give any out. They make good bookmarks, though) and
wanted to print some more. I use gLabels for low-res label printing,
but it prints poorly (or used to, anyway) so when I want something
with crisp and sharp images, I use GIMP with my
GIMP Labels
script.
That's all fine, except that the "make label page" part of the script
scales the labels to fit on a typical Avery letter-sized template --
and the Gutenprint drivers for my printer can't actually fill a
letter sized page. (I have the choice of normal printing, in which
it fills about 97% of the page, or Borderless printing, where it
slops way over the edges.)
The solution is to crop a little extra off the outside edge of the
label page. So I added some code to script-fu-rect-label-page to
keep a "Printer fudge factor" and crop the page at the end.
An easy tweak which seems to work fine, and with any luck it'll
cure a lot of the misalignment problems I've seen with labels.
While I was making changes anyway, I added some clearer installation
instructions for the 95% case of someone who just wants the script
with the labels I've included, since I recently heard from someone who
wasn't clear on where to install the script.
Tags: gimp
[
12:03 Oct 21, 2007
More gimp |
permalink to this entry
]
Thu, 23 Nov 2006
People keep showing up on GIMP's IRC channels and asking about
JPEG 2000 support. There was a
Summer
of Code "Wavelet" project last summer which implemented JPEG 2000 support
(among other things) but no one ever seems to know where to find it.
So I asked Simon, who was the mentor for that project. Turns out
he had tarballs, but didn't think it was appropriate to link them
from his own site, especially as they were somewhat tricky to
compile.
The consensus among the developers was that it would be nice to
have jp2 and the other wavelet plug-ins available on the
GIMP Plug-in Registry.
So I fiddled with them, fixed a couple of Makefile issues,
added READMEs with author credit and building instructions,
and uploaded them to the registry. The plug-ins are:
- jp2
- JPEG 2000 support
- denoise
- A noise removal plug-in
- ihalf
- Inverse halftoning -- remove halftones from printed images.
I haven't actually used these plug-ins (couldn't find a JPEG2k file
to test) -- I just wanted to help
make them available for people who need them.
While I was at it I updated my vastly out-of-date registry
entries for Pandora and added an entry for my label/business card
script.
Tags: gimp
[
12:54 Nov 23, 2006
More gimp |
permalink to this entry
]
Tue, 15 Aug 2006
"Favicons" are those little icons you see to the left of the URLbar
in a browser, and for each site in the bookmarks menu or toolbar.
They're just a file named
favicon.ico in the top level of
a web site, and they're a nice addition to a site.
(More details in the
Wikipedia entry.)
I'd made a few favicons in the distant past by creating a
32x32 image, saving it as ppm, then using ppmtowinicon.
But when I tried it in GIMP recently, I ran into trouble.
GIMP can save ICO files: Save As, click Select
File Type and choose "Microsoft Windows icon (ico)".
That gets you a dialog where you have to choose a color depth and
palette. I tried different settings, but the resulting images
never showed up properly in Firefox.
But then I tried saving as ppm and using ppmtowinicon and
that no longer worked either. Argh! What's up?
The silly answer, it turns out, is that it had nothing to do
with how GIMP was saving the images. The problem was that Firefox
caches favicons, and shift-reload or Clear Cache doesn't help.
When you're testing a new favicon, you have to load the url
for the favicon.ico itself (and reload it if necessary).
Success at last! It even handles transparency, so you can
make shaped favicons that show up nicely against a tab, menu
or toolbar background.
Of course, editing a 32x32 pixel image is a fun exercise in itself.
I recommend using a second view (View->New View).
Expand one view a lot (800x works well) so you can edit individual
pixels, while the other view remains at normal size so you can see
your final icon as others will see it in the browser.
Tags: gimp
[
10:57 Aug 15, 2006
More gimp |
permalink to this entry
]
Tue, 18 Jul 2006
I got slashdotted yesterday -- a positive
Slashdot
book review of Beginning GIMP. Hooray!
The comments were mostly the usual mix of flames from
Photoshop users saying "GIMP sucks because it isn't like Photoshop",
with hardly anything about the book; no surprise there.
I don't know why Photoshop users seem so compelled to attack
the GIMP, but obviously they do since this happens so often.
They don't seem to be willing to accept "Some people like one
style of user interface, other people like another style, and
a highly complex application like GIMP or Photoshop is going
to take some time to learn no matter how it's designed."
Slashdot linked to Barnes
& Noble rather than Amazon, which is understandable
since BN has a killer 40% off sale in progress on Apress books
(they have
at $26.99 for BN members,
$29.99 otherwise).
Apress also pointed me toward a couple other reviews that I hadn't
seen yet, so I created a review
page to link to them.
Tags: gimp
[
17:37 Jul 18, 2006
More gimp |
permalink to this entry
]