Showing posts with label imagemagick. Show all posts
Showing posts with label imagemagick. Show all posts

Monday, August 31, 2009

Ruby: Playing with Colour

Had an idea for a new twitter background.

I wrote it in ruby, using the rmagick API wrapper for ImageMagick.

Pretty straight forward:

  • make a big black background,

  • create a lot of random coloured squares and write the colour on them while you're at it,

  • write to jpeg file


Here's the code.

require 'rubygems'
require 'RMagick'
include Magick

buffer = Magick::Image.new(1700,1040) { self.background_color = "#000000" }

x = 10
y = 10
tone = [ '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ]

srand 1234
1118.times do
puts '* '+x.to_s+' '+y.to_s
red = rand(16)
green = rand(16)
blue = rand(16)
ligthness = red+green+blue
co = '#'+tone[red]+tone[green]+tone[blue]
patchObj = Magick::Draw.new
patchObj.fill = co
patchObj.polygon(x,y,x,y+30,x+30,y+30,x+30,y)
patchObj.matte(3,3,PaintMethod::ReplaceMethod)
patchObj.draw(buffer)
textObj = Magick::Draw.new
textObj.font = './fonts/Ghoul.ttf'
textObj.pointsize=12
if ligthness > 25 || green > 11
textObj.fill = '#000'
else
textObj.fill = '#fff'
end
textObj.text(x, y+12, co)
textObj.draw(buffer)
x = x+40
if x > 1690
x = 10
y = y+40
end
end

buffer.display
buffer.write("random_colour.jpg")

exit

Enjoy.

#ToDo: use a resource pool so don't have the cost of creating over 2K Draw objects.

Sunday, September 30, 2007

Ruby: First hack

Thursday night, last week, I released some static web pages. That’s nothing special, but the code that built them is, for me.

It’s me first ruby hack. It’s a flikr like doodad.

What it do?

It takes a cd of images (mainly bodybuilding), straight from the developers (I still use an analogue camera, just, but that’s another story). It crops, brands them and then builds a cluster of static web pages around them.

Using Image Magick, via rmagick, I manipulate the images. The images get rotate (if needed), cropped and branded. The data to do this is stored in a mysql data base. Each derived image is also tagged with data, so they can be captioned and grouped later. It is stored in three tables.

Next there is web page formatting. I used staticmatic for this. It based on HAML and SASS.

The formatting data is laid out as a node/leaf arrangement, also in the mysql database. Currently there is only one type of node. That is the root node type of page. All the leaf nodes types currently sit directly connected to the page node. The valid leaf types are navigation, photo grid and album. Currently there is no recursion up a node structure. That’s a future possibility.

That’s two tables, one for node and its one type, page. Not YAGNI (Your Aren’t Going to Need It), but then there is Agile Modelling. There are for leaf tables, Leaf and its three types. Six tables all up.

I’m sure you can guess what navigation and photo grid do.

Album is the complex stuff and the reason I wasted an automated process.

The album is a page cluster. On the page that has the album leaf, you get the results of the specific sports event. The event has divisions. Divisions have competitors, who appear in placing order. If a division or competitor has any photos with their tag, then it becomes a link to a generated page that contains a photo grid with all their tagged photos. The four tables that contain this data are event, division, competitor and place.

Now there is currently only one type of album. If I want an additional album structure for a different event thingy, I’ll refactor. The cluster generating methods would be moved into a command pattern that could be changed by Dependence Injection (DI). But YAGNI says that’s in some tomorrow.

What would I do differently, use rspec from the start.

Well that my first use of ruby in anger.


Gnoll110

Technorati tags:

Monday, July 02, 2007

Ruby: unable to extend cache

Over the weekend I used Ruby in anger for the first time.

The project was to extract JPEG images from a CD of photos I just got back from the lab. The CD has 4 rolls of 36 exposures & 2 rolls of 24 exposures on it.

I used ImageMagick’s Ruby library rmagick. Each photo was rotated (if needed), cropped, resized (for a thumbnail) and branded. So that is a couple of transitory images and two images saved back to disc.

The error text produced was ‘unable to extend cache’. I googled this and came up with four items. None of them these had a solution.

Based on the text, it was either a memory or disc space issue. So I kicked off saidar and ran the ruby code. Sure enough I watch the /tmp partition (2 GB) be steadily consumed and the run then fall over.

After digging around for anything on Ruby destructors & garbage collection, I got a break. My first lead came from a Space Babies blog post and its comments. In the comment, one of rmagick’s developers Tim Hunter refers to a 2004 Hints & Tips post titled Help! My script runs out of memory!.

After adding ‘CG.start’ into the main loop, the run didn’t go over 0.5% of the /tmp partition.

Gnoll110

Technorati tags: