Make part of image transparent with pyglet - opengl

I have two images, "desert" image over "winter" image:
import pyglet
desert_img = pyglet.image.load('assets/desert.jpg')
desert = pyglet.sprite.Sprite(desert_img, x=50, y=50)
winter_img = pyglet.image.load('assets/winter.jpg')
winter = pyglet.sprite.Sprite(winter_img, x=0, y=0)
window = pyglet.window.Window()
#window.event
def on_draw():
winter.draw()
desert.draw()
pyglet.app.run()
Result is:
I would like draw a square of "transparency" on desert image (winter image should be visible in this square). Is it possible ? How to do that ?
I found many question who permit to make transparency with image itself (png, alpha ... but no like i want).

Based on Torxed suggestion, replace image content with transparent bytes where we want to make it transparent:
import io
from PIL import Image
import pyglet
from PIL.PngImagePlugin import PngImageFile
def replace_content_with_transparency(img: PngImageFile, x, y, width, height):
pixels = img.load()
for i in range(x, width):
for j in range(y, height):
pixels[i, j] = (0, 0, 0, 0)
desert_png = Image.open('assets/desert.png')
replace_content_with_transparency(desert_png, 32, 32, 123, 123)
fake_file = io.BytesIO()
desert_png.save(fake_file, format='PNG')
desert_img = pyglet.image.load('img.png', file=fake_file)
desert = pyglet.sprite.Sprite(desert_img, x=50, y=50)
winter_img = pyglet.image.load('assets/winter.jpg')
winter = pyglet.sprite.Sprite(winter_img, x=0, y=0)
window = pyglet.window.Window()
#window.event
def on_draw():
winter.draw()
desert.draw()
pyglet.app.run()

Related

Python 2.7, Tkinter, make a point "trace" a waveform

I'm trying to make a widget that demonstrates the relationship between a sine wave and it's phasor diagram. I want a pointer that follows the user's mouse movements but instead of freely moving around the page, I want it to stick to the waveform I've plotted and only follow the co-ordinates that make up that line. Kind of like a slider but along a sine wave! Any suggestions? Thanks. Here's what I've got so far;
import math
import Tkinter as tk
from Tkinter import PhotoImage, Canvas
from PIL import Image, ImageTk
sinwidget = tk.Tk()
main_canvas = tk.Canvas(sinwidget, width = 400, height = 400, bg= "white")
main_canvas.pack()
def callback(event):
canvas = event.widget
x = canvas.canvasx(event.x)
y = canvas.canvasy(event.y)
print canvas.find_closest(x, y)
draw(event.x, event.y)
def draw(x, y):
box.coords(pointer, x-20, y-20, x+20, y+20)
def exit_():
sinwidget.destroy()
box = main_canvas
box.bind('<Motion>', callback)
box.pack()
pointer = box.create_rectangle(0.2,0.2,0.2,0.2)
wavelength = 360
height = 400
center = height//2
degree = 1
increment = 0.0175
amplitude = -80
sin = []
for x in range(360):
sin.append(x * degree)
sin.append(int(math.sin(x * increment) * amplitude) + center)
sinwave = main_canvas.create_line(sin, fill="red", width=2.0)
xaxis = main_canvas.create_line(0, center, wavelength, center, fill="black",
width=3.0)
yaxis = main_canvas.create_line(2, 100, 2, 300, fill="black", width=3.0)
exit_button = tk.Button(sinwidget, text = "exit", command = exit_)
exit_button.pack()
sinwidget.mainloop()
Thanks for any help you can offer!

Issue with script using Pygame

I'm writing a small game in my spare time. This is what I have so far:
from pygame import * #import relevant modules
from PIL import Image
import os, sys
init() #initialise
class sprite:
def __init__(self, object, x = 0, y = 0, w = 0, h = 0):
self.image = image.load(object).convert()
self.posx = x
self.posy = y
self.position = ((x, y, w, h))
def resize(self, sh, sw):
self.image = transform.scale(self.image, (sh, sw))
return self.image
def move(self, window, background, right, down):
self.posx = x + right
self.posy = y + down
window.blit(background, self.position, self.position)
self.position.move(right, down)
window.blit(self, self.position)
window.update()
os.chdir('C:\\Users\\alesi\\Documents\\Pygame\\Project\\') #current folder change
win = display.set_mode((736, 552))#load window
Clock = time.Clock() #handy clock
background = image.load('background.jpg').convert()#load images
player = sprite('ball.png', 350, 275, 20, 20)
player = player.resize(20, 20)
win.blit(background, (0, 0))
win.blit(player, (350, 275))
display.update()
while True:
event.pump()
keys = key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
elif keys[K_RIGHT]:
player.move(win, background, 20, 0)
elif keys[K_LEFT]:
player.move(win, background, -20, 0)
elif keys[K_DOWN]:
player.move(win, background,0, 20)
elif keys[K_UP]:
player.move(win, background, 0, -20)
In short, it should create an object on a background and allow you to move the object using the arrow keys. However, I get the error:
C:\Users\alesi\Documents\Pygame\Project>python2 game2.py
Traceback (most recent call last):
File "game2.py", line 51, in <module>
player.move(win, background, -20, 0)
AttributeError: 'pygame.Surface' object has no attribute 'move'
I'm struggling to understand why my player instance of the sprite class is not recognising the move method. Also, I'm confused by why during the win.blit() function, I have to give the argument player instead of player.image, the attribute which I've stored the image.
Any advice would be appreciated.
In code
def resize(self, sh, sw):
self.image = transform.scale(self.image, (sh, sw))
return self.image
you returns image which is Surface instance - so in line
player = player.resize(20, 20)
you replace sprite instance with Surface instance
But you don't have to assign it to player again.
Do:
def resize(self, sh, sw):
self.image = transform.scale(self.image, (sh, sw))
# without return
# without player =
player.resize(20, 20)
After that player.move(...) will work again.
And again you will have to use player.image in blit()

