How to make an image resize with the window in tk - tk-toolkit

I have a GUI with a photo image in it. The window starts out being exactly the size of the native image. What I would like is for the image to resize itself to fit the window when the user resizes the window. Based on the documentation from "photo" command, I have tried the follwing. But it does not seem to work. The image just stays the same size always and resizing the window just hides part of it instead of resizing it. Am I not understanding the function of the -shrink option?
package require Tk
image create photo previewraw -file preview.pnm
image create photo preview -file preview.pnm
ttk::frame .c
ttk::label .c.preview -image preview
grid .c -column 0 -row 0 -sticky nwes
grid .c.preview -column 0 -row 0
grid columnconfigure . 0 -weight 1
grid rowconfigure . 0 -weight 1
grid columnconfigure .c 0 -weight 1
grid rowconfigure .c 0 -weight 1
bind .c.preview <Configure> {
preview copy previewraw -to 0 0 %w %h -shrink
}

and the anwer is:
package require Tk
image create photo preview -file preview.pnm
ttk::frame .c
ttk::label .c.preview -image preview
grid .c -column 0 -row 0 -sticky nwes
grid .c.preview -column 0 -row 0
grid columnconfigure . 0 -weight 1
grid rowconfigure . 0 -weight 1
grid columnconfigure .c 0 -weight 1
grid rowconfigure .c 0 -weight 1
bind .c.preview <Configure> {
exec convert preview.pnm -resize %wx%h -background lightgray -gravity center -extent %wx%h newfile
preview read newfile -shrink
}

Related

Merging Images Vertically in Multipe of 2

I have a situation where from a folder , I would like to merge 1 and 2 nd image vertically , 3rd and 4th Image vertically and so forth .
I have searched and found that Image magic does merging of images but not in any order . Is it possible to do it in Command Line .
Scenario: Files inside the folder 1.png , 2.png , 3.png upto say 60.png
Expected Output : Merge 1.png and 2.png vertically stacked one below the other Merge 3.png and 4.png vertically stacked one below the other . SO that I would be having 30 images finally .
I am using Ubuntu.
You can do it like this:
#!/bin/bash
out=0
for ((i=1;i<60;i+=2)); do
((j=i+1))
((out=out+1))
A=${i}.png
B=${j}.png
echo Stacking $A and $B to make result-${out}.png
magick "$A" "$B" -smush 10 "result-${out}.png"
done

ImageMagick Linux mimick ezgif.com result

