title: RMagick, Tempfile, Paperclip: how to save a image file with large dimensions and small kbs as a thumbnail?
body: <p>I have a Rails rake task that is processing a batch of images. It strips out the white background (using RMagick), replaces it with a transparent layer, writes it to a tempfile and then saves it as a PNG on Amazon S3 (using Paperclip).</p>
  
  <p>It works for the bulk of the images. However, it runs into an error for at least 1 image. Can someone help me figure out why and how to fix it?</p>
  
  <p><strong>Code sample:</strong></p>
  
  <pre><code>require 'RMagick'
  require 'tempfile'
  include Magick
  
  task :task_name =&gt; :environment do
     x = Item.find(128)  # image 128 is the one giving me trouble
     sourceImage = Image.read(x.image_link_hires)
     processedImage = sourceImage[0].transparent("white")
  
     tempImageFile = Tempfile.new(["processed_image",".png"])
     processedImage.write("png:" + tempImageFile.path)
     x.image_transparent = tempImageFile
     x.save!
  end
  </code></pre>
  
  <p><strong>The error message:</strong></p>
  
  <blockquote>
    <p>rake aborted! Validation failed: Image transparent C:/Users/Roger/AppData/Local/Temp/processed_image20130107-8640-1ck71i820130107-8640-i6p91w.png is not recognized by the 'identify' command., Image transparent C:/Users/Roger/AppData/Local/Temp/processed_
    image20130107-8640-1ck71i820130107-8640-i6p91w.png is not recognized by the 'identify' command.</p>
  </blockquote>
  
  <p>This message appears upon running the last line (the save operation).</p>
  
  <p><strong>Tempfile problem with small files?</strong></p>
  
  <p>I think the error has something to do with Tempfile not actually writing a file to the temp path. This error may have to do with small filesize? The specific image that it's having trouble with has an usually amount of white space, so the resulting filesize after processing is about 30k for an 800x800 pixel image.</p>
  
  <p>How can I verify if this is the case? And if it is, how can I work around it?</p>
  
  <p><strong>Other observations:</strong></p>
  
  <ol>
  <li>When I write the trouble image to a normal file (rather than Tempfile), it saves successfully locally.</li>
  <li>The task works fine for other images, which tend to be much bigger (~1-2MB)</li>
  <li>After <code>processedImage.write</code>, I've checked <code>tempImageFile.size</code>. It says that it's 30kb as expected.</li>
  <li>When I observe the temp file directory when the rake task runs, I can see the temp files being created when the task is run other images successfully. The files seem to show up when <code>processedImage.write</code> runs. However, for the trouble image, I don't see temp files ever being created.</li>
  </ol>
  
  <p>Thanks for any advice.</p>
  
  <p><strong>Update 7 Jan 2013</strong></p>
  
  <p>I've investigated this more. I reran #1 above, but attempted to save onto S3 with Paperclip. This generated the same error message.</p>
  
  <p>So now I believe the issue is that this is a small file in terms of bytes (32kb), but with a decent height and width (800x800). Paperclip is trying to save a thumbnail version of it, which is 90x90. Typically this generates a filesize that is &lt;1% the original, which I assume is the source of the errors.</p>
  
  <p>If anyone has an elegant workaround / fix for this, I'd appreciate hearing about it.</p>