python-fu select copy paste - gimp

I'm a newbie in python-fu, (my second day), so my question may seem naive: I'd like to select a rectangular portion from "r400r.png", rotate it 90 degrees, and save my selection in "r400ra.png".
So far, I tried something on these lines:
for fv in range(400,401):
fn='r%sr.png' % fv
img=pdb.gimp_file_load('/path/'+fn,fn)
drw=pdb.gimp_image_get_active_layer(img)
img1=pdb.gimp_image_new(1024,1568,0)
lyr=pdb.gimp_layer_new(img1,1024,1568,0,'ly1',0,0)
pdb.gimp_rect_select(img,10,200,1422,1024,2,0,0)
drw=pdb.gimp_rotate(drw,0,1.570796327)
pdb.script_fu_selection_to_image(img1,drw)
f0=fn[:5]+'a'+fn[5:]
pdb.gimp_file_save(drw,'/path/'+f0,f0)
The "lyr" layer is there because my understanding is that it is a must, although it's not clear to me why. The "for" loop eventually should bulk process a bunch of files; for testing it is restricted to one file only. I get an error where I try o execute "script_fu_selection_to_image".
Can you point me, please, in the right direction?
Thanks,
SxN

The PDB calls to do that are better in this order:
# import your image:
img=pdb.gimp_file_load('/path/'+fn,fn)
#make the selection
pdb.gimp_rect_select(img,10,200,1422,1024,2,0,0)
# copy
pdb.gimp_edit_copy(img.layers[0])
# (no need to "get_active_layer" - if
# your image is a flat PNG or JPG, it only has one layer,
# which is accessible as img.layers[0])
# create a new image from the copied area:
new_img = pdb.gimp_paste_as_new()
#rotate the newly created image:
pdb.gimp_image_rotate(new_img, ...)
#export the resulting image:
pdb.gimp_file_save(new_img, ...)
#delete the loaded image and the created image:
# (as the objects being destroyed on the Python side
# do not erase then from the GIMP app, where they
# stay consuming memory)
pdb.gimp_image_delete(new_img)
pdb.gimp_image_delete(img)

Related

Timestamps of files in image created with rules_docker's container_image rule are all set to zero

I'm building an docker image with a call like this:
load(
"#io_bazel_rules_docker//container:container.bzl",
"container_image"
)
container_image(
name = "image",
base = "#nginx//image",
files = ["nginx.conf"],
symlinks = {
"/etc/nginx/nginx.conf": "/nginx.conf",
},
tars = [":build_tar"],
tags = ["catalog"],
)
As you can see, the image is supposed to contain the content of the tar archive available at target :build_tar, as well as the nginx.conf file.
The problem is that all the timestamps of the contents of produced image (both files taken from the tar archive as well as nginx.conf file) are set to zero (i.e. midnight Jan 1st 1970).
It looks like the container_image rule is not preserving timestamps. How can I fix this?
Adding enable_mtime_preservation = True param to container_image call fixes the timestamps.
A way to fix it would be to take the image you want to base your container off of, deploy it, then copy the files into it while it is running, and finally repack it for deployment.
I understand that is not ideal but from the searching I've done, that's how everyone recommends doing it.
I am just going to add that I do not completely understand what the image is for so I may be completely wrong on how to fix it.

How to list only top level directories in azure storage using ruby?

I am using below library to get all blobs in azure storage
https://github.com/Azure/azure-storage-ruby
First, I want to get only top level directories in a container
blob_client = Azure::Storage::Blob::BlobService.create(
storage_account_name: account_name,
storage_access_key: account_key
)
I can get all blobs using below code
blob_client.list_blobs(container_name)
But that's not what I want.
I want to loop all blobs under the loop of top level directories
top_level_directories = blob_client.list_top_level_directories(container_name)
top_level_directories.each do |directory|
blobs = directory.list_blobs
...
end
Above code is showing the strategy I want to loop. But that's not real code :)
Thanks

Can I create a script for GIMP to carry out a number of processes?

