Why are my composite PNGs black when using pillow? - python-imaging-library

For a final in one of my classes we basically had to make an NFT generator using pillow. I am a beginner with coding and so I googled some things and put together a code that would take random selections from PNGs I had made on photoshop and paste them on top of each other in random combinations. From what I can tell everything works fine, but the final image doesn't show any color, just black for filled areas and white for transparent ones. Its a very simple code so I will post most of it so you can see what I am talking about. Obviously y'all won't have all of my image files so I included only one of the dictionaries that reference them just so you know how I did it.
dict4 = {
"0": "C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/blue.png",
"1": "C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/green.png",
"2":"C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/orange.png",
"3":"C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/pink.png",
"4":"C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/purple.png",
"5":"C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/red.png",
"6":"C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/teal.png",
"7":"C:/Users/allib/OneDrive/Documents/SCHOOL CLASS FOLDERS/Vist 270/Final/worm_color/yellow.png",
}
# List section where the random selection will come from
worm_eye_lst = [dict['0'],dict['1'],dict['2'],dict['3'],dict['4'],dict['5'],dict['6'],dict['7'],dict['8'],dict['9']]
worm_hat_lst = [dict2['0'],dict2['1'],dict2['2'],dict2['3'],dict2['4']]
worm_eyebrow_list = [dict3['0'],dict3['1'],dict3['2'],dict3['3'],dict3['4']]
worm_color_list = [dict4['0'],dict4['1'],dict4['2'],dict4['3'],dict4['4'],dict4['5'],dict4['6'],dict4['7']]
# Random selection function
def rand_attr(lst):
number_bank=len(lst)-1
choice = random.randint(0,number_bank)
return choice
# Function that puts all the pictures together
def composite_pic():
bg = Image.open("background.png")
cc=rand_attr(worm_color_list)
print(cc)
color_choice = worm_color_list[cc]
color = Image.open(color_choice)
bg.paste(color, (0, 0), mask = color)
base_worm = Image.open("base_worm.png")
bg.paste(base_worm, (0,0), mask = base_worm)
ec=rand_attr(worm_eye_lst)
print(ec)
eye_choice = worm_eye_lst[ec]
eye = Image.open(eye_choice)
bg.paste(eye, (0, 0), mask = eye)
hc = rand_attr(worm_hat_lst)
print(hc)
hat_choice= worm_hat_lst[hc]
hat = Image.open(hat_choice)
bg.paste(hat, (0, 0), mask = hat)
ebc = rand_attr(worm_eyebrow_list)
print(ebc)
eyebrow_choice = worm_eyebrow_list[ebc]
eyebrow = Image.open(eyebrow_choice)
bg.paste(eyebrow, (0,0), mask = eyebrow)
bg.show()
composite_pic()
Picture of the wrong output

Related

In Unity, how can i instantiate 4 images that are clickable?

Id like to make a language learning game but im trying to instantiate images that are clickable and have 4 instantiate where one is correct and when all 4 are instantiated an audio clip plays that is associated with one of the images.
First I would make a Class to manage that each Image has an associated AudioClip.
class Answers{
AudioClip clip;
Sprite img;
}
Then you can create as many Answers as you choose (here 1 as an example)
AudioClip clip1; // define in inspector
Answers[] answers = {Resources.Load <Sprite>("name_of_the_sprite"), clip1}; // Sprite with the name will be loaded in runtime
Instead of instantiating new Images you should now just be able to edit the same ones. Because only the Image on the objects and the audio clip needs to change.
To manage that, we write a Function:
// Defining Images that we want to change
public Image img1;
public Image img2;
public Image img3;
public Image img4;
// Call it each time you want to change the Answers
public void ChangeImages()
{
// Selecting 4 Random Images
Answers a1 = answers[answers.RandomRange(0, answers.Count)];
Answers a2 = answers[answers.RandomRange(0, answers.Count)];
Answers a3 = answers[answers.RandomRange(0, answers.Count)];
Answers a4 = answers[answers.RandomRange(0, answers.Count)];
// Change Sprite of the Images
img1.sprite = a1.img;
img2.sprite = a2.img;
img3.sprite = a3.img;
img4.sprite = a4.img;
// To make that random just make a switch statement
int randomnumber = Random.Range(1, 5)
// Correct Solution
int correctImage = 0;
// Play your audioclip, for which every Image you want to be correct
switch (randomnumber)
{
case 1:
PlayAudioClip(a1.clip);
correctImage = 1;
break;
case 2:
PlayAudioClip(a2.clip);
correctImage = 2;
break;
case 3:
PlayAudioClip(a3.clip);
correctImage = 3;
break;
case 4:
PlayAudioClip(a4.clip);
correctImage = 4;
break;
}
}
If the player now clicks on an Image you can check if it was the correct one with the correct Image number and depending on that let the player continue or end the game.

