I'm a newbie trying to write a small plugin for gimp using Python. It would generate an image and convert it to CMYK using the separate+ plugin
Code sample:
# some code that creates xcf image
layer = pdb.gimp_image_merge_visible_layers(newimage, CLIP_TO_IMAGE)
layer = pdb.plug_in_separate_light(some args here )
I have no idea how to call pdb.plug_in_separate_light() and then save the result image. I just know that pdb.plug_in_separate_light() requires 10 arguments and one of them should be a gimp drawable object(layer in my case) and two strings containing paths to icm/icc profiles. Any suggestions?
Related
After building and installing the Python engine shipped with Matlab 2019b in Anaconda
(TestEnvironment) PS C:\Program Files\MATLAB\R2019b\extern\engines\python> C:\Users\USER\Anaconda3\envs\TestEnvironment\python.exe .\setup.py build -b C:\Users\USER\MATLAB\build_temp install
for Python 3.7 I wrote a simple script to test a couple of features I'm interested in:
import matlab.engine as ml_e
# Start Matlab engine
eng = ml_e.start_matlab()
# Load MAT file into engine. The result is a dictionary
mat_file = "samples/lena.mat"
lenaMat = eng.load("samples/lena.mat")
print("Variables found in \"" + mat_file + "\"")
for key in lenaMat.keys():
print(key)
# print(lenaMat["lena512"])
# Use the variable from the MAT file to display it as an image
eng.imshow(lenaMat["lena512"], [])
I have a problem with imshow() (or any similar function that displays a figure in the Matlab GUI on the screen) namely that it shows quickly and then disappears, which - I guess - at least confirms that it is possible to use it. The only possibility to keep it on the screen is to add an infinite loop at the end:
while True:
continue
For obvious reasons this is not a good solution. I am not looking for a conversion of Matlab data to NumPy or similar and displaying it using matplotlib or similar third party libraries (I am aware that SciPy can load MAT files for example). The reason is simple - I would like to use Matlab (including loading whole environments) and for debugging purposes I'd like to be able to show this and that result without having to go through loops and hoops of converting the data manually.
I'm trying to make a plugin for gimp that opens two images as separate layers and transforms one of them (more on that below). I'm using GIMP 2.10.12.
I've been struggling to find a proper complete documentation for GIMP's Python interface and am mostly just working from what code snippets I've been able to find. This is what I have so far:
#!/usr/bin/env python2
import os
from gimpfu import *
def load_pair(img_f):
mask_f = img_f.replace(IMG_DIR, PRED_DIR)
result_f = os.path.splitext(img_f.replace(IMG_DIR, SAVE_DIR))[0]
result_dir = os.path.dirname(result_f)
if not os.path.isdir(result_dir):
os.makedirs(result_dir)
img = gimp.Image(100, 100)
pdb.gimp_display_new(img)
for f, name, pos in ((img_f, "Image", 0), (mask_f, "Mask", 1)):
layer = pdb.gimp_file_load_layer(img, f)
pdb.gimp_layer_set_name(layer, name)
pdb.gimp_image_insert_layer(img, layer, None, pos)
register(
"python_fu_open_image_pair",
...,
"<Toolbox>/Image/Open Image Pair",
"",
[(PF_FILE, "img_f", "Image:", None)],
[],
load_pair
)
main()
This kind of does what I want but with a couple of problems.
Question 1
Currently I'm using gimp.Image(100, 100) to open a new image. This means I have to then Fit Canvas to Layers and adjust the zoom and position every time I load a new image pair.
Is there a way to find an image's size from pdb before opening it or do I have to use another library (like PIL) for this? I'm trying to keep my plugin's dependencies to a minimum.
The two images are guaranteed to have the same size.
Since File->Open automatically adjusts the canvas to the image size, I would hope there'd be a nice way to achieve this.
Question 2
I would like to automatically create and set the current working file to result_f + '.xcf' (see above code) - such that File -> Save would automatically save to this file. Is this possible in pdb?
Question 3
Most importantly, I currently have the Mask images saved as black-and-white images. Upon loading a mask as a new layer, I'd like to transform the black colour to transparent and white colour to green (0,255,0). Additionally, since they are saved as .jpg images, the white and black aren't necessarily exactly 255 and 0 intensities but can be off by a bit.
How do I do this automatically in my plugin?
The good way would be to load the first image normally, and the rest as additional layers. Otherwise you can reset the canvas size (pdb.gimp_image_resize(...)) once you have loaded all the layers, and then create the Display.
You can give a name and a default file to the image by setting image.name and image.filename.
To convert the white to green use pdb.plug_in_colors_channel_mixer(...) and set all the gains to 0., except green in green. Make the black transparent use pdb.plug_in_colortoalpha(...).
PS: For color2alpha:
import gimpcolor
color=gimpcolor.RGB(0,255,0) # green, integer args: 0->255)
# or
color=gimpcolor.RGB(0.,1.,0) # green, floating point args (0.->1.)
pdb.plug_in_colortoalpha(image, layer, color)
The Python doc is a direct copy of the Scheme one. In Python, the RUN-INTERACTIVE parameter is not positional, so it doesn't appear in most calls, if you need it, it is a keyword parameter.
I do not understand if sitk.ReadImage can read a list of images or not? I did not manage to find an example showing how to list of images should be inputed to the function.
But in the function documentations it say:
ReadImage(**VectorString fileNames**, itk::simple::PixelIDValueEnum outputPixelType) -> Image
ReadImage(std::string const & filename, itk::simple::PixelIDValueEnum outputPixelType) -> Image
ReadImage is a procedural interface to the ImageSeriesReader class which is convenient for most image reading tasks.
Note that when reading a series of images that have meta-data
associated with them (e.g. a DICOM series) the resulting image will
have an empty meta-data dictionary. It is possible to programmatically
add a meta-data dictionary to the compounded image by reading in one
or more images from the series using the ImageFileReader class,
analyzing the meta-dictionary associated with each of those images and
creating one that is relevant for the compounded image.
So it seems from the documentations that it is possible. Can someone show me a simple example.
EDIT:
I tried the following:
sitk.ReadImage(['volume00001.mhd','volume00002.mhd'])
but this is the error that I get:
RuntimeErrorTraceback (most recent call last)
<ipython-input-42-85abf82c3afa> in <module>()
1 files = [f for f in os.listdir('.') if 'mhd' in f]
2 print(sorted_files[1:25])
----> 3 sitk.ReadImage(['volume00001.mhd','volume00002.mhd'])
/gpfs/bbp.cscs.ch/home/amsalem/anaconda2/lib/python2.7/site-packages/SimpleITK/SimpleITK.pyc in ReadImage(*args)
8330
8331 """
-> 8332 return _SimpleITK.ReadImage(*args)
8333 class HashImageFilter(ProcessObject):
8334 """
RuntimeError: Exception thrown in SimpleITK ReadImage: /tmp/SimpleITK/Code/IO/src/sitkImageSeriesReader.cxx:145:
sitk::ERROR: The file in the series have unsupported 3 dimensions.
Thanks.
SimpleITK uses SWIG to wrap a C++ interface to Insight Segmentation and Registration Toolkit (ITK). As such the inline python documentation should be supplemented with the C++ Doxygen documentation. There is a mapping of C++ types to Python types with robust implicit conversion between them. You can find the documentation for the sitk::ReadImage methods here:
https://itk.org/SimpleITKDoxygen/html/namespaceitk_1_1simple.html#ae3b678b5b043c5a8c93aa616d5ee574c
Notice there are 2 ReadImage methods, and the Python docstring you listed appears is one of them.
From the SimpleITK examples here is a snippet to read a DICOM series:
print( "Reading Dicom directory:", sys.argv[1] )
reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames( sys.argv[1] )
reader.SetFileNames(dicom_names)
image = reader.Execute()
This uses the class interface as opposed to the procedural. Which would simply be:
image = sitk.ReadImage(dicom_names)
or generically with a list of string filenames:
image = sitk.ReadImage(["image1.png", "image2.png"...])
Many common array like type of strings will be implicitly converted to the SWIG VectorString type.
The instructions are simple enough in the Wand docs for reading a sequenced image (e.g. animated gif, icon file, etc.):
>>> from wand.image import Image
>>> with Image(filename='sequence-animation.gif') as image:
... len(image.sequence)
...but I'm not sure how to create one.
In Ruby this is easy using RMagick, since you have ImageLists. (see my gist for an example.)
I tried creating an Image (as the "container") and instantiating each SingleImage with an image path, but I'm pretty sure that's wrong, especially since the constructor documentation for SingleImage doesn't look for use by the end-user.
I also tried creating a wand.sequence.Sequence and going from that angle, but hit a dead-end as well. I feel very lost.
The best examples are located in the unit-tests shipped with the code. wand/tests/sequence_test.py for example.
For creating an animated gif with wand, remember to load the image into the sequence, and then set the additional delay/optimize handling after all frames are loaded.
from wand.image import Image
with Image() as wand:
# Add new frames into sequance
with Image(filename='1.png') as one:
wand.sequence.append(one)
with Image(filename='2.png') as two:
wand.sequence.append(two)
with Image(filename='3.png') as three:
wand.sequence.append(three)
# Create progressive delay for each frame
for cursor in range(3):
with wand.sequence[cursor] as frame:
frame.delay = 10 * (cursor + 1)
# Set layer type
wand.type = 'optimize'
wand.save(filename='animated.gif')
From stackoverflow I learned how to set image properties in LibreOffice Writer with pyhton macros, via com.sun.star.text.WrapTextMode. Now I use that to set the text wrap to THOUGHT. Now I would like to set the image to background, like a watermark.
In LibreOffice Writer interactively I select an image, right-click on it and the context menu contains the "Wrap" commands, one is "Wrap Through" and the other one is "In Background".
In the python macro I have the following code (from Insert several images at once using macro scripting in LibreOffice and from the often quoted Andrew Pitonyak):
from com.sun.star.text.WrapTextMode import THROUGHT
and then to insert the image:
img = doc.createInstance('com.sun.star.text.TextGraphicObject')
element_url = 'file://' + file_name
img.GraphicURL = element_url
img.Surround = THROUGHT
text.insertTextContent(cursor, img, False)
So what is the code to put it "In Background"?
MRI shows that setting "In Background" causes the Opaque attribute to be false. So add this to the code:
img.Opaque = False
By the way, Surround is deprecated. Try setting TextWrap to THROUGHT instead.