Sep 18, 2009

Writing GIMP 2.6 Python plugins

The GIMP now has Python support in addition to TinyScheme, allowing you to harness the power of GIMP in Python. The following is a simple logo plugin written in Python, usable as a template to write more complex ones. The plugin writes a logo in the bottom-right corner of your image.

#!/usr/bin/python

from gimpfu import *

gettext.install("gimp20-python", gimp.locale_directory, unicode=True)

def mylogo(img, text, opacity, fontname, fontsize):
  gimp.context_push()
  color = (255,255,255)
  pdb.gimp_context_set_foreground(color)
  logo_layer = pdb.gimp_text_fontname(img, None, 0, 0, text, 0, 1,
   fontsize, POINTS, fontname)
  pdb.gimp_layer_set_opacity(logo_layer, opacity)
  x = img.width - logo_layer.width
  y = img.height - logo_layer.height
  pdb.gimp_layer_translate(logo_layer, x, y)
  gimp.context_pop()

register(
  "python-fu-mylogo",
  N_("Add logo to image"),
  "Adds a logo to the bottom-right corner of the image.",
  "", # author name
  "", # copyright owner
  "2009-09-18", # date
  N_("Add logo..."), # text to show in menu
  "RGB*, GRAY*", # image types we can work with
  # Positional parameter definitions, each entry in this format:
  # (TYPE, "name", "caption", default, ...)
  [
    (PF_IMAGE, "image", "Input image", None),
    (PF_STRING, "text", _("Text"), _(u"\xa92009")),
    (PF_SLIDER, "opacity", _("Opacity"), 50, (0,100,1)),
    (PF_FONT, "fontname", _("Font"), "Sans"),
    (PF_INT, "fontsize", _("Size"), 24),
  ],
  # Return values list
  [],
  mylogo, # function name
  # Where to add in menu structure
  # Special tag <Image> indicates you must have an open image
  menu="<Image>/Filters/Render",
  domain=("gimp20-python", gimp.locale_directory)
)
main()

Write ~/.gimp-2.6/plug-ins/mylogo.py and make sure it is executable, or GIMP will ignore it! After restarting GIMP and opening an image file, you will be able to invoke "Add logo..." from the Filters/Render submenu.

All the functions you can use to manipulate an image are in the "Procedural Database", accessible in Python as methods of the pdb object. The list of functions and their documentation can be found using procedure browser: Filters / Python-Fu / Console, click "Browse...". Just remember that the Python names have underscores instead of dashes as illustrated in the code above.

You will also find your own plugins there (search for "mylogo").

References:

May 28, 2009

Fixing yum for older Fedora installations

On an old Fedora system (say Core 5 or 6), you may encounter this problem:

# yum list kernel
Setting up repositories
Error: Cannot find a valid baseurl for repo: core

For releases that have reached EOL (end of life), this happens because their repositories are no longer available on the Fedora Public Active Mirrors system.

They are, however, still available in the archives. The solution is to update the yum repository configuration to use the archives instead of the now nonfunctional mirrorlist.

# cd /etc/yum.repos.d/
# ls
fedora-core.repo fedora-updates.repo fedora-extras.repo

There may be more files there, but those are the ones you need to change. Edit each of the above files and go to the first section, called [core], [updates] and [extras], respectively. You don't need to bother with the other sections, as they have "enabled=0" so they are not normally used. Comment out mirrorlist (and existing baseurl, if not already done) and add a new baseurl line, as shown below (the additions are shown in darker color):

fedora-core.repo
[core]
name=Fedora Core $releasever - $basearch
#baseurl=http://download.fedora.redhat.com/pub/fedora/linux/core/$releasever/$basearch/os/
#mirrorlist=http://fedora.redhat.com/download/mirrors/fedora-core-$releasever
baseurl=http://archives.fedoraproject.org/pub/archive/fedora/linux/core/$releasever/$basearch/os

fedora-updates.repo
[updates]
name=Fedora Core $releasever - $basearch - Updates
#baseurl=http://download.fedora.redhat.com/pub/fedora/linux/core/updates/$releasever/$basearch/
#mirrorlist=http://fedora.redhat.com/download/mirrors/updates-released-fc$releasever
baseurl=http://archives.fedoraproject.org/pub/archive/fedora/linux/core/updates/$releasever/$basearch/

fedora-extras.repo
[extras]
name=Fedora Extras $releasever - $basearch
#baseurl=http://download.fedora.redhat.com/pub/fedora/linux/extras/$releasever/$basearch/
#mirrorlist=http://fedora.redhat.com/download/mirrors/fedora-extras-$releasever
baseurl=http://archives.fedoraproject.org/pub/archive/fedora/linux/extras/$releasever/$basearch/


