ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi everyone, I'm a bit noobish with scripting and I want to automate the hard and long work that is the mannually resize hundreds of image with GIMP. For those that are interested I do that because I want to resize a texture pack for minecraft.
There is many thing I don't really know how to do and for those I need a bit of help, so here are some of theme:
I need to pass sucesively on each image in the directory himself and in directory that are inside the main one. Some time the image are even in a third or fourth directory
I need to know the size of the image so I can resize it with the correcte ratio. I have thincked about using imagemagick's identify command, but then I don't know how to get the size out of the output. Maybe using awk or somthing.
Sometime the image sizes are not perfect ( 513x512 insted of 512x512), so it would be great to be able to round the size up
I aslo need to copy the output (after the image is resized) in another directory but I also need to keep the same hierarchy.
I dont't know if I'm clear so here a exemple: input_pack/item/mod/TC2/dirt_block.png need to be resized and copied in outpout_pack/item/mod/TC2/dirt_block.png
Thanks in advence for your help and if I have new probleme I'll probably come back and ask my question here
this is not the bash. You need a graphical tool to do this. Bash can only be used to collect filenames and start the process.
For example ufraw can resize the picture to a given size (ratio depends on the longer side) and also you can define an output folder. I recommend you to search/google for "batch resize images" or similar, do not try to implement it in awk.
this is not the bash. You need a graphical tool to do this. Bash can only be used to collect filenames and start the process.
For example ufraw can resize the picture to a given size (ratio depends on the longer side) and also you can define an output folder. I recommend you to search/google for "batch resize images" or similar, do not try to implement it in awk.
Sorry, but you are wrong, you can do this completely in bash.
I'm not gonna write it all for you tho, because it is tedious and you didn't provide enough info on what exactly needs to be done. However, here are some possibly useful commands or algorithms.
I don't need any graphical tool other then imagemagick and I just need to pars the image size out of the output of the identify command. Also, I don't want to use batch I want to write a bash script
I don't want to implement anythingin awk, I just want to use it to pars an output.
What do you mean by this is not the bash ?
EDIT:
Thanks TeXMeX this is exactly what I needed. Could you explain the "awk '{ print $3}'" ?
#! /bin/sh
INPUT_DIR=/home/xeyve/DATA/Document/Minecraft/texture\ pack/Sphax
OUTPOUT_DIR=/home/xeyve/DATA/Document/Minecraft/texture\pack/Sphax\ 32
INPUT_SIZE=128
OUTPOUT_SIZE= 32
RATIO= $($INPUT_SIZE/$OUTPOUT_SIZE)
cd "$INPUT_DIR"
for file in $(find |grep .png)
do
SIZE=$(identify $file |awk '{ print $3}')
# problem here, I can't divide the string "2048x2048"(exemple) by 4. I need to convert it to a number, divide it then convert it back to a string.
RESIZE= $($SIZE/$RATON)
convert $file --resize $RESIZE
done
the awk's output is a string in the format of "128x128" or "1024x1024". I need to devide those number by the ration ( 4 this time ) and then pass the new string to the convert command. The new string should look like "32x32" or "256x256" for the to exemple I have given.
I just wanted to say: this is not the optimal solution, bash is not planned to do such things, and again, bash itself is unable to do that (or really hard to implement it).
So you need to use some additional tools, like awk, perl, cut, identify or whatever you know or you find useful. From my point of view (and you do not need to agree) it can be solved easily by an image manipulation tool which has batch processing capabilities. I have a lot of pictures (those are made with a nikon) and I wanted to resize them (and I also modified other things, like brightness). ImageMagick's convert is a good command line tool for this, but maybe not suitable for you.
the awk's output is a string in the format of "128x128" or "1024x1024". I need to devide those number by the ration ( 4 this time ) and then pass the new string to the convert command. The new string should look like "32x32" or "256x256" for the to exemple I have given.
well maybe It's not optimal but I also do it for the fun of learning, so it doesn't rellay matter.
I have solved the string to number conversion but now I am unable to convert it back to a string.
let "RATIO = $INPUT_SIZE / $OUTPOUT_SIZE"
#echo $RATIO
for file in $(find "$INPUT_DIR"|grep .png)
do
SIZE=$(identify $file |awk '{print $3}' |cut -d 'x' -f 2 )
let "RESIZE = $SIZE / $RATIO"
#STRING="$RESIZEx$RESIZE"
#this line doesn't work and I don't know any other way to do it
echo $STRING
#STRING="$RESIZEx$RESIZE"
#this line doesn't work and I don't know any other way to do it
This is because the x is a valid character in a variable name, so that bash tries to reference the value of a variable named "RESIZEx". Use braces to embed the variable name (this is the correct bash syntax):
Code:
STRING="${RESIZE}x${RESIZE}
Moreover you don't need to use cut to manage the output from awk:
@colucix: Wouldn't it be better to make sure the result is an integer? Maybe printf("%.0fx%.0f\n", a[1]/ratio, a[2]/ratio) ? The %.0f pattern will print a correctly rounded integer.
You can use variable expansion to extract the x and y values.
size="2048x1024"
xsize=${size%x*}; ysize=${size#*x}
Replace the 'x' with [xX:] to cover the 3 forms you might see, 1024x512, 1024X512, 1024:512. You could define functions to hide the details of extracting the dimensions, making the rest of the script easier to read and write.
I don't have time now to really read this thread, but you might want to look through this list of preexisting imagemagick scripts. There may be one that does exactly what you want.
PS: Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
Last edited by David the H.; 04-16-2012 at 12:57 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.