How to combine several PNG images as layers in a single XCF image? - png

I have several PNG images, which I need to combine as individual layers in a new GIMP XCF image. I need to perform this task many times, so a script based solution would be best.
So far i tried to play around with the batch mode of GIMP, but failed miserably.

Instead of script-fu, which uses Scheme, I'd recommend using the GIMP-Python binding for this, since it is far easier to manipulate files and listings.
If you check filters->Python->Console you will b dropped into an interactive mode - at the bottom of it, there will be a "Browse" button which lets you select any of GIMP's procedures in its API and paste it directly in this console.
There is as an API call to "load a file as a layer" - pdb.gimp_file_load_layer -
this however, brings the image to memory, but do not add it to the image - you have to call
pdb.gimp_image_insert_layer afterwards
You can type this directly in the interactive console, or,check one of my other GIMP-related answers, or some resource on GIMP-Python on the web to convert it to a plug-in, which won't require pasting this code each time you want to perform the task:
def open_images_as_layers(img, image_file_list):
for image_name in image_file_list:
layer = pdb.gimp_file_load_layer(image_name)
pdb.gimp_image_insert_layer(img, layer, None, 0)
img = gimp.image_list()[0]
image_list = "temp1.png temp2.png temp3.png"
open_images_as_layers(img, image_list.split())
The second to last line img = ... picks a needed reference to an open image
in GIMP - you could also create a new image using pdb calls if you'd prefer
(example bellow).
The file list is a hardcoded space separated string in the snippet above,
but you can create the file list in any of the ways allowed by Python.
For example, to get all the ".png" file names in a
c:\Documents and Settings\My user\Pictures folder, you could do:
from glob import glob
image_list = glob("c:/Documents and Settings/My user/Pictures/*png")
To create an image programatically:
img = gimp.Image(1024, 768)
pdb.gimp_display_new(img)

Related

Houdini flipbook default output location

I would like to set a default output location for the Houdini flipbook so that all users have a standard path to save and look for their flipbook sequences rather than everyone inputting their own save locations from the gui. Is there an option or setting for that, preferably something I could set with an environment variable?
This script might help.
Just create a new tool and paste code into script tab.
Once you click on the tool, it will load up the flipbook and save jpg sequence to disk.
Also make sure to create "resx" and "resy" here, here, and here
PS: double check if folder you are outputing JPGs to exists.
import os
import toolutils
filename = "---YOURPATH---/$HIPNAME/$HIPNAME.$F4.jpg"
scene = toolutils.sceneViewer()
setting = scene.flipbookSettings().stash()
res = (int(hou.getenv("resx")), int(hou.getenv("resy")))
range = hou.playbar.playbackRange()
setting.frameRange(range)
setting.output(filename)
setting.useResolution(True)
setting.motionBlurSegments(10)
setting.resolution(res)
setting.useMotionBlur(False)
scene.flipbook(scene.curViewport(), settings)

Why does DITA Open Toolkit PDF plugin rename image href attributes?

I'm sorry if this doesn't have enough information. I don't typically ask for help online like this.
I'm using DITA Open Toolkit 3.4 on Windows. I generated a plugin called "vcr2" using Jarno's (very excellent and helpful) PDF Plugin Generator and then made a handful of customizations. The plugin uses the pdf2 plugin as a base. When I try to use the vcr2 plugin, my images are not working. I've tracked the problem down to malformed image filenames in the image's href attribute.
For example:
In my source file (a DITA Task), the markup for one of my images looks like this:
<image href="MyRemindersChooseReminder.png"/>
If I run a transform with the pdf2 plugin, the images work fine. In the merged stage1.xml file in the Temp folder, the XML for that same image looks like this:
<image class="- topic/image " href="df2d132af27436c59c5c8c4282e112d62bec8201.png" placement="inline" xtrc="image:1;10:66" xtrf="file:/V:/Vasont/Extract/t12340879-minimal/t12340879.xml"/>
It is processed into a file Topic.fo, and looks like this:
<fo:external-graphic
 src="url('file:/V:/Vasont/Extract/t12340879-minimal/MyRemindersChooseReminder.png')"/>
