<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Photography on R Views</title>
    <link>https://rviews.rstudio.com/tags/photography/</link>
    <description>Recent content in Photography on R Views</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 13 Feb 2020 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://rviews.rstudio.com/tags/photography/" rel="self" type="application/rss+xml" />
    
    
    
    
    <item>
      <title>Photo Mosaics in R</title>
      <link>https://rviews.rstudio.com/2020/02/13/photo-mosaics-in-r/</link>
      <pubDate>Thu, 13 Feb 2020 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2020/02/13/photo-mosaics-in-r/</guid>
      <description>
        


&lt;p&gt;&lt;em&gt;Harrison Schramm, CAP, PStat, is a Senior Fellow at the Center for Strategic and Budgetary Assessments.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In this short piece, I’m going to discuss a fun photography project I did over the winter using R. I’m also going to touch on some of the implications of the R license, which underlies our entire ecosystem, but we don’t usually think about that often.&lt;/p&gt;
&lt;p&gt;I’ve been a dedicated useR for the past 4 years. I started by using R for all the things that I previously did with spreadsheets - a great way to learn your way around the &lt;code&gt;magrittr&lt;/code&gt; and &lt;code&gt;dplyr&lt;/code&gt; packages. From there, I Replaced my word processor and slide software with &lt;code&gt;markdown&lt;/code&gt;. All the while, I’ve been increasing the amount of time I spend on graphics, particularly within the &lt;code&gt;ggplot2&lt;/code&gt; construct, as well as color scales provided by the &lt;code&gt;ggsci&lt;/code&gt; package (there are Simpsons and Futurama color palettes)! At some point over the summer, my interest in developing graphics began to inspire my R-tistic side, which I wrote about in &lt;a href=&#34;https://pubsonline.informs.org/do/10.1287/LYTX.2019.06.12/full/&#34;&gt;INFORMS/Analytics&lt;/a&gt; magazine this summer.&lt;/p&gt;
&lt;p&gt;All of last year, I worked on a project titled ‘Mosaic’. When we finished our &lt;a href=&#34;https://csbaonline.org/research/publications/mosaic-warfare-exploiting-artificial-intelligence-and-autonomous-systems-to-implement-decision-centric-operations&#34;&gt;report&lt;/a&gt;, my coauthors asked for suggestions on cover art, I naturally suggested we create a photo mosaic. While there are both free and commercially available solutions, my first choice, of course, was to find an R-centric solution. The advantages to using R for this project (as well as other things) is that it allows for the creation of &lt;em&gt;bespoke&lt;/em&gt; solutions; in other words, I don’t want just any photo mosaic, but rather one that has the attributes that I want.&lt;/p&gt;
&lt;p&gt;After a quick stack overflow / CRAN search, I found the &lt;code&gt;RsimMosaic&lt;/code&gt; &lt;a href=&#34;https://cran.r-project.org/web/packages/RsimMosaic/RsimMosaic.pdf&#34;&gt;package&lt;/a&gt; which gave me the tools I was looking for.&lt;/p&gt;
&lt;div id=&#34;making-a-photo-mosaic&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Making a photo mosaic&lt;/h3&gt;
&lt;p&gt;Basically, the tools in this package take a base image and replace each pixel with a tile. If properly chosen, a close-up view will focus on the tiles, but at a distance the base image will emerge. While this is simple enough, there are a few thing to consider.&lt;/p&gt;
&lt;p&gt;First, the dimension of the resulting image will be (in pixels) approximately the base image expanded by the tiles; for instance, if the base is 150x150, and the tiles are 30x30, the resulting image will be 4500 x 4500. I (somewhat embarrassingly) discovered this by having my R instance return a &lt;code&gt;cannot allocate vector of size 9GB&lt;/code&gt; error &lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Second, there is an R-tistic balance between the size of the base image and the size of the tiles; some experimentation is necessary. If you have an extensive library of tiles (I had over 600 in this instance), it is possible - but ill advised - to try to adjust their sizes manually. Fortunately, the package has a utility for doing this. However, there’s a catch; not all pictures have a base resolution that is amenable to being scaled down.&lt;/p&gt;
&lt;p&gt;Building a photo mosaic is really an R-tistic thing to do. The key is to collect a library of tiles that will allow sufficient diversity so that the program can make good contrast choices. For example, pictures of ships and airplanes are heavy in blue tones, etc.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;making-a-tile-library&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Making a tile library&lt;/h3&gt;
&lt;p&gt;Once you have found your tiles, you will want to resize them. For example, I have found in most images I’ve been working with that a 30 x 30 (pixel) tile is a good size, balancing resolution with ‘mosaic-ness’. However, this is not the size of most raw images, and while you can resize them in MS Paint, this is a painstaking process. Fortunately &lt;code&gt;RsimMosaic&lt;/code&gt; provides a handy method: &lt;code&gt;createTiles&lt;/code&gt;. It’s &lt;em&gt;almost&lt;/em&gt; perfect for my application.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;heres-the-bit-about-the-license&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Here’s the bit about the license&lt;/h3&gt;
&lt;p&gt;Because R and it’s packages are distributed under the GPL license, you have the ability to adjust the functions that are in packages. This is straightforward; if you want to adjust a function &lt;code&gt;foo&lt;/code&gt;, you can assign it a new name &lt;code&gt;foo2 &amp;lt;- foo&lt;/code&gt; and then &lt;code&gt;fix(foo2)&lt;/code&gt; (or use a script / markdown) to make changes. You can of course simply edit the original function, but I do not recommend this as it can cause confusion on subsequent sessions.&lt;/p&gt;
&lt;p&gt;Remember in the preceding paragraph where I said it was &lt;em&gt;almost&lt;/em&gt; perfect? Some tiles have base sizes that cannot be coerced to rectangular shapes. The method that comes with the package simply generates an error. What is useful when generating ~600 tiles is to have a list so that I know the one that threw an error &lt;a href=&#34;#fn2&#34; class=&#34;footnote-ref&#34; id=&#34;fnref2&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;But because we can do whatever we want with existing functions, consider the following modification:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;createTiles2 = createTiles&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;fix(createTiles2)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;{add the following directly after line 15:}&lt;/p&gt;
&lt;p&gt;&lt;code&gt;print(filenameArray[i])&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;And voila! Fast and artistic! I used this list to find the tiles that could not be resized, and removed them from my tile library.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;making-the-mosaic&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Making the Mosaic&lt;/h2&gt;
&lt;p&gt;Now, for the part where we make the mosaic. The command:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;composeMosaicFromImageRandomOptim(&amp;quot;RLogo_small.jpg&amp;quot;, &amp;quot;RMosaic.jpg&amp;quot;, &amp;quot;OutputPathGoesHere&amp;quot;)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;generates the following:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;RMosaic.jpg&#34; height = &#34;600&#34; width=&#34;800&#34;&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;is it surprising that an accomplished professional would admit to such a silly mistake in public? I don’t think so! We would all be better off if we were as open with our success as our failures; first to prevent others from wasting precious time with similar mistakes, and second to show that if we are going to work at the cutting edge, we are all in a sense students.&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn2&#34;&gt;&lt;p&gt;other programmers might suggest a &lt;code&gt;tryCatch()&lt;/code&gt; environment, and I did think of that. Here was a case where I wanted something that worked fast, vice something that worked well.&lt;a href=&#34;#fnref2&#34; class=&#34;footnote-back&#34;&gt;↩&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2020/02/13/photo-mosaics-in-r/&#39;;&lt;/script&gt;
      </description>
    </item>
    
  </channel>
</rss>