Ezgif.com is a great page but can ImageMagick get the same results?
Ezgif settings:
Delay: 200 per image
Crossfade frames
Fader delay: 6
Frame Count: 10
My attempt via Linux Terminal:
convert -delay 200 -loop 0 *.jpg myimage.gif
As i told you on FL, you need to simply use
convert -resize (Smallest size) -delay 200 -morph 200 (source) (destination)
e.g:
convert -resize 200x200 -delay 200 -morph 200 /var/home/user1/pictures/*.jpg /var/home/user1/myresult.gif
on my side i can show you the result like this used on WINDOWS
in Windows Pictures folder
used this command:
C:\Users\Public\Pictures\Sample Pictures>convert -resize 20% -delay 20 -loop 0 *.jpg -morph 5 myimage.gif
Here is Unix shell script to create a faded animation of 4 images each with 10 intermediate fades. It loops over each successive pair of images and creates the faded intermediate images by blending the pair at different percentages.
Input:
(
imgArr=(lena.jpg mandril3.jpg zelda1.jpg peppers.jpg)
for ((i=0; i<4; i++)); do
img1=${imgArr[$i]}
j=$((i+1))
jj=$((j%4))
img2=${imgArr[$jj]}
for ((k=0; k<11; k++)); do
pct=$((10*k))
convert $img1 $img2 -define compose:args=$pct -compose blend -composite miff:-
done
done
) | convert -delay 20 - -loop 0 anim.gif
Animation:
Note that I had to shrink the image to 75% dimensions to make the file size small enough for upload here.
A complete one-liner solution with increased delay for main frames and last-to-first transition, with the same settings OP asked:
convert -loop 0 *.jpg -morph 10 -set delay '%[fx:(t%11!=0)?6:200]' -duplicate 1,-2-1 out.gif
-morph 10 inserts 10 intermediate frames between every original frame, so we need to increase the delay time for every 11th frame, so the fx part sets different delay time for them.
-duplicate 1,-2-1 does the transition for last to first frame

How to copy an image line and insert it at specified positions n-times with imagemagick?

I have some pixel based images that I would like to manipulate with imagemagick in the following way.
Each image follows the same pixel-line format:
aaaaaaaaaaaaaaa
bbbbbbbbbbbbbbb
bbbbbbbbbbbbbbb
ccccccccccccccc
ccccccccccccccc
bbbbbbbbbbbbbbb
bbbbbbbbbbbbbbb
aaaaaaaaaaaaaaa
So what I would like to do is to copy the first bbbb line and insert it after last b line before the c lines and after the last b lines after the c lines. This will expand the images height.
This process should be repeated n-times.
This means that the a and c lines part will be left untouched, while the b parts will blow up the height of the image.
After reading imagemagick option summary I have no clue how this could be done.
So any kind of help would be appreciated.
Thanks to #fmw42 I could script the following solution which gets the image size and then calculates the number of lines to increase the image to a height of 48/49 pixel.
First it adds the lines at the image bottom and after this it adds the same number of lines at the image top. While keeping the first/last lines.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set image=Welcome1.gif
rem use %1 for comanndline argument
set image2=%TEMP%\temp1.gif
set image3=%TEMP%\temp2.gif
magick identify -format "%%w" %image% > %TEMP%\imwidth.txt
set /P width=<%TEMP%\imwidth.txt
del %TEMP%\imwidth.txt
magick identify -format "%%h" %image% > %TEMP%\imheight.txt
set /P height=<%TEMP%\imheight.txt
del %TEMP%\imheight.txt
set /A addlines=(49 - %height%) / 2
set /A lastline=%height% - 2
magick %image% -crop %width%x1+0+2 %TEMP%\copyLine.gif
rem Add lines at end of image
set image1=%image%
FOR /L %%i IN (1,1,%addlines%) DO (
magick !image1! -background black -splice 0x1+0+%lastline% %image2%
magick composite %TEMP%\copyLine.gif %image2% -geometry 99x1+0+%lastline% %image3%
del %TEMP%\%image%
ren %image3% %image%
set image1=%TEMP%\%image%
)
rem Add lines at top of image
set image1=%TEMP%\%image%
FOR /L %%i IN (1,1,%addlines%) DO (
magick !image1! -background black -splice 0x1+0+2 %image2%
magick composite %TEMP%\copyLine.gif %image2% -geometry 99x1+0+2 %image3%
del %TEMP%\%image%
ren %image3% %image%
set image1=%TEMP%\%image%
)
copy %TEMP%\%image% %image:~0,-4%touch.gif
del %TEMP%\copyLine.gif %TEMP%\temp1.gif %TEMP%\temp2.gif %TEMP%\%image%
endlocal
exit /b 0

Why Imagemagick's morphology dilation algorithm differs from mathematical definition?

Origin Image
0 0 0
0 1 0
0 0 0
generated by:
$ convert -size 3x3 xc:black -fill white -draw 'point 1,1' origin.png
Dilation Process
Use a 2x1 rectangle as the kernel with central point (0,0):
processed by:
$ convert origin.png -morphology Dilate Rectangle:2x1+0+0 output.png
Expected Output
0 0 0
1 1 0
0 0 0
Actual Output
0 0 0
0 1 1
0 0 0
Question
Why the output is unexpected? I wonder how ImageMagick processes dilation.
Here is my understanding:
When the kernel's central point iterates to the position (0,1) of the original image:
I thought (0,1) should have been 1 after AND operations.
The "center point" of a 2x1 kernel is between pixels. So you have to choose which one is the official "origin". It is arbitrary. But you can set the origin in ImageMagick when defining the kernel. See https://imagemagick.org/Usage/morphology/#user
For example for a 2x1 kernel, it could be either
2x1+0+0: 0,1
or
2x1+1+0: 0,1

ImageMagick and transparent background for animated gif

I have an animation as a batch of .png files (100 files). The background is transparent in the source .png files. I want to convert them into a single animated gif. I have tried this command:
convert -delay 0 -loop 0 -alpha set *.png ani.gif
But the result is the following (green is the HTML page background):
How should I remove the previous frames from an every following one?
I've found -dispose previous.
UPDATE
OK, convert -delay 0 -loop 0 -alpha set -dispose previous *.png ani.gif
The solution is here:
http://www.alecjacobson.com/weblog/?p=2601
The magic keyword seems to be “dispose” and calling the following fixed the problem:
convert -dispose 2 screencapture-*.tga screencapture.gif

Resources