Python and Kivy: How to update Image from memory? - python-3.x

I'm currently trying to display an image with kivy on a Raspbery Pi 4. The problem is that no image is shown on the Image widget. Also no Error or something else is given out to the terminal.
This is what I came up with in show_picture() in .py file:
image = camera.takePicture()
encode_param = [int(cv.IMWRITE_JPEG_QUALITY), 95]
result, enc_img = cv.imencode('.jpg', image, encode_param)
data = io.BytesIO(enc_img)
texture_ = CoreImage(data, ext="jpg").texture
The texture_ object was declared and initialized at class level with:
texture_ = StringProperty(None)
And in the .kv file I have following for the Image widget:
Image:
id: imageView
source: app.texture_
allow_stretch: True
I want the image to be updated via this method so that the gui shows the latest taken image. If someone has a different approach I'm also happy with it.
Thanks in advance.
Edit: messed something up when copying code

Related

Displaying Images Using Python Events

I am creating a small program that should display an image overlayed the main window based on a button click event. However, whenever I test the code that should be doing this, I get this error: AttributeError: 'int' object has no attribute '_create'
Here is the appropriate code:
def display_image(event):
fail_image = tk.PhotoImage(file="./RedXMark.png")
Canvas.create_image(0, 0, image=fail_image, anchor="nw")
# Later in the code:
root.bind('<t>', display_image)
I have been trying to use Tkinter's labels to display the image and, while I wouldn't get an error, it wouldn't display the image. The image is in the same folder as the program and is saved with that exact name.
Any advice would be welcome!
You must call create_image on an instance of the Canvas class, but you're calling it on the class itself.
something = tk.Canvas(...)
...
something.create_image(0, 0, image=fail_image, anchor="nw")

How to create folder in my android file manager via kivy android application

I'm trying to develop android application by using kivy. For testing I need to select an image from my internal storage, convert it into gray scale image and it needs to be stored in a particular folder.
I was wondering how to achieve this, every option is welcome.
Thanks in advance
If I understand correctly you are trying to show an image from a particular folder. You can achieve this by adding a image to your .kv file:
Image:
source: '/myfolder/mylogo.png'
Or if you are getting the path from you python file. You can use get the path by calling the function in your kv file which you can use this:
<MyScreen>:
on_pre_enter: get_image()
image:
size: self.width self.height

Image not Displaying even after following effbot [duplicate]

This question already has an answer here:
PhotoImage not showing up the image associated with it
(1 answer)
Closed 3 years ago.
I'm trying to add in an image to a screen and its not displaying.
Followed other Stack overflow solutions and the Effbot solution and neither worked. Ive moved the image around and it still isnt displaying so its not where its displaying. Not getting any Error messages either.
image = PhotoImage("newspaper-extra-computer-icons-breaking-newsnewspaper.jpg")
image_label = Label(news_aggregator,image = image)
image_label.image = image
image_label.place(x=400,y = 200)
Just expecting the image to be displayed.
The main problem is you are missing file= in PhotoImage().
Try this:
PhotoImage(file='path_to_image.gif')
That said tkinter only supports a hand full of formats.
The PhotoImage class can read GIF and PGM/PPM images from files
For working with other formats you will need PIL.
If you need to work with other file formats, the Python Imaging Library (PIL) contains classes that lets you load images in over 30 formats, and convert them to Tkinter-compatible image objects:
You can see all there here on the documentation.
For your file try this:
import tkinter as tk
from PIL import ImageTk
root = tk.Tk()
image = ImageTk.PhotoImage(file="newspaper-extra-computer-icons-breaking-newsnewspaper.jpg")
image_label = tk.Label(root, image=image)
image_label.image = image
image_label.place(x=400, y=200)
root.mainloop()
Keep in mind due to you using place() the above code will not be showing the image as it is off frame. You will need to expand the window.

How to draw a picture by OpenCV within Gtk in Python

I am struggling with the integration of OpenCV with in my Python Gtk application. What I am trying to do, is to load a picture like this:
pic = cv2.imread("image.jpg") #pic is a numpy.ndarray
Afterwards, I would like to display this image using a GtkWidget. But actually I have no idea how. My first idea was to use something like GtkImage, but GtkImage can only work directly with an image file. Since my plan is to manipulate a video-stream this is not practicable.
Maybe one of you has an idea how to proceed. Thank you very much.
I found a solution. So for anyone how is interested I did it like this:
self.frame = Gtk.Frame()
pic = cv2.imread("image.jpg")
pic = cv2.resize(pic, (400,600))
pic = np.array(pic).ravel()
print(pic.size)
pixbuf = GdkPixbuf.Pixbuf.new_from_data(pic,GdkPixbuf.Colorspace.RGB, False, 8, 600, 400, 3*600)
Image = Gtk.Image.new_from_pixbuf(pixbuf)
self.frame.add(Image)

watermark in image using nodejs

I have added watermark on image using node_module 'gm', i had make font color grey but i found an bug in that if image content grey color so my watermark merged with image so i can't read the watermark text, that's why i want to create an overlay between image and watermark text but i don't know how i achieve it my current code is
const image = gm(__dirname+'/download.jpeg').fill('#ffffff').font('Arial', 10, '#ffffff').drawText(10, 20, "some text");
image.write('result.png', err => {
if(err) return console.error(err);
});
and i don't know how to make overlay between them my actual image is
and my watermarked image is
any help should be appreciate thanks in advance
So there is no such module that can give you the expected result.
But I faced the same problem & came across this module "node-caption" but again this module is also not giving us the expected result. So I have tweaked this module a little bit & made it work as expected.
Here is the URL to the modified module of my git hub repo https://github.com/mohsincynexis/node-caption.
The changes are only in the generate.js file.
Here is the attached image after watermark.

Resources