Gimp script-fu working only sometimes - gimp

I'm not really one of the graphic savvy people so I kinda hit a wall with this script found on the interwebz
(
define (script-fu-pcb-filter img)
(gimp-image-convert-grayscale img)
(gimp-image-flip img 1)
(gimp-image-resize img (* (cadr (cddr (gimp-selection-bounds 1))) 2) (* (caddr (cddr (gimp-selection-bounds 1))) 2) 0 0)
(gimp-layer-copy 2 1)
(gimp-image-add-layer img 3 0)
(gimp-layer-new img (cadr (cddr (gimp-selection-bounds 1))) (caddr (cddr (gimp-selection-bounds 1))) 2 "druhy" 100 0)
(gimp-image-add-layer img 4 0)
(gimp-edit-fill 4 2)
(gimp-image-lower-layer-to-bottom img 4)
(gimp-image-merge-down img 2 0)
(gimp-selection-all img)
(gimp-flip 5 0)
(gimp-floating-sel-anchor 6)
(gimp-selection-all img)
(gimp-flip 5 1)
(gimp-floating-sel-anchor 7)
(gimp-flip 3 1)
(gimp-image-merge-down img 3 0)
(gimp-convert-indexed img 0 3 0 FALSE FALSE "a")
(file-png-save 1 img 8 "/tmp/etch_eagle_export.png" "/tmp/etch_eagle_export.png" FALSE 9 FALSE FALSE FALSE TRUE FALSE)
)
(script-fu-register "script-fu-pcb-filter"
"PCB-Filter"
"Gets ready export image from egale ready for print"
"Anton Krug "
"Anton Krug"
"2009-04-25"
"RGB*, GRAY*, INDEXED*"
SF-IMAGE "Input Image" 0)
(script-fu-menu-register "script-fu-pcb-filter" "/Image/Eagle/")
It's supposed to flip an image and mirror it so that I get two copies of the same image on one page (A4 destination) and the image should retain it's original proportions i.e. no resize whatsoever. The image will then be printed on a transparency and used to etch a pcb.
I saved the script in the right folder and when I start gimp with a clean data folder (in my case /home/foo/.gimp-2.8) the script runs fine, once. I can't run the script a second time on another image after closing the first image or even after restarting gimp. I have to delete gimp's data folder and re-save it in the scripts folder for the script to run again. This kinda beats the purpose of scripts imho so I was wondering why it's now working as intended. Is there something I'm missing?
Edit: error message & addendum
addendum: the menu item PCB-Filter appears 2x when I refresh the scripts from a fresh profile folder. I'm running manjaro, a derivative of arch linux, and gimp -v gives out:
$ gimp -v
GNU Image Manipulation Program version 2.8.22
git-describe: GIMP_2_8_20-60-ge39a4e1203
using GEGL version 0.2.0 (compiled against version 0.2.0)
using GLib version 2.52.3 (compiled against version 2.52.2)
using GdkPixbuf version 2.36.8 (compiled against version 2.36.6)
using GTK+ version 2.24.31 (compiled against version 2.24.31)
using Pango version 1.40.9 (compiled against version 1.40.6)
using Fontconfig version 2.12.4 (compiled against version 2.12.3)
using Cairo version 1.14.10 (compiled against version 1.14.8)
Regards

Related

Gimp 'gimp-image-convert-indexed' produces huge files