You may need to run "yum clean all" afterwards to remove the old data from the yum cache.

Mar 29, 2009

Aquarium CO2 Injection

All plants need CO2 (carbon dioxide). During photosynthesis, they break down CO2 into carbon, which they use to grow, and oxygen, which they release. In an aquarium the CO2 comes from fish respiration and a small amount mixes in from the air at the surface. This isn't usually enough for a densely planted tank or for high light plants which demand a lot of CO2 to be able to use the light. Without CO2 they wither and die or be overrun by algae.

There are two methods for adding CO2 to a tank. The DIY method involves using yeast to break down sugar and produce CO2 in a plastic bottle. This is cheap but its output cannot be controlled and is relatively high maintenance (need to replace sugar and yeast every couple of weeks).

The other way is to use a pressurized CO2 container. This requires little maintenance as you only need to refill the container maybe once a year, the rest is just occasional monitoring. The cost, around $250 in 2008 dollars. The parts you need for a fully automated system are the following:

CO2 Cylinder with Regulator AssyCO2 tank. Holds liquid CO2 at high pressure. A 5lb or 10lb tank is good enough (the weight refers to how much liquid CO2 they contain). An aluminum tank won't rust and is lighter. To (re)charge, find a welding gas supply store in your area and take it there. Cost to recharge is around $10-$15 for a 5lb container.

Regulator. Screwed into the tank (have some teflon tape on hand), is a manual valve that releases the gas little by little so it can be used, as you can't just dump it at the 1500psi tank pressure. You want one with two gauges, one measures the bottle pressure so you can tell when it's near empty, and the other the output pressure. A common regulator is the Milwaukee MA957 which comes with the solenoid and bubble counter as well.

MA957 Regulator AssemblySolenoid. It's an electric valve hooked to the regulator output, that turns CO2 on and off completely. It is usually triggered by the pH controller (see below).

Bubble counter. Visualization device, allows you to "measure" the amount of CO2 you are dumping in the tank. It is just a tiny bottle half-filled with water that the CO2 bubbles through. There's also a needle valve used to fine-tune the flow (but the main control is the regulator).

A few feet of CO2-safe tubing (CO2 makes some tubes brittle after a while)

Diffuser or "reactor", sits in the tank at the end of the CO2 tube, makes sure the gas is dissolved in the water and not wasted in the atmosphere. There are many kinds. You can also dump the CO2 in the filter intake. I use a small Aqua Medic ceramic CO2 diffuser that makes very fine bubbles that dissolve faster. The principle of most reactors is to make the bubbles run along a long path on their way up. You can see the bubble get smaller as the gas dissolves but some of it still escapes at the end.

SMS122 pH ControllerpH controller uses a probe in the tank to measure the pH level and turns the solenoid on and off to control the CO2 release into the tank. It also displays the current pH level. The controller is a critical part of the system, you need this because CO2 dissolved in water makes it acidic (lowers pH). At night plants stop using CO2 and the pH would otherwise drop and kill the fish. The Milwaukee SMS122 pH controller is a good option and works together with the MA957 regulator.

Notes

Measure the water's pH and hardness (kH and gH). For plants you want a relatively low pH, 7.0 or a bit lower if your fish can tolerate it. Do check against the pH range for all your fish. Change in small steps as fish do not tolerate large pH changes well. Don't just set the controller to 7 if your water is 8.

Plants won't grow well (or at all) in very hard water, even with a normal pH. Water should be softened a bit but try to avoid buffers (they alter the chemistry in a way that makes it difficult to measure things).

Set the regulator and needle valve to the lowest flow that keeps your pH in balance. This is better for the fish (avoids a quick pH change) and if the controller malfunctions it will give you more time to catch it before creating a dangerous pH level in the aquarium.

Try to avoid hang-on-tank filters or anything that agitates the water surface, the dissolved CO2 will be lost by dissipating into the atmosphere easier.

Safety notes

The tank contains gas under high pressure (1500psi or higher). Must be handled with caution. Do not let it get hot, that causes its pressure to rise and the safety valve all tanks are equipped with may break open and dump all the gas instantly. You do not want this to happen in your car while you're driving.

A smaller CO2 tank (5lbs) is easier to work with, easy to carry and is safer than the bigger tanks should all the gas be released in the room by accident. Secure the tank upright while in operation and ensure it cannot be toppled over, as the liquid CO2 will flow through and may cause damage to the regulator, other components or the fish tank. It can be transported in any position but when used it must sit upright.

Links, references: