Open files with python script in Gimp like File->Open - python

TL;DR: How to open an Image in Gimp with Python like you do with (File->Open) in order to get the Overwrite file.png option and avoid the Save the changes to image 'sample.png' before closing? Dialog.
I wrote a Python script for Gimp which does a few modifications on a selected area, and then saves it as a PNG. After that I want to open the file so the user can check, whether everything is correct or change something. Since the users of the plugin dont necessarily know how to use Gimp, so the "Save the changes to image 'sample.png' before closing?" Dialog might be confusing.
The method I currently use is
pdb.file_png_save(image, drw, new_file, new_file,0,9,0,0,0,0,0)
gimp.Display(pdb.gimp_file_load(new_file, new_file))

This cleans the “dirty” bit – you will not be asked to save the image if you close it afterwards (unless you do further edits):
image.clean_all()
See it referred to here: https://www.gimp.org/docs/python/

Have a look at gimp_image_clean_all. This resets the image's dirty flag and allows it to be closed.
Note that you can't prevent it from being set again if the users change something in the image.

Related

Sikuli: How to prompt programatically to capture a region and save it as png in cwd

Instead of providing beforehand all the images as png to find UI elements on screen, I want to have an option for user to capture the region first time the program is run. For example, when program starts, I want user to provide image for button that fires a command and then from next time onwards, I will use that image to find that element and no user input is needed.
So basically, there is one user setup step which will capture all UI elements in the form of png images.
So question is - how to invoke region capture and wait for user to provide the region preferably by dragging and dropping mouse. At the end of this prompt, I should get a png which I will use in subsequent region.find calls. There is "Take screenshot" available in sikuli IDE. Is it exposed programatically? Also, I am working on Windows.
Please have a look at the docs here.
With the respective setting you will be prompted for an action on FindFailed/ImageMissing. One possible action is to (re)capture the image.
To make it work in your sense, you have to name the image in advance:
find("image1")
On prompt and capture the image will be named image1.png.
RaiMan from SikuliX

PIL-pillow's `Image.show()` removes temporary image before shown with okular

When I try to display a pil image using the XDGViewer viewer, okular (I am on kde, and okular is my default image viewer) displays a message it can't open the file. Indeed, it is not present in the /tmp when I look for it.
Since it works as expected with DisplayViewer (and the file is present until I close the window), I suspect xdg-open to launch okular and quit immediately, and then, since xdg closed, the file is removed immediately before okular can access it.
Is there a way to prevent deleting the temporary file (my /tmp is mounted on tmpfs, thus it's not really a problem...) ? for example, deleting the temporary file eigher when the image gets garbage-collected or when the interpreter closes ?

How to save an image as a lossless webp using only GIMP's python-fu

I have a python-fu question.
I am using GIMP 2.10.12, mac version.
Let's say I have an image open in GIMP
Original example image:
and I want to save it in webp format using python-fu. I do this:
img=gimp.image_list()[0]
layer=img.layers[0]
pdb.gimp_file_save(img,layer,'/my_path/my_image.webp','?')
It works, but when I check the resulting webp file turns out its quality has degraded. It's being saved with lossy compression.
Resulting lossy image:
If I do the same process with the GUI, in the dialog box there's the checkbox for lossless compression, which I checked, and the resulting file has the quality that I want.
Gimp dialog box with the lossless option checked:
My question is: How can I save an image as a webp file with the lossless compression option checked using only Python-fu, not the GUI?
I have tried:
-Flattening the image first to take away the alpha channel, didn't work.
-Looking in GIMP's developer documentation, but haven't found anything yet.
-Looking in the preferences panel for a way to leave the lossless option checked by default. Haven't found anything yet.
Am I missing something? Any pointers will be welcome. Thanks.
When you save an image with gimp-file-save Gimp determines the image type by looking at the extension you provide in the name, and saves it with the defaults settings that are set for this type. With some image types (JPEG & PNG), one solution is to change Gimp's default options, but there is no way to change them for WebP.
However the general solution is that there are type-specific export/save functions (normally called file-${type}-save), and these have all the options shown in the export dialogs. For WebP you would use file-webp-save(*):
which, as you can see has a lossless option.
I never used that one but word of warning: normally these calls only save a "drawable", i.e., in most cases, a single layer. If you have a multi-layer image and want to save the whole image, you have to flatten it (or save a flattened duplicate) or create a temporary layer to save using gimp_layer_new_from_visible).
(*) If you haven't found it yet, Filters > Python-fu > Console gives you a console to try things, and in that window a Browse button sends you to the procedure browser where everything is documented (this doc is generated dynamically and takes in account all the plugins you may have added).

How to Create a Restricted File Browser in Python for Windows

I’d like to create a restricted folder/ file explorer in Python (I have version 2.7.9, but I don’t mind changing that) for Windows.
Essentially, I want to initially specify the folder to which the code opens. For example, the code should initially open to: C:\Users\myName\Desktop\myDemoFolder (the user must not know this folder simply by looking at the GUI).
The user must be able to browse downwards (deeper into folders) and backwards (but only up to the initial folder to which the code opens). The user must be able to click to open a file (for example: pdf), and the file must automatically open in its default application.
An example of what I’d like is presented in figure 1. (The look of the interface is not important)
Currently, I am able to get figure 2 using the code presented here:
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
print(filename)
Research has indicated that it is not possible to change the default buttons in Tkinter windows. Is this true? If it can’t be done with Tkinter (and that’s fine), how else can we do it?
I’d happily choose simple, non-Tkinter code (perhaps using wxPython’s wx.GenericDirCtrl()) rather than elaborate Tkinter code, but no restrictive libraries please.
A modular design approach is not needed. I’d rather have simple (functional) code that is shorter than object-oriented code.
I was trying to do the same thing when I realized that maybe you could create all the buttons you need and then set the color of the buttons you don't need to your background color using:
button-name.config(bg = "background-color")
Just change the "button-name" to your button's name and set "background-color" to the background color!

adding a quit timer that closes a photo

After many hours of searching online and in my python book I can't seem to find the answer to my question which is what do I add to my code so I can put in a timer that automatically closes the photo? It pulls itself up but then I have to manually close the photo to get back to my main program. Any help would be appreciated.
from PIL import Image
img = Image.open('battleship load screen.png')
img.show()
This is not possible using PIL alone - img.show() is just launching another program, it's intended for debugging really, not for presenting things to the user.
From the docs.
Displays an image. This method is mainly intended for debugging
purposes.
On Unix platforms, this method saves the image to a temporary PPM
file, and calls the xv utility.
On Windows, it saves the image to a temporary BMP file, and uses the
standard BMP display utility to show it.
This method returns None.
If you want to display an image and have control over it, use a graphical toolkit and construct a UI for your purpose. I've linked there to an example using PySide, a set of QT bindings, but of course you could use any toolkit - each will be different.

Categories

Resources