I want to process images before I send them to Tesseract for OCR.
For example:
Resize the image
Change the resolution to 300 dpi
Threshold (B&W image)
Sharpen image
How can I automate this process?
I've just put together an answer (https://graphicdesign.stackexchange.com/questions/53919/editing-several-hundred-images-gimp/53965#53965 ) on graphicdesign, which is intended as an GIMP automation primer for people with no programing skills -
it should be nice for understanding Python-fu as well.
On the very same answer, there are links to the official documentation, and one example of how to create a small script. You should them brose GIMP's PDB to findout about the exact proceeds you want.
But, all in all, you can create a Python file like this:
from gimpfu import *
import glob
def auto():
for filename in glob(source_folder + "/*.png"):
img = pdb.gimp_file_load(source_folder + filename, source_folder + filename)
# place the PDB calls to draw on the image before your interation here
#disp = pdb.gimp_display_new(img)
pdb.gimp_image_merge_visible_layers(img, CLIP_TO_IMAGE)
pdb.gimp_file_save(img, img.layers[0], dest_folder + filename, dest_folder + filename)
# pdb.gimp_display_delete(disp)
pdb.gimp_image_delete(img) # drops the image from gimp memory
register("batch_process_for_blah",
"<short dexcription >Batch Process for Bla",
"<Extended description text>",
"author name",
"license text",
"copyright note",
"menu label for plug-in",
"", # image types for which the plug-in apply - "*" for all, blank for plug-in that opens image itself
[(PF_DIRNAME, "source_folder", "Source Folder", None),
(PF_DIRNAME, "dest_folder", "Dest Folder", None)], # input parameters -
[], # output parameters
menu="<Image>/File", # location of the entry on the menus
)
main()
To find the wanted operations inside the for loop, go to Help->Procedure Browser - or better yet, Filters->Python->Console and hit Browse - it is almost the same, but with an "apply" button that makes it easy to test the call, and copy it over to your plug-in code.

Saving image in plot window after placing points in plot

Using Octave, I am able to show a image and then plot some red circles over it, as follow:
tux = imread('tux.png');
imshow(tux);
hold on;
plot(100,100,'r','markersize', 10);
plot(150,200,'r','markersize', 10);
The above code display this window:
My question is: How can I save this image as it is being showed inside the window?
Thank you very much!
Pretty simple. Use:
print -djpg image.jpg
print is a command in Octave that allows you to capture what's currently seen in the current figure window. -d specifies what output device you want to write to. There are multiple "devices" you can use to save to file... EPS, PS, TEX, etc. A device can also be an image writer, and so here I chose JPEG. You can choose other valid image formats that are supported by Octave. Take a look at the link I provided above for more details.
After, you just specify what file name you want to save the plot to. In this case, I chose image.jpg.
You can also take a look at saveas. Make sure you get a handle to the current figure first before doing so:
h = gcf;
saveas(h, "image.jpg");
Also... a more point-and-click approach would be to Go to File -> Save As in the figure that your image is displayed in :)
You can use print to save your plot to a file:
print (FILENAME, OPTIONS) // for the current figure
print (H, FILENAME, OPTIONS) // for the figure handle H
and also take a look to saveas
saveas (H, FILENAME)

RMagick, Tempfile, Paperclip: how to save a image file with large dimensions and small kbs as a thumbnail?

I have a Rails rake task that is processing a batch of images. It strips out the white background (using RMagick), replaces it with a transparent layer, writes it to a tempfile and then saves it as a PNG on Amazon S3 (using Paperclip).
It works for the bulk of the images. However, it runs into an error for at least 1 image. Can someone help me figure out why and how to fix it?
Code sample:
require 'RMagick'
require 'tempfile'
include Magick
task :task_name => :environment do
x = Item.find(128) # image 128 is the one giving me trouble
sourceImage = Image.read(x.image_link_hires)
processedImage = sourceImage[0].transparent("white")
tempImageFile = Tempfile.new(["processed_image",".png"])
processedImage.write("png:" + tempImageFile.path)
x.image_transparent = tempImageFile
x.save!
end
The error message:
rake aborted! Validation failed: Image transparent C:/Users/Roger/AppData/Local/Temp/processed_image20130107-8640-1ck71i820130107-8640-i6p91w.png is not recognized by the 'identify' command., Image transparent C:/Users/Roger/AppData/Local/Temp/processed_
image20130107-8640-1ck71i820130107-8640-i6p91w.png is not recognized by the 'identify' command.
This message appears upon running the last line (the save operation).
Tempfile problem with small files?
I think the error has something to do with Tempfile not actually writing a file to the temp path. This error may have to do with small filesize? The specific image that it's having trouble with has an usually amount of white space, so the resulting filesize after processing is about 30k for an 800x800 pixel image.
How can I verify if this is the case? And if it is, how can I work around it?
Other observations:
When I write the trouble image to a normal file (rather than Tempfile), it saves successfully locally.
The task works fine for other images, which tend to be much bigger (~1-2MB)
After processedImage.write, I've checked tempImageFile.size. It says that it's 30kb as expected.
When I observe the temp file directory when the rake task runs, I can see the temp files being created when the task is run other images successfully. The files seem to show up when processedImage.write runs. However, for the trouble image, I don't see temp files ever being created.
Thanks for any advice.
Update 7 Jan 2013
I've investigated this more. I reran #1 above, but attempted to save onto S3 with Paperclip. This generated the same error message.
So now I believe the issue is that this is a small file in terms of bytes (32kb), but with a decent height and width (800x800). Paperclip is trying to save a thumbnail version of it, which is 90x90. Typically this generates a filesize that is <1% the original, which I assume is the source of the errors.
If anyone has an elegant workaround / fix for this, I'd appreciate hearing about it.

Resources