How to use bitmap to load any image format in wxpython

import wx
import sys, glob
class DemoFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1,"wx.ListCtrl in wx.LC_ICON mode",size=(600,400))
il_max=0
il = wx.ImageList(200,200)
for name in glob.glob("img/*.bmp"):
bmp = wx.Bitmap(name, wx.BITMAP_TYPE_BMP)
il_max = il.Add(bmp)
self.list = wx.ListCtrl(self, -1, style=wx.LC_ICON | wx.LC_AUTOARRANGE)
self.list.AssignImageList(il, wx.IMAGE_LIST_NORMAL)
for x in range(25):
img = x % (il_max+1)
self.list.InsertImageStringItem(x, "This is item %02d" % x, img)
app = wx.PySimpleApp()
frame = DemoFrame()
frame.Show()
app.MainLoop()
This code only work with bmp not with jpeg or png
What suitable change is required to get it work with any image format?
change
bmp = wx.Bitmap(name, wx.BITMAP_TYPE_BMP)
to
bmp = wx.Bitmap(name, wx.BITMAP_TYPE_ANY)
There's also
BITMAP_TYPE_CUR
BITMAP_TYPE_ICON
BITMAP_TYPE_IFF
BITMAP_TYPE_GIF
BITMAP_TYPE_JPEG
BITMAP_TYPE_PNG
BITMAP_TYPE_TIF
....among others

How to keep a Tkinter widget on top of the others

I have some code that moves images left and right but I do not want them to appear on top of the right border, which I draw as a rectangle.
What are the options in Tkinter to keep a widget (in my example a rectangle) on top of some other widgets (in my code a tile, which is an image)?
I am drawing the rectangle and the image on one canvas.
I can image that using two canvas could do the trick, but are there any other options/settings?
Thanks
import Tkinter as tk # for Python2
import PIL.Image, PIL.ImageTk
win = tk.Tk()
#Create a canvas
canvas = tk.Canvas(win, height = 500, width = 500)
#Create a rectangle on the right of the canvas
rect = canvas.create_rectangle(250, 0, 500, 250, width = 2, fill = "red")
#Create an image
SPRITE = PIL.Image.open("sprite.png")
tilePIL = SPRITE.resize((100, 100))
tilePI = PIL.ImageTk.PhotoImage(tilePIL)
tile = canvas.create_image(100, 100, image = tilePI, tags = "a tag")
#Place the canvas
canvas.grid(row = 1, column = 0, rowspan = 5)
#Move the tile to the right.
#The tile will go on top of red rectangle. How to keep the rectangle on top of the tile?
canvas.coords(tile, (300, 100))
canvas.mainloop()
Use tag_raise() method:
canvas.tag_raise(tile)

Transparent pngs with Django and matplotlib

I currently have a Django project, with a view function, with code like the following (code copied from this post):
from pylab import figure, axes, pie, title
from matplotlib.backends.backend_agg import FigureCanvasAgg
def test_matplotlib(request):
f = figure(1, figsize=(6,6))
ax = axes([0.1, 0.1, 0.8, 0.8])
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15,30,45, 10]
explode=(0, 0.05, 0, 0)
pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})
canvas = FigureCanvasAgg(f)
response = HttpResponse(content_type='image/png')
canvas.print_png(response)
return response
It creates a png from a graph and serves it directly. How can I make the background of the graph transparent in this png?
Untested, but this should work. You don't have to write to an (actual) file, a file-like object should work. This code saves the image into the response.
def test_matplotlib(request):
f = figure(1, figsize=(6,6))
ax = axes([0.1, 0.1, 0.8, 0.8])
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15,30,45, 10]
explode=(0, 0.05, 0, 0)
pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})
#canvas = FigureCanvasAgg(f)
response = HttpResponse(content_type='image/png')
f.savefig(response, transparent=True, format='png')
#canvas.print_png(response)
return response