We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm using the code below to successfully load and display jpg's, but when I try with a png file with a transparent background, I'm just getting a black box. I tried iterating through the pixels in img.pixels (with loadPixels() and updatePixels()) but everything's already set to -16777216....
Thanks for any suggestions.
InputStream istream = MyApp.class.getResourceAsStream("/res/images/xyz.png");
byte bytes[] = loadBytes(istream);
Image awtImage = new ImageIcon(bytes).getImage();
PImage img = new PImage(awtImage);
Answers
PImage img = new PImage(awtImage);
Problem here. If this is Processing (Java), the reference is clear by saying that you need to use
loadImage()
orcreateImage()
and not to usenew PImage(...)
.Kf
i think that's ok, it's here in the source
https://github.com/processing/processing/blob/master/core/src/processing/core/PImage.java#L291
and those last 3 lines are pretty much taken from here:
https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L5370
but it's missing the call to checkAlpha: https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L5462
lol: https://github.com/processing/processing/blob/master/core/src/processing/core/PImage.java#L273
Thanks a ton kfrajer! (and yes, definitely using Processing there with the PImage).
You reminded me I couldn't get loadImage() working from the jar/resource file, so I just re-surfed it and found this thread with the same problem with loadImage(). https://forum.processing.org/two/discussion/25493/eclipse-export-to-jar-images-don-t-load
The thread includes the working solution below! Super psyched and working like a charm! Thanks again!
Sorry I missed your post koogs! Another perfectly working solution below. I know the file has an alpha channel, so I just set it, which will bring me right back to this thread when it comes back to bite me! ;)
Thxs for sharing the solution @ottenm. It was simple. I checked the source code and the createImage() call a
new PImage()
and then it associates its parent field to the current PApplet. It seems this is important when saving an image.@koogs Any ideas why the change of format by
checkAlpha()
drives to this result? I am just curious....Kf
I'm guessing it's an efficiency thing. If there's no alpha then you don't need to worry about the current value of the screen pixel you're just about to overwrite, you can just copy the new value over it. If there is alpha involved then you have to blend the two.