We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I’m using Processing.py 3, and I want to be able to detect when the mouse is over a specific image, but also on a pixel that isn’t transparent. I think doing img.get() would give me the info I need, but I’ve also scaled up the image. How would I go about doing this?
For example: I have an image, whose original size is 10x10 pixels, scaled up by 10 so on the screen it’s 100x100. It’s placed at x=100, y=100. The mouse is located at x=150, y=150. What code would I need to get a true/false on the transparency of the image at the mouse position?
I looked at the documentation but it’s a little unclear. I also don’t know how to access alpha data.
Here’s my guess(in python mode):
test_img = loadImage(“image.png”)
image(img, 100, 100, 100, 100)
def if_mouse_on_image(img, xpos, ypos):
mouse_on_image_x = mouseX - xpos
mouse_on_image_y = mouseY - ypos
pixel_data = img.get(mouse_on_image_x, mouse_on_image_y, 10, 10) #here is where I’m confused the most.
return not pixel_data[3] #is this correct? I’m not familiar with alpha values
print(if_mouse_on_image(test_img))
Would this code achieve my goals? The reason I haven’t tried this myself is because I’m not at my computer. I’m writing this from my phone.
Answers
I don’t know why the code section freaked out.
You can go back
Edit your post with the gear
Empty line before and after the code
Select code with the mouse
Hit ctrl-o to indent it by four
Save the post
(done)
Thank you koogs. Any comment on the question?
Python mode? No
Try drawing a rectangle of known colour / alpha and the getting the pixels from that to see how they are stored. In Java the pixels are ARGB, one byte per channel = one int per pixel.
Thank you again! I worked out the problem from your comment.
Python mode is ARGB bytes as well -- I believe the underlying
pixels[]
implementation is the Java API running on Jython.See also alpha and tint:
Yep. Python mode has almost the exact same features as java mode.