# Ruby .read image upside down?

**URL:** <https://www.ruby-forum.com/t/ruby-read-image-upside-down/138994>\
**Category:** Ruby\
**Created:** [May 29, 2008, 8:14pm UTC](https://www.ruby-forum.com/t/ruby-read-image-upside-down/138994 "2008-05-29T20:14:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrismalek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@chrismalek](https://www.ruby-forum.com/u/chrismalek)\
**Post date:** [May 29, 2008, 8:14pm UTC](https://www.ruby-forum.com/t/ruby-read-image-upside-down/138994/1 "2008-05-29T20:14:58Z")

</div>

Hi,

I’m discorvering a strange issue when reading in an image being sent by  
a 3D rendering software. The image data is being passed in a parameter  
called ‘thumbnail’. We read it into a variable in the responding  
controller like so:

if params[:thumbnail] !=nil && params[:thumbnail] != “”  
virtual\_item.thumbnail = params[:thumbnail].read  
virtual\_item.thumbnail\_content\_type =  
params[:thumbnail].content\_type.chomp  
end

---

<div class="post-metadata">

**Author:** ![chrismalek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@chrismalek](https://www.ruby-forum.com/u/chrismalek)\
**Post date:** [May 29, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/ruby-read-image-upside-down/138994/2 "2008-05-29T20:23:26Z")

</div>

Sorry the above message posted prematurely.

The code is as follows:

if params[:thumbnail] !=nil && params[:thumbnail] != “”  
virtual\_item.thumbnail = params[:thumbnail].read  
virtual\_item.thumbnail\_content\_type =  
params[:thumbnail].content\_type.chomp  
end

Now the .read function read the image properly. However it is being  
stored in the database upside down. The rows of the image are flipped  
only vertically, which is strange, and not both vertically and  
horizontally backwards. I’m assuming that there is a some ‘header’  
bytes which describe the image, then the actual pixel data list row by  
row. We are reading PNG files.

The 3D rendering software outputs the images the right side up, but once  
they have been transfered and saved in the database they are upside  
down.

Any ideas on how we can investigate whether the .read function is doing  
something strange or prove that truly the params[:thumbnail] parameter  
actually contains upside down data?

---

<div class="post-metadata">

**Author:** ![chrismalek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@chrismalek](https://www.ruby-forum.com/u/chrismalek)\
**Post date:** [May 29, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/ruby-read-image-upside-down/138994/3 "2008-05-29T22:07:29Z")

</div>

On Thu, May 29, 2008 at 8:23 PM, Chris M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Now the .read function read the image properly. However it is being  
> Any ideas on how we can investigate whether the .read function is doing  
> something strange or prove that truly the params[:thumbnail] parameter  
> actually contains upside down data?

Can you save the data back from the database in a file and then look at  
a  
hex dump of it? I assume you store the PNG file intact and are not  
trying  
to interpret it at all?  
If you can save the file you can look at the bytes using Hexer on Linux  
or  
XVI32 on Windows, and compare the bytes to the PNG file.

If you are using Ruby under Windows there’s one trick I know of:  
binary files need ‘binmode’ to be set to true in order for them to be  
read properly. But that probably wouldn’t invert them.

Les

---

<div class="post-metadata">

**Author:** ![chrismalek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@chrismalek](https://www.ruby-forum.com/u/chrismalek)\
**Post date:** [June 11, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/ruby-read-image-upside-down/138994/4 "2008-06-11T20:40:37Z")

</div>

Still no luck. I’ve looked at the .read function and dumped its  
contents into a file and the image is still flipped when I view it.

It seems that once the .read function is performed the data is  
interpreted and the rows are inverted.

I’ve come to the solution that there is a bug in the way the .read  
function works in ruby, or there is a bug in Unity (the 3D engine)  
sending the rendered image.

The .read seems to work fine when uploading an image through the  
browser, however we run into the vertical flip problem when images are  
sent from Unity.

Any thought?
