Im using python turtle (Tkinter) to draw some lines which I need to export to a .jpg or .png file. To do so, I'm using python's turtle method to export my canvas to a postscript file:
pen.getcanvas().postscript(file="grafica.ps")
Where pen is just a fancy name for my turtle.
I get my .ps file, I convert it and... surprize! The image gets cut.
I tried some modifications like:
pen.getcanvas().postscript(file="grafica.ps", colormode='color', pagewidth=1600, pageheight=1200, width=1600, height=1200)
Since my turtle's window is 800x600 I thought that maybe twice as much space would be enough space to fit all the image but it still gets cut down...
I'm posting some output examples after the convertion, how my turtle's screen looks like when saving it, and how it should look exported.
Window while saving the image:
(Yes, there are sliders for the canvas)
How should it look:
And this is what I get:
I'm wondering how should I call postscript(), any idea?
I don't want to code this again on WxPython or other library :(
thanks!
This is probably a problem with ImageMagick interacting with the bounding box of the EPS file. My typical workflow for .eps files on Windows may be slightly convoluted, but it works. Similar thing should work for Linux. Install GhostScript (you'll have to make sure the GhostScript executables are on your path), then use the ps2pdf utility from the command line with the -dEPSCrop option:
ps2pdf -dEPSCrop input.eps output.pdf.
Then, use ImageMagick to convert the PDF to anything else, e.g. PNG
convert output.pdf output.png
You can control the PNG resolution etc. through ImageMagick. Like I said, convoluted, but it works.
Related
Windows 10 has a preview function for TTF fonts.
Does there exist a program that does sorta the same but for TXF fonts?
Or does anyone know of a python function that can load such a font so I can write such a program myself, PILLOW didn't seem to support it.
Note: I tried showtxf.exe, but it show an image in too low resolution cannot really make out how the font looks.
I know I'm a little late. But I found this question AND the solution when searching for the answer to the same question.
txf seems to be a private file format for a bitmapped texture font which was/can be used in the context of GLUT by Marc Kilgard, an OpenGL toolkit in the late nineties.
The sourcecode and makefile are still around, along with a little instruction and a tool to generate(!) txf files from ttf font files. Also included is a txf-file viewer (source) where you are supposed to see all glyphs crammed into one coherent bitmap image, IIRC.
I found the source package at this link:
http://chateau-logic.com/content/ttf-txf-font-conversion
Here is the situation. I use the Inkscape command to transform png file to svg file.
Like this in cmd:
inkscape image.png --export-plain-svg=image.svg
However, I need to remove the background of the svg file.
The GUI process is (1) open up the file, (2) select all(Alt + A) and (3) selecet "Path" -> Trace BitMap -> click the Remove Background. Finally, save it.
Is there any command which can insert in the terminal or cmd even in python to do this process???
i might have thousands of svg files...
What you're doing with your command is embedding a png image into an SVG image.
What you want to do is to trace a png image to turn it into vector data. I don't think tracing works without GUI currently. What you want is not possible with Inkscape 0.92.4.
You need to either:
use a different software or
write an extension that can run potrace without parameters, or with default parameters or
run potrace directly
do this manually
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.
I am trying to understand how I can use PIL in Python 2.7 to search the whole screen for a certain image and click on it. I've been searching around and haven't been able to find a solution. I want to create a small GUI with one button in the middle of it that when clicked will search the entire screen for a predefined image. Once the image is found the program will then click in the centre of it and end. In short the program will detect if an image is present on the users screen and click it.
I did find an interesting bit on Sikuli, but that doesn't help me because it's unable to export to an .exe.
The image that the program will look for will most likely be in the same place each time it searches, but I didn't want to hard-code the location as it has the potential to move and I don't want that being an issue later on.
What I need is the code method I would use to search for the image on screen and send back the cords to a variable.
Image explanation/example:
Reference image of rifle:
PIL is the wrong tool for this job. Instead you should look into openCV (open source computer vision), which has fantastic python bindings. Here is a link to an example (in C but should be easy to redo with the python bindings) that does what you are looking for, but even allows the image to be rotated, scaled, etc.
http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html
http://docs.opencv.org/doc/tutorials/features2d/detection_of_planar_objects/detection_of_planar_objects.html
Edit:
I assume you are using windows, as your example image looks like window. In this case you can use:
from PIL import ImageGrab
pil_img = ImageGrab.grab()
opencv_img = numpy.array(pil_img)
then use opencv to process the image to find sub image you are looking for.
If you want to do this cross platform, then you will need to use wxWidgets to do the screengrab: https://stackoverflow.com/a/10089645/455532
Even I wanted to do the same but using different module - pyautogui. I finally found the solution for my problem and I am sure this solution will also help you.
You have to just go to this webpage and read the locate function topic completely
and you'll be able to solve your problem.
I recommend you give a look on PyAutoGUI, a well documented library to control mouse and keyboard, also can locate imagens on screen, find the position, move the mouse to any location and clicks on location, also can simulate drag and drop, type on input fields, give double clicks and much more.
I am a beginner at programming at can't seem to figure out how to use bgpic('photo.gif') in turtle.
I have enclosed the 'photo.gif' in the same folder as the script. Every time I execute the program with bgpic, the turtle window crashes. Do I have to resize the picture? Change its format?
bgpic only accepts gif images.
You should convert your image to gif format with photoshop or another similar tool or alternatively use for example python PIL to make the conversion programmatically