I’d like to load an image, then size()
the display window to fit the actual width
and height
of the image, using a technique that is considered acceptable according to Processing.py standards. But I have only been able to accomplish the task by inadvisably placing a call to the loadImage()
function inside a settings()
function, then passing the dimensions of the image to the size()
function within the setup()
function.
The above may be problematic for a couple of reasons. For one thing, I cannot find information on the settings()
function within the Processing.py documentation. In addition, the documentation on settings()
for Java Mode specifically advises against the technique described above, stating, in part:
The settings() method runs before the sketch has been set up, so other Processing functions cannot be used at that point. For instance, do not use loadImage() inside settings().
The code is below, and it does seem to work. But is there a more acceptable and better means of doing this that is less likely to break in the future?
def settings():
global img
"""
image from:
https://py.processing.org/tutorials/pixels/imgs/tint1.jpg
(renamed sunflower.jpg)
"""
# Load the image; Is this allowed here?
img = loadImage("sunflower.jpg")
size(img.width, img.height)
def setup():
image(img, 0, 0)
def draw():
x, y = mouseX, mouseY
pix = get(mouseX, mouseY)
print("RGB at {}, {}: {}, {}, {}".format(x, y, int(red(pix)), int(green(pix)), int(blue(pix))))
EDIT (December 30, 2020):
Thanks, all, for the help.
Following is revision of the draw()
function, based on Reference: >> (right shift):
def draw():
x, y = mouseX, mouseY
argb = get(mouseX, mouseY)
a = (argb >> 24) & 0xFF # Faster way of getting alpha(argb)
r = (argb >> 16) & 0xFF # Faster way of getting red(argb)
g = (argb >> 8) & 0xFF # Faster way of getting green(argb)
b = argb & 0xFF # Faster way of getting blue(argb)
print("ARGB at {}, {}: {} {}, {}, {}".format(x, y, a, r, g, b))