I have several images that I want to layer in a different order, than the order in which they were created. I am using Python with Tkinter and was wondering if someone could help me with this. The order that I create the images is:
#Using Tkinter
image1 = PhotoImage(file = "imageA.gif")
image2 = PhotoImage(file = "imageB.gif")
image3 = PhotoImage(file = "imageC.gif")
A = canvas.create_image(X,Y,image=image1)
B = canvas.create_image(X,Y,image=image2)
C = canvas.create_image(X,Y,image=image3)
The order in which I create the images cannot be changed, so as of right now C is on top of B which is on top of A.
Is there a way to change the order - without changing the order that I create them - so that B is on top of C, and both on top of A? Perhaps there is some sort of attribute like B.Ontopof(C) ? Thanks for your help in advance.
canvas.tag_raise(item)
at least I think ...
Related
So I am trying to make a painterly node group with Python code straight so I can use it for future projects but I can't seem to get the power part of the formula in nuke to work from this colour difference formula( I'm also new to Nuke so if there is a better way of writing this please let me know it would be awesome thank you, or if I'm doing this wrong completely also let me know)
The following formula for color difference is used to create the
difference image: |(r1,g1,b1) – (r2,g2,b2)| = ((r1 – r2)^2 + (g1
–g2)^2 + (b1 – b2)^2)^1/2.
nRedShuffle = nuke.nodes.Shuffle()
nRedShuffle['red'].setValue('red')
nRedShuffle['green'].setValue('red')
nRedShuffle['blue'].setValue('red')
nRedShuffle['alpha'].setValue('red')
nGreenShuffle = nuke.nodes.Shuffle()
nGreenShuffle['red'].setValue('green')
nGreenShuffle['green'].setValue('green')
nGreenShuffle['blue'].setValue('green')
nGreenShuffle['alpha'].setValue('green')
#...(so on for the rest of rgba1 and rgba2)
nGreenShuffle2 = nuke.nodes.Shuffle()
nGreenShuffle2['red'].setValue('green')
nGreenShuffle2['green'].setValue('green')
nGreenShuffle2['blue'].setValue('green')
nGreenShuffle2['alpha'].setValue('green')
nBlueShuffle2 = nuke.nodes.Shuffle()
nBlueShuffle2['red'].setValue('blue')
nBlueShuffle2['green'].setValue('blue')
nBlueShuffle2['blue'].setValue('blue')
nBlueShuffle2['alpha'].setValue('blue')
#I am having troubles with the powers below
redDiff = nuke.nodes.Merge2(operation='minus', inputs=[nRedShuffle2, nRedShuffle])
redDiffMuli = nuke.nodes.Merge2(operation='multiply', inputs=[redDiff, redDiff])
greenDiff = nuke.nodes.Merge2(operation='minus', inputs=[nGreenShuffle2, nGreenShuffle])
greenDiffMuli = nuke.nodes.Merge2(operation='multiply', inputs=[greenDiff, greenDiff])
blueDiff = nuke.nodes.Merge2(operation='minus', inputs=[nBlueShuffle2, nBlueShuffle])
blueDiffMuli = nuke.nodes.Merge2(operation='multiply', inputs=[blueDiff, blueDiff])
redGreenAdd = nuke.nodes.Merge2(operation='plus', inputs=[redDiffMuli, greenDiffMuli])
redGreenBlueAdd = nuke.nodes.Merge2(operation='plus', inputs=[redGreenAdd, blueDiffMuli])
Here are at least two ways to implement Color Difference formula for two images. You can use difference op in Merge node or you can write a formula in field for each channel inside MergeExpression node:
Expression for each channel is as simple as this:
abs(Ar-Br)
abs(Ag-Bg)
abs(Ab-Bb)
Python commands
You can use .nodes.MergeExpression methodology:
import nuke
merge = nuke.nodes.MergeExpression(expr0='abs(Ar-Br)',
expr1='abs(Ag-Bg)',
expr2='abs(Ab-Bb)')
or regular .createNode syntax:
merge = nuke.createNode('MergeExpression')
merge['expr0'].setValue('abs(Ar-Br)')
merge['expr1'].setValue('abs(Ag-Bg)')
merge['expr2'].setValue('abs(Ab-Bb)')
Full code version
import nuke
import nukescripts
red = nuke.createNode("Constant")
red['color'].setValue([1,0,0,1])
merge = nuke.createNode('MergeExpression')
merge['expr0'].setValue('abs(Ar-Br)')
merge['expr1'].setValue('abs(Ag-Bg)')
merge['expr2'].setValue('abs(Ab-Bb)')
yellow = nuke.createNode("Constant")
yellow['color'].setValue([1,1,0,1])
merge.connectInput(0, yellow)
nuke.toNode('MergeExpression1').setSelected(True)
nukescripts.connect_selected_to_viewer(0)
# Auto-alignment in Node Graph
for everyNode in nuke.allNodes():
everyNode.autoplace()
Consider! MergeExpression node is much slower that regular Merge(difference) node.
I have developed a console-based adventure game for my Sixth Form Computing class, and now want to migrate it into Tkinter. The main reason for this is that I can make use of pictures, mainly ones from game-icons.net.
So far so good, but the images are such high quality that they appear huge when I display them. Here is an example:
The code works by using a for loop to iterate through a list of items that are in the current area (that the player is in). Here is the code:
if len(itemKeys) > 0:
l = Label(lookWindow, text="Looking around, you see the following items....\n").pack()
for x in range(0, len(itemKeys)):
icon = PhotoImage(file=("icons\\" + itemKeys[x] + ".png"))
l = Label(lookWindow, image=icon)
l.photo = icon
l.pack()
l = Label(lookWindow, text=("" + itemKeys[x].title())).pack()
l = Label(lookWindow, text=(" " + locations[position][2][itemKeys[x]][0] + "\n")).pack()
else:
l = Label(lookWindow, text="There's nothing at this location....").pack()
The part saying ("icons\\" + itemKeys[x] + ".png") simply goes into the icons folder in the game directory and strings together a file name, which in this case would result in "key.png" because the item we're currently looking at is a key.
Now, however, I want to resize the image. I've tried using PIL (which people say is deprecated but I managed to install just fine?) but so far no luck.
Any help appreciated.
Jake
EDIT:
The question has been marked as a duplicate, but I've already tried to use it, but the person who answered seems to open a file, save it as a ".ppm"(?) file and then display it, but when I try I get a huge error that says that I couldn't display a "PIL.Image.Image".
EDIT 2:
Changed it to this:
im_temp = PILImage.open(("icons\\" + itemKeys[x] + ".png")).resize((250,250), PILImage.ANTIALIAS)
photo = PhotoImage(file=im_temp)
label = Label(lookWindow, image=photo)
label.photo = photo
label.pack()
and now get this:
For python 2 you can do something like this, it should work for python 3 as well after small changes of import
from tkinter import Tk, Label
from PIL import Image, ImageTk
root = Tk()
file = 'plant001.png'
image = Image.open(file)
zoom = 0.5
#multiple image zise by zoom
pixels_x, pixels_y = tuple([int(zoom * x) for x in image.size])
img = ImageTk.PhotoImage(image.resize((pixels_x, pixels_y))) # the one-liner I used in my app
label = Label(root, image=img)
label.image = img # this feels redundant but the image didn't show up without it in my app
label.pack()
root.mainloop()
Instead of resizing those huge images on-the-fly, you could preprocess them before bunding them with your application. I took the 'key' and 'locked chest' images and placed them in a 'icons' subdirectory, then ran this code:
from PIL import Image
import glob
for infn in glob.glob("icons/*.png"):
if "-small" in infn: continue
outfn = infn.replace(".png", "-small.png")
im = Image.open(infn)
im.thumbnail((50, 50))
im.save(outfn)
It created a 'key-small.png' and 'locked-chest-small.png', which you can use in your application instead of the original images.
I have this code, a list of images.
self.image0 = gtk.Image()
self.image1 = gtk.Image()
self.image2 = gtk.Image()
[...]
self.image20 = gtk.Image()
self.img = # list of all this 20 images. How to write this?
I'm looking for a way to write this list with a short line.
Can you help me ?
Thanks
Using list comprehension:
self.img = [gtk.Image() for i in range(20)]
You can use list comprehensions like:
self.img = [gtk.Image() for _ in xrange(20)]
Images will be accessible via instance.img[0] but not via instance.image0 anymore.
I wanted to know how to display the integer and double values in the text box. As I have to calculate mean values of an image, and I want those values to be displayed in the text box in the GUI.
When I tried with my code I got an error:
AttributeError: numpy.ndarray object has no attribute set
This is because I'm using .set() for ndarray. But without .set() how to send the values to the textbox?
Here's my code snippet:
def open():
path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])
blue, green, red = cv2.split(re_img)
total = re_img.size
B = sum(blue) / total
G = sum(green) / total
R = sum(red) / total
B_mean1.append(B)
G_mean1.append(G)
R_mean1.append(R)
blue.set(B_mean)
root = Tk()
blue_label = Label(app,text = 'Blue Mean')
blue_label.place(x = 850,y = 140)
blue = IntVar(None)
blue_text = Entry(app,textvariable = blue)
blue_text.place(x = 1000,y = 140)
button = Button(app, text='Select an Image',command = open)
button.pack(padx = 1, pady = 1,anchor='ne')
button.place( x = 650, y = 60)
root.mainloop()
I have no idea whether my coding is wrong or not. And these mean values are being stored in list. Any suggestions for this problem?
Thanks for your support!
blue is a local name in your function, shadowing your global IntVar reference blue.
Rename one or the other.
There's nothing on numpy.ndarray called "set":
http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html
Take a good look at this reference and figure out how to apply B_mean to blue.
I am trying to create a function which will load a whole lot of images and map them to appropriate names in PyGame. I'm not all that great with python and this really has me stuck. My current code is this:
tile1 = pygame.image.load("/one.bmp")
tile2 = pygame.image.load("/two.bmp")
tile3 = pygame.image.load("/three.bmp")
and it keeps going on for about 20 tiles. The thing is I just found out that I need a lot more and was wondering how I could do this using a for x in y loop. My basic idea was:
tile = ['/one.bmp', '/two.bmp', '/three.bmp']
tilelist = [1,2,3]
for tile in tile:
tilelist[x] = pygame.image.load(tile)
or something like that but I'm not quite there. I was also wondering if it could be done using dictionaries.
Any help would be appreciated, thanks :)
List comprehensions to the rescue.
tiles = ['/one.bmp', '/two.bmp', '/three.bmp']
tilelist = [pygame.img.load(tile) for tile in tiles]
As #isakkarlsson commented,
...or easier(?) tilelist = map(pygame.img.load, tiles)
To load the data
tile = ['/one.bmp', '/two.bmp', '/three.bmp']
imageMap = {}
for t in tile:
imageMap[t] = pygame.img.load(t)
Then you have all the data in a dictionary and can loop through the file names using imageMap.keys() or the index directly into the dictionary to get a particular image.