# [localhost:~]\$ cat * > /dev/ragfield

## Tuesday, March 31, 2009

### FlickrCalendar

I take (at least) one photo each day and designate it my Photo of the Day. I have a set on Flickr with all of these photos and I thought it would be interesting to look at these in a calendar layout. I wrote a simple Mathematica package to do some basic photoset and photo management on Flickr. Using this package it is a simple matter to download all the Photos of the Day for a given month and arrange them in a calendar layout.
Load the Flickr package.
<<Flickr`
Get a list of all my photosets.
Short[sets = FlickrPhotosets[]]
{72157610604254463->Photo of the Day, <<22>>, 72157609839542329->S…on}
Get the ID of each photo in the "Photo of the Day" set.
set = First[sets]
72157610604254463->Photo of the Day
photos = FlickrPhotosetPhotos[set,
{2009, 3, 1, 0, 0, 0}, {2009, 4, 1, 0, 0, 0}]
{3320801525, 3325013076, 3327845866, 3330214268, 3332574152, 3333951749, 3336286979, 3339612210, 3343616292, 3346180622, 3347410349, 3349861835, 3352196443, 3355574796, 3358281254, 3361319213, 3364753768, 3366450803, 3368858991, 3372005764, 3374713192, 3377655879, 3381457738, 3384282674, 3386926438, 3389038038, 3390498631, 3394129518, 3396858987, 3400094291, 3402856851}
Length[photos]
31
Import the 75x75 pixel "Square" sized thumbnail of each photo.
squares = FlickrPhotoImport[#, "Square"]& /@ photos;
First[squares]
Overlay the day of the month on top of each thumbnail.
tiles = Table[
Show[Rasterize[squares[[i]]],
Graphics[{
White, Opacity[0.667],
FontSize -> 64,
Text[ToString[i], {Center, Center}]
}]
], {i, Length[squares]}
];
First[tiles]
Get the URL of each image and make the tiles hyperlinks.
urls = First["urls" /. FlickrPhotoInfo[#]]& /@ photos;
First[urls]
http://www.flickr.com/photos/ragfield/3320801525/
tiles = Table[Hyperlink[tiles[[i]], urls[[i]]], {i, Length[tiles]}];
Now write a reusable calendar[] function which takes a list of content and lays them out into a grid for the specified month.
daysInMonth[year_Integer, month_Integer] := Part[
{31,
Which[
Mod[year, 400] === 0, 29,
Mod[year, 100] === 0, 28,
Mod[year, 4] === 0, 29,
True, 28
], 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, month
];
calendar[
year_Integer,
month_Integer,
content_List,
opts___
] := Module[
{startDate, startDay, daysOfWeek, days},
startDate = {year, month, 1, 0, 0, 0};
startDay = DateString[startDate, "DayNameShort"];
daysOfWeek = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

(*
if the month starts in the middle of the week
prepend some empty strings to the content list
*)

days = Join[Table["",
{Position[daysOfWeek, startDay][[1,1]] - 1}], content];

(*
if less content was specified than days in the month
pad the end of content list
*)

days = Join[days, Table["",
{daysInMonth[year, month] - Length[content]}]];

(*
if the month ends in the middle of the week
append some empty strings to the content list
*)

days = Join[days, Table["",
{Mod[7 - Mod[Length[days], 7], 7]}]];

GraphicsGrid[
Join[
{Join[{Text[Style[
DateString[startDate,
{"MonthName", " ", "Year"}],
FontFamily -> "Times",
FontSize -> 48
]]},
Table[SpanFromLeft, {6}]
]},
Partition[days, 7]
],
opts
]
];
Now add the tile images to the calendar.
c = calendar[2009, 3, tiles,
Spacings -> 0, ImageSize -> 460,
Dividers -> All, Background -> LightGray]
We could export directly to JPEG, but since the image contains hyperlinks exporting to HTML will yield both a JPEG and the HTML code for an image map.
SetDirectory[CreateDirectory["/tmp/FlickCalendar"]];
Export["FlickCalendar.html", c, "GraphicsOutput" -> "JPEG"];