I am writing a script (my first script-fu) to export .jpeg to .avif and .webp, and I want to optimize the files for the web. Somewhere I read the suggestion to change from RGB to an indexed color palette, so I implemented the procedure gimp-image-convert-indexed. I was expecting smaller, web optimized files, but the opposite happened. The output resulted in huge files:
Original jpeg: 7.510 KB
Exported files without gimp-image-convert-indexed:
.avif: 95 KB
.webp: 2.052 KB
Exported files with gimp-image-convert-indexed:
.avif: 6.337 KB
.webp: 19.393 KB
Did I sent a parameter wrong? Or is the whole idea of using 'gimp-image-convert-indexed' nonsense?
(Aside: any other obvious flaws in my beginner's script? Comments and hints welcome.)
convert.scm:
(define (filename-basename orig-name)
(car (strbreakup orig-name "."))
)
(define
(convert in_filename WebP Avif)
(let* (
(image (car (gimp-file-load RUN-NONINTERACTIVE in_filename in_filename)))
(drawable (car (gimp-image-flatten image)))
(outWebP (string-append (filename-basename in_filename) ".webP"))
(outAvif (string-append (filename-basename in_filename) ".avif"))
)
; small output files sizes without this line, huge with:
(gimp-image-convert-indexed image CONVERT-DITHER-FS CONVERT-PALETTE-WEB 0 0 1 "")
(cond ((equal? WebP 1)
(gimp-message "exporting as .webP")
(file-webp-save2 RUN-NONINTERACTIVE image drawable outWebP outWebP 0 0 90 100 0 0 0 0 0 0 0 0 0 0)))
(cond ((equal? Avif 1)
(gimp-message "exporting as .avif")
(file-heif-av1-save RUN-NONINTERACTIVE image drawable outAvif outAvif 50 0)))
(gimp-image-delete image)
)
)
Invoke under windows:
gimp-console-2.10 -idf -b "(convert \"IMG.jpg\" 1 1)" -b "(gimp-quit 0)"
Converting to indexed is nonsense since your output format does not support it. Color-indexing may reduce image size on PNG (which is the only format I know with both color-indexed and full-RGB formats).
I don't see much benefit on WebP/Avif over Jpeg, for the same final visual quality. In other words,there are JPEG options that work just as well: reducing quality or increasing chroma sub-sampling.
Also to batch-convert images, ImageMagick is much easier to code around than Gimp.

opencv_createsamples "Assertion Failed" Error

I am trying to create a vec file from my positive image samples. I'm using the latest version of Opencv. The command I am using is this:
opencv_createsamples -info Positive.info -num 3750 -w 24 -h 24 -vec Positive.vec
All my images are bigger than 24x24, so I don't believe that that is the issue. I've tried searching online, but I don't find anyone with this error while doing the createsamples command. The output that I get when I run that is this:
Info file name: Positive.info
Img file name: (NULL)
Vec file name: Positive.vec
BG file name: (NULL)
Num: 3750
BG color: 0
BG threshold: 80
Invert: FALSE
Max intensity deviation: 40
Max x angle: 1.1
Max y angle: 1.1
Max z angle: 0.5
Show samples: FALSE
Width: 24
Height: 24
Max Scale: -1
RNG Seed: 12345
Create training samples from images collection...
OpenCV: terminate handler is called! The last OpenCV error is:
OpenCV(4.0.0-alpha) Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat, file c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\matrix.cpp, line 465
This is my first time using opencv, so I'm very unclear as to what this error is. Anyone have any experience with this or can point me in the right direction to correcting the error?
This is Priyanka.
I was also getting the same error and I was trying to get resolution to it somehow.
But I could not find anything on internet.
But I am able to solve the problem successfully.
2-3 things that were missing as below:
I used opencv_annotation.exe to generate the -info (text file). This utility embeds complete image path into the file, where as the opencv_createsamples.exe expects that the images are present in the same folder as of the -info file. So what used to happen was the path of the image was getting concatenated with the path of the -info file.
i.e Suppose -info file is available at D:\MyPath\DataSet\annotations.txt and the images are at D:\MyPath\DataSet\Vehicles\*.png. In this case, while executing the opencv_createsamples.exe, the complete image path becomes D:\MyPath\DataSet\D:\MyPath\DataSet\Vehicles\image000.png, which the utility is not able to find.
So cater to the correct path kindly take care of below things
put the -info file in the same path as of images.
ensure that the -info file does not have the path for any image. It should contain only the
<image name without complete path> <object instances> <x y, width height>
With this change the image path was taken correctly and my issue got resolved.
Try doing this. It may help you.
This is Tushar Pathade, this error comes when your info.txt(file which contains information of objects to be detected) file contains some wrong information like image_name 1 0 0 0 0 or image_name 2 10 20 30 40. In first examples coordinates are (0,0,0,0) which is impossible while in second example no. of objects are 2 but coordinates of only one objects are mentioned.
So this lines produced this error. So first remove it and then run. You will not get any error then.
As priyanka mentioned above, this also need to take care of first but this will produced another error. It is nothing but image not found error. So to avoid it put info.txt file in a folder where negative images are. Basically u will see path in error so accordingly handle it.
Thank you.
I got the same error.
The error is about the bounding boxes, sometimes while creating bounding boxes you create a box outside the image pixel so that region is not get accepted and then you get this error.
Solution :
I used divide and conquer tech for this solution. What I did is I've created a new text file and in that text file I pasted some files from the original info or text file I ran the code if its creating samples then good if not then delete any one image from that text file run again*(run the create_samples again)*. do this until you get the clean file. I know this is too tedious task but this works.!
Thanks
I face the same issue now, if the num argument is less than 950 it works for me.

most cv2 tuple arguments don't work in python 3.5 (windows 7)

I switched a project over from python 2.7 to 3.5 and now I can't use most functions that require tuples.
As an example:
rgb = (255,0,0)
cv2.circle(img,(x, y),2,rgb,-1)
will return the "new style getargs format but argument is not a tuple" system error.
No matter how I enter the tuple expressing the color of the circle it will always fail, even if I explicitly use "tuple()"
I realize this problem isn't new but the solutions available are package dependent (https://mail.python.org/pipermail/python-dev/2017-January/147091.html).
I just want to put dots on an image without having to bring in another library when the same script is already using opencv.
EDIT: it's complaining about the x,y. The reason this worked in 2.7 and not 3.5 are unclear but specifically declaring the value as a tuple() fixes the issue
In Python 3.5, the following works:
import numpy as np
import cv2
img = np.zeros((100, 100)) # Black image
rgb = (255, 0, 0)
cv2.circle(img, (50, 50), 2, rgb, -1) # Plot centered on (50, 50)
Could you try this on your system and see if the error persist?
(Using Windows 10, Anaconda3, OpenCV 3.1.0)
If this works, your issue may be related to the type of your variable 'img' or the type of its values (see the link I posted as a comment of your question)

Why are these scripted gimp image modifications not being persisted?

This is my first foray into gimp scripting, and I seem to be having some basic misunderstanding. I've written the following code which is intended to convert white into transparency in the specified file, and to export the result as a png file:
(define (edit-name original-name)
(let* ((pos (- (string-length original-name) 4))
(suffix (substring original-name pos))
(prefix (substring original-name 0 pos))
(new-name (string-append prefix ".png")))
new-name))
(define (my-process filename)
(let* ((color '(0 0 0))
(image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
(drawable (car (gimp-image-get-active-layer image)))
(filename-modified (edit-name filename)))
(if (not (gimp-drawable-is-rgb drawable))
(gimp-image-convert-rgb image))
(plug-in-colortoalpha RUN-NONINTERACTIVE image drawable color)
(file-png-save2 RUN-NONINTERACTIVE image drawable filename-modified filename-modified 0 9 1 1 1 1 1 0 1)))
This script is saved in the file ~/.gimp-2.8/scripts/my-process.scm and is invoked with:
gimp -i -b '(my-process "somefile.xcf")' -b '(gimp-quit 0)'
The script appears to run and I get output on the console saying batch command executed successfully. Furthermore, if I put an obvious syntax or parameter error into the script then I get an error. I've also tried re-getting the drawable after the rgb conversion, in case the conversion results in a different object. I've also tried using file-png-save-defaults instead of file-png-save2, to no avail.
However, the output I get is a simple png export in indexed color mode (the original mode of the source image), which does not contain the alpha channel.
I've looked at the documentation for the conversion steps I'm using and cannot figure out what's going on; help would be much appreciated.
This works for me (this is your code in Python, using the same calls):
image=gimp.image_list()[0]
layer=image.active_layer
print "Type before:",pdb.gimp_drawable_type(layer)
pdb.gimp_image_convert_rgb(image)
print "Type after:",pdb.gimp_drawable_type(layer)
print "Has alpha after:",pdb.gimp_drawable_has_alpha(layer)
pdb.plug_in_colortoalpha(image,layer,(0,0,0))
pdb.file_png_save2(image,layer,'/tmp/foo.png','/tmp/foo.png',0,9,1,1,1,1,1,0,1)
And the output is (you can paste the above directly in the Python console):
>> image=gimp.image_list()[0]
>>> layer=image.active_layer
>>> print "Type before:",pdb.gimp_drawable_type(layer)
Type before: 4
>>> pdb.gimp_image_convert_rgb(image)
>>> print "Type after:",pdb.gimp_drawable_type(layer)
Type after: 0
>>> print "Has alpha after:",pdb.gimp_drawable_has_alpha(layer)
Has alpha after: 0
>>> pdb.plug_in_colortoalpha(image,layer,(0,0,0))
>>> pdb.file_png_save2(image,layer,'/tmp/foo.png','/tmp/foo.png',0,9,1,1,1,1,1,0,1)
What I find fishy is that there is no alpha channel after the conversion (type is RGB_IMAGE and not RGBA_IMAGE) (which is expected), but color-to-alpha works without adding one. You can however try to add one explicitly and see if this changes something.
This said, 95% of all batch processing in Gimp is better done with ImageMagick. For color-to-alpha, see this SO question

OpenCV createsamples - invalid background description file

I'm in my OPENCV_ROOT folder and running the following command in Win-x64:
opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1
maxzangle 0.5 -maxidev 40 -w 80 -h 40
-img ABSOLUTE_PATH_TO_POSITIVE_IMAGES_FOLDER\car.jpg
-bg ABSOLUTE_PATH_TO_PROJECT\negatives.txt
-vec ABSOLUTE_PATH_TO_SAMPLES_FOLDER\car.jpg.vec
-num 125
My negatives.txt looks like this:
negative_images\city01_02.jpg
negative_images\city01_04.jpg
negative_images\city01_05.jpg
negative_images\city01_06.jpg
negative_images\city01_07.jpg
This is my output:
Info file name: (NULL)
Img file name: ABSOLUTE_PATH_TO_POSITIVE_IMAGES_FOLDER\car.jpg
Vec file name: ABSOLUTE_PATH_TO_SAMPLES_FOLDER\car.jpg.vec
BG file name: ABSOLUTE_PATH_TO_PROJECT\negatives.txt
Num: 125
BG color: 0
BG threshold: 0
Invert: FALSE
Max intensity deviation: 40
Max x angle: 1.1
Max y angle: 1.1
Max z angle: 0.5
Show samples: FALSE
Width: 80
Height: 40
Create training samples from single image applying distortions...
Invalid background description file.
What's invalid about my negatives collection file (negatives.txt)? I tried listing the files within it both with absolute paths and with relative paths. I'm following this tutorial.
If you created the file on Windows, and running it on Linux e.g. Ubuntu, you have to change the "End of line sequence".
Click on the bottom of the editor on CR LF and change it to LF and try again!
Notepad++:
Visual Studio Code:
Geany: Documents > Set Line Endings > Convert and Set to LF (Unix)
Problem solved! The issue was simply that I had written the collection file names in Notepad++, and it all seemed fine. But when I happened to open the same file in notepad, I noticed there were no newlines after each image filename! When I added the newlines, and ran the command, the samples got created without errors!
I had the same problem. Just skip lines between addresses of the images. For example: before my negatives.dat file was:
./Negative_Images/197.
./Negative_Images/69.
./Negative_Images/510.
./Negative_Images/513.
./Negative_Images/169.png
Now it's:
./Negative_Images/197.png
./Negative_Images/69.png
./Negative_Images/510.png
./Negative_Images/513.png
./Negative_Images/169.png
Problem solved.
In my case, I was creating this bg.txt file with a script. Basically, it was doing :
for (int i = 0; i < numberOfImages; i++)
{
myDescFile << imagesFolder + std::to_string(i) + ".jpg\n";
}
I simply added another \n and the problem went away!
myDescFile << imagesFolder + std::to_string(i) + ".jpg\n\n";
EDIT:
You also have to make sure that your paths have/or\\ as separators. Else, you will be able to run the opencv_createsamples.exe cmd, won't have error, but will never see the Done at the end.
ex:L:/imgTraining/plant/bnw/0.jpg
Here's a picture of a good bg file from notepad++ (With View>Show Symbol>Show End of Line activated and Edit>EOL Conversion>Windows (CR LF) selected):
Install dos2unix on Linux by sudo apt-get install dos2unix.
And now you just need to use dos2unix YourFileName.txt.

Resources