Erasing parts of a bitmap in EaselJS using destination-out compositing

I'm having a bit of difficulty getting some functionality to work. I'm trying to create an eraser and erase parts of an image using easelJS. I've seen other people do this, but only erasing other graphics - and when I try to erase an image, I can't get anything to work. If I wanted to erase a bitmap instead of other graphics, is that possible?
I also tried to use the AlphaMaskFilter, but it's giving me the exact opposite of what I'm looking for (it's masking everything, and only revealing what I paint).
var c = createjs, stage, art;
var x, y, listener, color, hue=0;
stage = new c.Stage("test");
var testImg = new c.Bitmap("http://lorempixel.com/output/animals-q-c-640-480-5.jpg");
art = stage.addChild(testImg, new c.Shape());
art.cache(0,0,600,400);
stage.on("stagemousedown", startDraw, this);
function startDraw(evt) {
listener = stage.on("stagemousemove", draw, this);
stage.on("stagemouseup", endDraw, this);
color = c.Graphics.getHSL(hue+=85, 50, 50);
x = evt.stageX-0.001; // offset so we draw an initial dot
y = evt.stageY-0.001;
draw(evt); // draw the initial dot
}
function draw(evt) {
art.graphics.ss(20,1).s(color).mt(x,y).lt(evt.stageX, evt.stageY);
// the composite operation is the secret sauce.
// we'll either draw or erase what the user drew.
art.updateCache(erase.checked ? "destination-out" : "source-over");
art.graphics.clear();
x = evt.stageX;
y = evt.stageY;
stage.update();
}
function endDraw(evt) {
stage.off("stagemousemove", listener);
evt.remove();
}
http://jsfiddle.net/17xec9y5/8/
Your example is only affecting the Shape instance that you have cached. When you use multiple arguments in addChild(), it returns the last added item, so in your sample, the art variable just references the shape. So the image is just below the "painted area" that you are drawing to.
To fix this, create and cache a container instead. A few minor additions:
Once the image loads, update the cache one time (to apply the image).
Then remove the image so it is no longer applied every time you update the cache while drawing.
That's it!
Here is a fiddle:
http://jsfiddle.net/lannymcnie/17xec9y5/9/
Relevant Code:
// Listen for the image load
testImg.image.onload = function() {
cont.updateCache("source-over"); // Update cache once
cont.removeChild(testImg); // Remove image
stage.update(); // Draw the stage to see the image
}
// Create a sub-container that will hold the art and image
var cont = stage.addChild(new c.Container());
art = new c.Shape(); // Art is just the shape
cont.cache(0,0,600,400); // Cache the container instead
cont.addChild(testImg, art);
// Then, later update the container's cache (instead of the art)
cont.updateCache(erase.checked ? "destination-out" : "source-over");

Bing maps polygon/polyline appears beneath image overlay

I´ve been following this post about using an image overlay for bing maps:
http://blogs.msdn.com/b/bingdevcenter/archive/2014/04/04/image-overlays-with-bing-maps-native.aspx
What I want to now is to be able to add polygons/polylines on top of this image. Lets say for example that we use the following code:
(From here: http://blogs.bing.com/maps/2014/01/23/make-clickable-shapes-in-the-native-bing-maps-control/)
private void MyMapLoaded(object sender, RoutedEventArgs e)
{
//Add a shape layer to the map
shapeLayer = new MapShapeLayer();
MyMap.ShapeLayers.Add(shapeLayer);
//Create mock data points
var locs = new LocationCollection();
locs.Add(new Location(coordinates that are over the image));
locs.Add(new Location(coordinates that are over the image));
locs.Add(new Location(coordinates that are over the image));
//Create test polygon
var polygon = new MapPolygon();
polygon.Locations = locs;
polygon.FillColor = Colors.Red;
shapeLayer.Shapes.Add(polygon);
var locs2 = new LocationCollection();
locs2.Add(new Location(20, 20));
locs2.Add(new Location(40, 40));
locs2.Add(new Location(50, 20));
//Create test polyline
var polyline = new MapPolyline();
polyline.Locations = locs2;
polyline.Width = 5;
polyline.Color = Colors.Blue;
//Add the shape to the map
shapeLayer.Shapes.Add(polyline);
}
The problem is that the polygon/polyline will always appear beneath the image.
ShapeLayer has a property for z-index but it does not help. Is there a way for my polygons to always be on top?
Not sure why you want to do this, but you won't be able to show a MapPolygon above a user control that is added as a child of the map. That said, you can create WPF Polygons and added them. What you could do is create a custom user control that combines the image overlay functionality with something like this: http://blogs.msdn.com/b/bingdevcenter/archive/2014/03/25/custom-shapes-in-windows-store-apps-c.aspx

Image disappearing when adding a blur

I load images via LoadQueue:
this.queue = new createjs.LoadQueue(false);
I create my bitmap, this works fine:
var myImg = new createjs.Bitmap(this.queue.getResult('test-img'));
myImg.scaleX = 0.2;
myImg.scaleY = 0.2;
myImg.x = 300;
I then add a blur:
var blurFilter = new createjs.BlurFilter(5, 5, 1);
myImg.filters = [blurFilter];
var bounds = blurFilter.getBounds();
myImg.cache(-50+bounds.x, -50+bounds.y, 100+bounds.width, 100+bounds.height);
Then finish up with:
this.stage.addChild(myImg);
this.stage.update();
The problem is, as soon as I add the blur, the image no longer appears, where am I going wrong?
I implemented your code and it works well http://jsfiddle.net/k4yhz6oy/2/.
I suppose the cache area of your image is white or transparent.
myImg.cache(-50+bounds.x, -50+bounds.y, 100+bounds.width, 100+bounds.height);
Apply image bounds to cache
var imageBound = myImg.getBounds();
myImg.cache(imageBound.x, imageBound.y, imageBound.width, imageBound.height);

remove graphics from inside a class as3

When I click a button in my game it draws shapes using the graphics in as3. simple shapes such as circles and rectangles.
I want to remove the graphics that have been drawn when something happens in one of my classes.
Basically when there is a hitTestObject (which works fine) I want all graphics on stage to be cleared.
if (gb2.hitTestObject(h1s2))
{
trace ("holed")
ySpeed2=0;
xSpeed2=0;
this.visible=false;
var mcSplash:MovieClip =parent.getChildByName("mcSplash") as MovieClip;
mcSplash.visible=true;
//parent.drawings.graphics.clear();
}
My attempt using parent.drawings.graphics.clear(); was unsuccessful, it gives me this error:
Line 481 1119: Access of possibly undefined property drawings through a reference with static type flash.display:DisplayObjectContainer.
Anyone have any suggestions
UPDATE:
this is how, on the min time line, the drawings occur.
var drawings:Shape = new Shape;
for (i=0; i<numRecs; i++)
{
recStartX = Number(xmlContent.rec[i].startpoint.#ptx);
recStartY = Number(xmlContent.rec[i].startpoint.#pty);
recWidth = Number(xmlContent.rec[i].dimensions.#w);
recHeight = Number(xmlContent.rec[i].dimensions.#h);
fillColor=int(xmlContent.rec[i].look.fillhex);
lineThick = Number(xmlContent.rec[i].look.strokethick);
lineColor = int(xmlContent.rec[i].look.strokehex);
drawings.graphics.lineStyle(lineThick, lineColor);
drawings.graphics.beginFill(fillColor);
drawings.graphics.drawRect(recStartX,recStartY,recWidth,recHeight);
drawings.graphics.endFill();
}
Create an array and push in each shape/rect.
Then iterate through this and remove..
for(var iteration:int = 0; iteration < rectArray.length; iteration++)
this.removeChild(rectArray[iteration]);
or if you are calling this from a class, use
MovieClip(this.root).removeChild(rectArray[iteration]);
Hopefully this is helpful :)
Z
What's drawings?! If you draw in mcSplash, you should use mcSplash.graphics.clear(). If you draw in a child called drawings, you should first get it as a child (after mcSplash get): var drawings = mcSplash.getChildByName('drawings); drawings.graphics.clear();. You could write checks to see what's going on: if (mcSlpash) { if (drawings) {, etc..