Everything works fine and the image looks fine.
If I run the same file through my 'vcr2' plugin, which just calls the same pdf2 plugin with some overrides, all the images get broken:
stage1.xml
<image class="- topic/image " href="df2d132af27436c59c5c8c4282e112d62bec8201.png" placement="inline" xtrc="image:1;10:66" xtrf="file:/V:/Vasont/Extract/t12340879-minimal/t12340879.xml"/>
Topic.fo
<fo:external-graphic
 src="url('file:/V:/Vasont/Extract/t12340879-minimal/df2d132af27436c59c5c8c4282e112d62bec8201.png')"
/>
As I track this down further, it appears that somewhere in the map-reader Ant task, this filename gets changed to that cryptic string of pseudo-hexadecimal. I think later on it's supposed to be changed back or resolved to a complete URI or something.
So, the two-part question is: Why does Open Toolkit change my filenames, and what's supposed to change them back?
DITA-OT's preprocess uses hashes for temporary filenames because it allows the code to not deal with directory structures. This enables preprocess to work in so-called "map-first" mode, where it first processes all DITA map resources and only then starts to process DITA topic and image resources.
The preprocess has a step called clean-preprocess that can rewrite the temporary file names to match source resource files names. However, this rewrite operation is disabled for PDF output because the original file names are not used for anything in that output type.

How to embed base64 image in HTML using pytest-html?

I am using python-appium client and generating a HTML report after the tests are finished. I would like to add the embedded images of the failure tests in the HTML report. The reason to embed the image is that I can access it from the remote machine as well. Here is the code which I tried and doesn't work on another system but locally it works:
#pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call' or report.when == 'setup':
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
screenshot = driver.get_screenshot_as_base64()
extra.append(pytest_html.extras.image(screenshot, ''))
report.extra = extra
It seems to me that the encoded image is not generated properly as this is what I can see in the output HTML file:
<td class="extra" colspan="4">
<div class="image"><img src="assets/75870bcbdda50df90d4691fa21d5958b.png"/></div>
and I expect "src" to not to end with ".png" and it should be long string of characters. I have no idea how to resolve this.
Your code is correct. However, the standard behaviour of pytest-html is that even if you pass the image as base64 string, it will still store a file in the assets directory. If you want to embed the assets in the report file, you need to pass the --self-contained-html option:
$ pytest --html=report.html --self-contained-html
Or store the option in the pytest.ini:
# pytest.ini (or tox.ini or setup.cfg)
[pytest]
addopts = --self-contained-html
For the sake of completeness, here's the relevant spot in pytest-html readme:
Creating a self-contained report
In order to respect the Content Security Policy (CSP), several assets such as CSS and images are stored separately by default. You can alternatively create a self-contained report, which can be more convenient when sharing your results. This can be done in the following way:
$ pytest --html=report.html --self-contained-html
Images added as files or links are going to be linked as external resources, meaning that the standalone report HTML-file may not display these images as expected.
The plugin will issue a warning when adding files or links to the standalone report.

Matlab: labeling images stored in a single file based on image name

