# Resize PImage: "width or height must be \> 0 for resize"

**URL:** <https://discourse.processing.org/t/resize-pimage-width-or-height-must-be-0-for-resize/7109>\
**Category:** Coding Questions\
**Created:** [January 2, 2019, 6:36pm UTC](https://discourse.processing.org/t/resize-pimage-width-or-height-must-be-0-for-resize/7109 "2019-01-02T18:36:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amaia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amaia/32/3012_2.png) [@amaia](https://discourse.processing.org/u/amaia)\
**Post date:** [January 2, 2019, 6:36pm UTC](https://discourse.processing.org/t/resize-pimage-width-or-height-must-be-0-for-resize/7109/1 "2019-01-02T18:36:27Z")

</div>

Hello:  
I want to scale myImage to fit in the height of the screen  
FIRST: divide the width and height of the screen by the width and height of myImage.  
SECOND: multiply the width and height of myImage by one of the two previous results, according to whether we want it to fit the width or height of the screen (in this case, the height).  
The problem is that there are no real-floats as you can see in the console´s first line  
The code:

```auto
PImage myImage;

void setup(){
  size(600,600);
  float myOperation= 600/1024;
  println("Why 600/1024 = "+myOperation+"???");
  PImage myImage = createImage(512, 1024, RGB);
  myImage.loadPixels();
  for (int i = 0; i < myImage.pixels.length; i++) {
    myImage.pixels[i] = color(0, 90, 102); 
  }
  myImage.updatePixels();
  int myImageWidth= myImage.width;
  int myImageHeight= myImage.height;
  println("myImageSize= "+myImageWidth+" X "+myImageHeight);
  // scale(0.5); // just to check
  // image(myImage, 17, 17); //it´s OK
  println("screenSize = "+width+" X "+height);
  float widthRatio= width /myImageWidth;
  float sameWidthRatio= 600/512;
  println("widthRatio = "+widthRatio+"; same Operation: 600/512 = "+sameWidthRatio);
  float heightRatio= height /myImageHeight;
  float sameHeightRatio= 600/1024;
  println("heightRatio = "+heightRatio+"; same Operation: 600/1024 = "+sameHeightRatio);
  int resizeWidth=int(myImageWidth*heightRatio);
  int resizeHeight=int(myImageHeight*heightRatio);
  myImage.resize(resizeWidth,resizeHeight); // width or height must be > 0 for resize
}

```

Am I doing something wrong? I tried this:

```auto
void setup(){
  size(200,200);
  float a=1024;
  float b= 600;
  float c=b/a;
  textSize(20);
  text(c,50,50); 
}

```

And c is 0,586… of course…  
There is something I don’t understand but I don’t know what it is  
How can i resize the Pimage? Thanks!!

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [January 2, 2019, 6:50pm UTC](https://discourse.processing.org/t/resize-pimage-width-or-height-must-be-0-for-resize/7109/2 "2019-01-02T18:50:24Z")

</div>

> [@amaia](#):
>
> `float widthRatio= width /myImageWidth; `  
> `float sameWidthRatio= 600/512;`

These are performing integer division so the decimal part is truncated, hence `sameWidthRatio = 1` not `1.171875`

The solution is to convert one or both of the values to a float before doing the division e.g.

`float widthRatio= ((float) width) / myImageWidth; `  
`float sameWidthRatio= 600.0/512;`

---

<div class="post-metadata">

**Author:** ![amaia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amaia/32/3012_2.png) [@amaia](https://discourse.processing.org/u/amaia)\
**Post date:** [January 2, 2019, 7:02pm UTC](https://discourse.processing.org/t/resize-pimage-width-or-height-must-be-0-for-resize/7109/3 "2019-01-02T19:02:05Z")

</div>

Thank you quark!! it works
