Reading from a table/spreadsheet

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Locked
Andon

Reading from a table/spreadsheet

Post by Andon »

I only got ImageMagick yesterday, and while I've managed to get it to do most of what I want it to do, there's still one thing: Reading from a file.

I know that ImageMagick can read from a plain text file - I fiddled with this a bit, but it just wasn't quite what I wanted. I'm wondering if ImageMagick can read from a table or spreadsheet file of some kind.

What I am trying to do is bulk-make cards for a card game. I have everything set up in the right places, but I still have to go in and change the text manually each time. What I do have is a spreadsheet with each card, its various costs, and its text in different columns, with a new card being on a new row.

Does ImageMagick read from a table file natively?

If it doesn't, what other options are there? I'm fairly new with Ubuntu here, but I'm getting the hang of it. Maybe a shell script to take a csv file, take the row, put it into various files (IE card_title.txt, card_text.txt), then run the image script, then go back and do it again for the next row?

Any help is appreciated

Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Reading from a table/spreadsheet

Post by Bonzo »

I tend to use php and would create an array and loop through the array. Thats how I create buttons for websites.

Andon

Re: Reading from a table/spreadsheet

Post by Andon »

I'm not too familiar with PHP, though, and I'd like to be able to do this myself. Mostly for when I want to change it, so that I can do that myself as well

Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Reading from a table/spreadsheet

Post by Bonzo »

If you are not happy using php there must be shell, batch script methods you can use to do something like this:

Code: Select all

<?php
// http://php.net/manual/en/function.fgetcsv.php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
        $num = count($data);
		$text = "";
        $row++;
        for ($c=0; $c < $num; $c++) {
		$text .= $data[$c];
		}
		exec("convert -size 400x100 xc:lightblue -fill black -pointsize 20 -gravity center -annotate +0+0 \"$text\" card$text.jpg");
        echo "<br><img src=\"card$text.jpg\"><br />\n";      
    }
    fclose($handle);
}
?>
CSV file:

Code: Select all

day|data|year
01|02|09
02|02|10
It would need some work to get what you wanted but you can see the method:
Read the csv file row contents into an array a row at a time and then add the array contents to an image; move onto the next row.

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reading from a table/spreadsheet

Post by fmw42 »

IM does not read from any special formatted table such as Excel (to my knowledge), but it can read from a space (or other) delimited fields of text data in a plain text file. I have done this a number of times in some of my scripts, but requires some knowledge of unix commands to read the data and parse the fields into variables.

Andon

Re: Reading from a table/spreadsheet

Post by Andon »

Thanks for the help, guys. I've had some help in getting it done via Python

Locked