I was assigned a matlab assignment where I was given 25000 pictures of cats and dogs all stored in one folder. My question is how can I use the imagedatastore function on matlab to store these files into one single variable containing two labels (cats and dogs). Each image stored in the file follow the following format:
cat.1.png,
cat.2.png,
.....,
cat.N.png,
dog.1.png,
dog.2.png,
.....,
dog.N.png,
Ideally I think labeling them based on image name would probably be the best approach to this. How ever I've tired doing this using various implementations methods but I keep failing. Any advice on this would be greatly appreciated!
The steps for both image data stores are the same:
Find all the image files with a matching name with dir.
Rebuild the full path to these files with fullfile.
Create the image data store with the files.
My code assumes that you are running the script in the same folder in which images are located. Here is the code:
cats = dir('cat.*.png');
files_cats = fullfile({cats.folder}.', {cats.name}.');
imds_cats = imageDatastore(files_cats);
dogs = dir('dog.*.png');
files_dogs = fullfile({dogs.folder}.', {dogs.name}.');
imds_dogs = imageDatastore(files_dogs);
You could also use the short path:
imds_cats = imageDatastore('cat.*.png');
imds_dogs = imageDatastore('dog.*.png');
If you want to use a single image data store and split files into categories within it (without using folder names, since all your files seem to be located in the same directory):
cats = dir('cat.*.png');
cats_labs = repmat({'Cat'},numel(cats),1);
dogs = dir('dog.*.png');
dogs_labs = repmat({'Dog'},numel(dogs),1);
labs = [cats_labs; dogs_labs];
imds = imageDatastore({'cat.*.png' 'dog.*.png'},'Labels',labs);

Fiji error: "No images file selected" when using a macro that calls the plugin 'Average Images'

I would like to create a macro with Fiji, that then I would run by using matlab.
I would like to average 3 images by using the Plugins>Stacks>Average Images.
I used first Plugins>Macro>Record... to get an idea what to use. Here is the result:
run("Average Images", "add=D:\\Corinne\\diabetes_paper_meas\\2013_01_29\\Measurement.007_cb\\Measurement._cb_frame0_pvar-8bit_combined.tif add=D:\\Corinne\\diabetes_paper_meas\\2013_01_29\\Measurement.007_cb\\Measurement._cb_frame1_pvar-8bit_combined.tif add=D:\\Corinne\\diabetes_paper_meas\\2013_01_29\\Measurement.007_cb\\Measurement._cb_frame2_pvar-8bit_combined.tif");
saveAs("Tiff", "D:\\Corinne\\diabetes_paper_meas\\2013_01_29\\Measurement.007_cb\\test.tif");
However, even when I run this macro I got an error: No images file selected. However, in Fiji you do not need to open any images to run the plugin but you add the files through a window interface...
So of course, since just running the macro is not working I get the same error when I use the following macro:
file = getArgument;
if (file=="") exit ("No argument!");
setBatchMode(true);
file_vasc = file;
file_vasc_frame_0 = file;
file_vasc_frame_1 = replace(file, "\\_frame0_pvar-8bit_combined.tif", "_frame1_pvar-8bit_combined.tif");
file_vasc_frame_2 = replace(file, "\\_frame0_pvar-8bit_combined.tif", "_frame2_pvar-8bit_combined.tif");
run("Average Images", "add=file_vasc_frame_0 add=file_vasc_frame_1 add=file_vasc_frame_2");
file_vasc_out = replace(file, "frame0_pvar-8bit_combined.tif", "_vasc_averaged.tif");
saveAs("Tiff", file_vasc_out);
Somebody has an idea what should I do?
Thanks
The Average Images plugin is using dialogs in a non-standard way and therefore is not scriptable: when running the plugin in Fiji with the macro recorder active (Plugins>Macros>Record... and then Plugins>Stacks>Average Images), you will see Fiji complain as soon as you add a second file to the list:
Duplicate keyword:
Command: "Average images"
Keyword: "add"
Value: /path/to/your/file
Add an underscore to the corresponding label in the dialog to make the first word unique.
This clearly is a bug in the plugin (it makes multiple use of the same OpenDialog thereby recording duplicate keywords in the macro recorder). You can report it via Help>Report a bug, however I fear that this plugin is not actively maintained at the moment.
In any case, there is a simple alternate way to average images in ImageJ without requiring this plugin, using the Image>Stacks>Images to Stack and Image>Stacks>Z Project... commands:
setBatchMode(true);
open("/path/to/first-image.tif");
open("/path/to/second-image.tif");
run("Images to Stack", "name=stack title=[]");
run("Z Project...");
saveAs("Tiff", "/path/to/new-file-name.tif");