Loading...
Logo
Processing Forum

Displaying a part of the image

in General Discussion  •  Other  •  3 years ago  
Hi!

I need to display a part of the image, eg I have 300x300px image and I need to display the lower 100x300px. Adding some mask elements over the image won't help, because they will cover other stuff too. So, is it possible to display just a part of the image with image()  (or some other) function? 

Replies(4)

Assuming that you have a PImage object called img then inside draw() you can copy just part of this image with

image(img, dx, dy, dw, dh, sx, sy, sw, sh);

where
dx, dy, dw, dh   = the area of your display that you want to draw to.
and
sx, sy, sw, sh  = the part of the image to draw (measured in pixels)

The beauty of this is that the destination width and height does not hve to match the source width and height. The image source will be scaled to fit the destination.

Wonderful! Very easy to do rollover with that :)
Yeah, but I am trying to do this with the image from webcam, ( here is an example). When the coordinates of the source image are set to 0,0 the everything is ok, but when I move them to another position, then the image is being zoomed or a mirror image (or upside down) is displayed.

I was wondering, is it possible to pass the image captured by read() function to the PImage object? 
I couldn't get this approach to work either. But the method gordancsa describes here does work for me.