Basically I want to import an image file and set it as a sprite/texture for an object while the game runs.
I just started coding so my code is a mess yet I will show you what I came up with.
I used a file dialog to select the file I wanted:
var selected = null
func _process(_delta):
if visible:
selected = get_current_path()
And then I tried to import the file I selected with load function and set it as a texture:
var tex = ImageTexture.new()
var img = Image
onready var imag = get_node("Sprite")
if Input.is_action_just_pressed("ui_home"):
img = load($FileDialog.selected)
tex.create_from_image(img)
imag.set_texture(tex)
But the thing is this only works for files within res:// folder. I can't import a file from a different directory which isn't already imported as a texture in godot editor. Help appreciated. Thanks!
Use Image.load(path) to load an image from an arbitrary path. Make sure to check the returned error code.
var image_path = "..."
var image = Image.new()
var error = image.load(image_path)
if error != OK:
# handle the error
var texture = ImageTexture.new()
texture.create_from_image(image)
sprite.texture = texture
I'm trying to read a PDF and insert an image at bottom(Footer) of each page in PDF. I've tried with PyMuPDF library.
Problem: Whatever the Rect (height, width) I give, it doesn't appear in the bottom, image keeps appearing only on the top half of the page (PDF page).
My code:
from fitz import fitz, Rect
doc = fitz.open("test_pdf2.pdf")
def add_footer(pdf):
img = open("footer4.jpg", "rb").read()
rect = Rect(0, 0, 570, 1230)
for i in range(0, pdf.pageCount):
page = pdf[i]
if not page._isWrapped:
page._wrapContents()
page.insertImage(rect, stream=img)
add_footer(doc)
doc.save('test_pdf5.pdf')
Processed_image with footer image in the middle of the page:
https://i.stack.imgur.com/HK9mm.png
Footer image:
https://i.stack.imgur.com/FRQYE.jpg
Please help!
Please let me know if this can be achieved by using any other library.
Some pages internally change the standard PDF geometry (which uses the bottom-left page point as (0, 0)) to something else, and at the same time do not encapsulate this doing properly as they should.
If you suspect this, use PyMuPDF methods to encapsulate the current page /Contents before you insert your items (text, images, etc.).
The easiest way is Page._wrapContents() which is also the friendly way to keep incremental PDF saves small. The MuPDF provided Page.cleanContents() does this and a lot more cleaning stuff, but bloats the incremental save delta, if this is a concern.
After doing little trial and error I was able to figure out the problem. I was missing out on proper dimensions of Rect, I had to give 0.85*h in second parameter.
below is the code:
from fitz import fitz, Rect
doc = fitz.open("test_pdf2.pdf")
w = 595
h = 842
def add_footer(pdf):
img = open("footer4.jpg", "rb").read()
rect = fitz.Rect(0, 0.85*h, w, h)
for i in range(0, pdf.pageCount):
page = pdf[i]
if not page._isWrapped:
page._wrapContents()
page.insertImage(rect, stream=img)
add_footer(doc)
doc.save('test_pdf5.pdf')
use:
wrapContents – Page.wrap_contents()
https://pymupdf.readthedocs.io/en/latest/znames.html?highlight=wrapContents
from fitz import fitz, Rect
doc = fitz.open("example.pdf")
w = 250
h = 400
def add_footer(pdf):
img = open("firmaJG.png", "rb").read()
rect = fitz.Rect(0, 0.85*h, w, h)
for i in range(0, pdf.pageCount):
page = pdf[i]
if not page._isWrapped:
page.wrap_contents()
page.insertImage(rect, stream=img)
add_footer(doc)
doc.save('test_pdf5.pdf')
I want to take a video from screen but it shouldn't use a while loop for taking the picture.I am using tkinter for my GUI.
I have tried after method for taking the picture every time when it needs to be taken. But it doesn't work appropriately. is there any way that I can do it without while true loop?
{def recording_loop(out):
"""take video by Imagegrab"""
img = ImageGrab.grab()
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
out.write(frame)
self.canvas.after(41, self.recording_loop, res))}
I expect that every 41ms the recording_loop revokes itself, so it can take 24 pictures in 1s (frame=24). but it doesn't work. Any help would appreciate. (out is output of cv2.videowriter(....) )
I want to keep transparent background at my 'image' variable.
If I write into a file, image looks fine. I mean image has a transparent background.
with urllib.request.urlopen(request) as response:
imgdata = response.read()
with open("temp_png_file.png", "wb") as output:
output.write(imgdata)
However, if I keep the image data in BytesIO, transparent background becomes a black background.
with urllib.request.urlopen(request) as response:
imgdata = response.read()
ioFile = io.BytesIO(imgdata)
img = Image.open(ioFile)
img.show()
(Above code piece, img.show line shows an image with black background.)
How can I keep a transparent image object in img variable ?
Two things...
Firstly, if you want and expect an RGBA image when opening a file with Pillow, it is best to convert whatever you get into that - else you may end up trying to display palette indices instead of RGB values:
So change this:
img = Image.open(ioFile)
to this:
img = Image.open(ioFile).convert('RGBA')
Secondly, OpenCV's imshow() can't handle transparency, so I tend to use Pillow's show() method instead. Like this:
from PIL import Image
# Do OpenCV stuff
...
...
# Now make OpenCV array into Pillow Image and display
Image.fromarray(numpyImage).show()
how to change the batch object of a sprite in pyglet?
def createSprite(obj, batch, layer):
img = pyglet.resource.image(obj.path)
img.width = obj.w
img.height = obj.h
return pyglet.sprite.Sprite(img, x=obj.x, y=obj.y, batch=batch, group=layer)
Very ambiguous question... But, good theme.
In pyglet the sprite objects have a property called batch that can be changed in ejecution time. Is very simple, asuming that x is a object of the Sprite class, then:
x.batch = pyglet.graphics.Batch()