Tkinter: Couldn't recognize data in image file - python

This question already has answers here:
Tkinter error: Couldn't recognize data in image file
(11 answers)
Closed yesterday.
I have the following problem. I generate .GIF files using an external application.
To me the GIFs look fine, I can open them without problems.
However, using
photo = PhotoImage(file=screenshot_file)
self.previewImageLabel.config(image=photo)
self.previewImageLabel.image = photo
to add an image to a Tkinter Label, is giving me the following error:
TclError: couldn't recognize data in image file "C:\Users\D8W\Python\hoang\combi
ne_model_neu\test_bauteile\SQTR-VO\prev_F45_SQTR.GIF"
The thing is, when I upload one of the GIFs to some online Converter and convert them to "GIF", they get displayed perfectly fine.
So my external application, which generates the GIFs, is putting something in there that Tkinter doesn't like.
How can I modify the GIFs to work? What are alternatives solving this problem?
I may not use modules that are not included in Python(like PIL).
Do you have any ideas?
Thanks in advance

I solved it, the problem was within my external application. I specified the file as .GIF but I have set the type to PNG.
Thanks anyway

Related

How can I use svgwrite.image.Image to set an image in the background of a canvas I have other elements on?

I have an svgwrite.Drawing into which I wish to insert a JPG image as the background image. Here's the code to what I have so far. It is using computer vision for a machine learning task. The essential part to help me begin understanding how I can insert an image, is on line 147 where I create a drawing using svgwrite. The problem I'm running into is that in the documentation, if you scroll down to where the factory methods are, it says that you can use Drawing.image and set an href for an image that you want to use. I did further down notice that in the factory methods it also has Drawing.saveas in the form svg_canvas.saveas, which does end up working for me so I know that I am using this Drawing method to create an svgwrite.image.Image object correctly, but it doesn't seem to be working. For reference, I insert the following code right after this line:
svg_canvas.image(href="/home/mendel/project/bg/bg.jpg", size = src_size)
I triple checked that my href was correctly written out and that my bg image was in fact in the folder. src_size is already defined before as well so I'm pretty sure it's all working. But I'm not sure why the above line isn't working for me at all to give me a background image for the canvas. Please let me know if I'm using it incorrectly I really appreciate it!
I already tried the above solutions using the Python API but it isn't working. I also tried looking into other stack posts, none have worked for me so far.

Downloading an image in Python does not properly display it

I am using PyCharm in order to download an image from a fixed URL
This is the code I'm using in order to do it:
import urllib.request
import random
def download_web_image(url):
name=random.randrange(0,1000)
fullname=str(name)+".jpg"
urllib.request.urlretrieve(url,fullname)
download_web_image('imagizer.imageshack.com/v2/626x626q90/673/MT82dR.jpg')
This is what happens when I double click the downloaded image:
But as you see the image is already downloaded in the Python directory and it is a proper image:
What do I have to do in order for the image to be properly displayed in Pycharm?
This has nothing to do with the fact that the image was downloaded using Python. You will see the same behaviour if you copy an image into your project folder. You can fix it by telling PyCharm about which file types should be displayed as images.
In Settings (File->Settings on Windows) expand Editor then select File Types
Choose Image from the list of Regognized File Types (scroll down...) then make sure that Registered Patterns contains all the file types you want to be displayed as images. If *.jpg is not listed there, add it using the green + on the right.

Is It Possible to Embed a Video In a Tkinter Window? [duplicate]

I posted a similar question a while ago but haven't received any answers so i thought i might ask a more generic question:
Does anyone know how to, through any means, output true video, not a rapidly changing sequence of images, in a Tkinter window with python?
You probably need an extension like TkVideo or Quicktime to do that, not sure there is a python style wrapper for it available yet.
https://github.com/patthoyts/tkvideo

Is it possible to save in a file an animation created with Tkinter?

I wanted to use Python to create animations (video) containing text and simple moving geometric objects (lines, rectangles, circles and so on).
In the book titled "Python 2.6 Graphics Cookbook" I found examples using Tkinter library. First, it looked like what I need. I was able to create simple animation but then I realized that in the end I want to have a file containing my animation (in gif or mp4 format). However, what I have, is an application with GUI running on my computer and showing me my animation.
Is there a simple way to save the animation that I see in my GUI in a file?
There is no simple way.
The question Programmatically generate video or animated GIF in Python? has answers related strictly to creating these files with python (ie: it doesn't mention tkinter).
The question How can I convert canvas content to an image? has answers related to saving the canvas as an image
You might be able to take the best answers from those two questions and combine them into a single program.
I've accomplished this before, but not in a particularly pretty way.
Tl;dr save your canvas as an image at each step of the iteration, use external tools to convert from image to gif
This won't require any external dependencies or new packages except having imagemagick already installed on your machine
Save the image
I assume that you're using a Tkinter canvas object. If you're posting actual images to the tk widgets, it will probably be much easier to save them; the tk canvas doesn't have a built-in save function except as postcript. Postscript might actually be fine for making the animation, but otherwise you can
Concurrently draw in PIL and save the PIL image https://www.daniweb.com/software-development/python/code/216929/saving-a-tkinter-canvas-drawing-python
Take a screenshot at every step, maybe using imagegrab http://effbot.org/imagingbook/imagegrab.htm
Converting the images to to an animation
Once the images are saved, I used imagemagick to dump them into either a gif, or into a mpg. You can run the command right from python using How to run imagemagick in the background from python or something similar. It also means that the process is implictely run on a separate thread, so it won't halt your program while it happens. You can query the file to find out when the process is done.
The command
convert ../location/*.ps -quality 100 ../location/animation.gif
should do the trick.
Quirks:
There are some small details, and the process isn't perfect. Imagemagick reads files in order, so you'll need to save the files so that alphabetical and chronological line up. Beware that the name
name9.ps
Is alphabetically greater than
name10.ps
From imagemagick's point of view.
If you don't have imagemagick, you can download it easily (its a super useful command-line tool to have) on linux and mac, and cygwin comes with it on windows. If you're worried about portability... well... PIL isn't standard either
There is a way of doing that, with the "recording screen method", this was explained in other question: "how can you record your screen in a gif?".
Click the link -->LICEcap : https://github.com/lepht/licecap
They say that it's free software for Mac (OS X) and Windows
You could look at Panda3D, but it could be a little over killed for what you need.
I would say you can use Blender3d too but i'm not really sure of how it works. Someone more experimented then me could tell you more about this.

Please call SetImage before attempting recognition.0 error by pytesser

i am trying to convert a text image into text. I am using pytesser in python for that I have already installed tesseract but on running even the following code:
from pytesser import *
im = Image.open('phototest.tif')
text = image_to_string(im)
print text
I get the following error:
Tesseract Open Source OCR Engine with Leptonica
Please call SetImage before attempting recognition.
and nothing gets printed nothing (no result).
Any help on the above problem?
Try converting your image to another format and see if you have the same issue. I had this same problem using Tesseract form the command line. I had a bmp file, I saved the files in my pre processing to png and tesseract worked fine.
As an alternative, you can use pytesseract, which will automatically convert your image and process it correctly.

Categories