change resolution, resize and frame
change resolution, resize and frame
Hi everyone!
My goal is to prepare a file image file to be printed, in the maximum possible resolution, with a frame.
So, I have an image with W pixels vs H pixels (Wp x Hp), with a resolution of D dots/in. The paper size is W inches x H inches (Wi x Hi), and I would like a frame with M inches (Mi) of white margin.
I thought I had the math right, and the problem would be using IM to get what I want, but after so many tries, I am not sure of the math anymore, and IM is sitll an issue.
I tried over and over to write a Linux shell script, but with no success, so I would appreciate a lot if someone could help me.
Thanks a lot!
My goal is to prepare a file image file to be printed, in the maximum possible resolution, with a frame.
So, I have an image with W pixels vs H pixels (Wp x Hp), with a resolution of D dots/in. The paper size is W inches x H inches (Wi x Hi), and I would like a frame with M inches (Mi) of white margin.
I thought I had the math right, and the problem would be using IM to get what I want, but after so many tries, I am not sure of the math anymore, and IM is sitll an issue.
I tried over and over to write a Linux shell script, but with no success, so I would appreciate a lot if someone could help me.
Thanks a lot!
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: change resolution, resize and frame
Post your image to dropbox.com and put the URL here.
Do you want to replace Mi inches border around the image by white to have Wi x Hi print output or do you want to reduce the image size in pixels to add Mi inches to the border to have Wi x Hi print output?
Do you want to replace Mi inches border around the image by white to have Wi x Hi print output or do you want to reduce the image size in pixels to add Mi inches to the border to have Wi x Hi print output?
Re: change resolution, resize and frame
Hi fmw42, and thanks for your answer!
I would like to reduce the image size in pixels to add Mi inches to the border to have Wi x Hi print output.
The file is not at dropbox, but you can it here https://copy.com/Q3hGUHy2wSbMYyb7.
Thanks!!
I would like to reduce the image size in pixels to add Mi inches to the border to have Wi x Hi print output.
The file is not at dropbox, but you can it here https://copy.com/Q3hGUHy2wSbMYyb7.
Thanks!!
Re: change resolution, resize and frame
Fred,
I came up with this script, which does not create the margin yet:
#! /bin/sh
# 30.5 cm page size (12 inches), less 2 centimeters
final_size_in=`echo "(112205 / 10000)" | bc -l`
echo "$final_size_in"
# TODO: pass page and margin size as parameters
if [ $# -eq 0 ]; then
echo "Sintax: `basename $0` list-arqs"
exit 10
fi
for full_file_name in "$@"; do
echo -n "$full_file_name: "
if [ ! -f "$full_file_name" ]; then
echo " does not exist!";
else
# x and y resolution
x_res=`identify -format "%x" $full_file_name`
y_res=`identify -format "%y" $full_file_name`
if [ $x_res != $y_res ]; then
echo "Cant handle differente X and Y resolutions"
else
# width and height in pixels
w_px=`identify -format "%w" $full_file_name`
h_px=`identify -format "%h" $full_file_name`
# resolution candidate 1
r1=`echo "($w_px / $final_size_in)" | bc -l`
# resolution candidate 2
r2=`echo "($h_px / $final_size_in)" | bc -l`
# crossing resolutions
h_in_using_r1=`echo "( $h_px / $r1 )" | bc -l`
w_in_using_r2=`echo "($w_px / $r2)" | bc -l`
# which fits best?
r2_gt_r1=`echo "($w_in_using_r2 > $h_in_using_r1)" | bc`
if [ $r2_gt_r1 -eq 1 ]; then
r=$r2
else
r=$r1
fi
# converting
new_full_file_name="M_$full_file_name"
convert $full_file_name -density $r -quality 100 $new_full_file_name
# dit it work?
new_x_res=`identify -format "%x" $new_full_file_name`
new_y_res=`identify -format "%y" $new_full_file_name`
# report
echo "w_px = $w_px, h_px = $h_px, r1 = $r1, r2 = $r2, h_in_using_r1 = $h_in_using_r1, w_in_using_r2 = $w_in_using_r2, r = $r, new_x_res = $new_x_res, new_y_res = $new_y_res"
fi
fi
done
I tested in 4 files, and, I dont know why, it did not work in 1 of them. IM did not report any error.
Again, thanks for your help
I came up with this script, which does not create the margin yet:
#! /bin/sh
# 30.5 cm page size (12 inches), less 2 centimeters
final_size_in=`echo "(112205 / 10000)" | bc -l`
echo "$final_size_in"
# TODO: pass page and margin size as parameters
if [ $# -eq 0 ]; then
echo "Sintax: `basename $0` list-arqs"
exit 10
fi
for full_file_name in "$@"; do
echo -n "$full_file_name: "
if [ ! -f "$full_file_name" ]; then
echo " does not exist!";
else
# x and y resolution
x_res=`identify -format "%x" $full_file_name`
y_res=`identify -format "%y" $full_file_name`
if [ $x_res != $y_res ]; then
echo "Cant handle differente X and Y resolutions"
else
# width and height in pixels
w_px=`identify -format "%w" $full_file_name`
h_px=`identify -format "%h" $full_file_name`
# resolution candidate 1
r1=`echo "($w_px / $final_size_in)" | bc -l`
# resolution candidate 2
r2=`echo "($h_px / $final_size_in)" | bc -l`
# crossing resolutions
h_in_using_r1=`echo "( $h_px / $r1 )" | bc -l`
w_in_using_r2=`echo "($w_px / $r2)" | bc -l`
# which fits best?
r2_gt_r1=`echo "($w_in_using_r2 > $h_in_using_r1)" | bc`
if [ $r2_gt_r1 -eq 1 ]; then
r=$r2
else
r=$r1
fi
# converting
new_full_file_name="M_$full_file_name"
convert $full_file_name -density $r -quality 100 $new_full_file_name
# dit it work?
new_x_res=`identify -format "%x" $new_full_file_name`
new_y_res=`identify -format "%y" $new_full_file_name`
# report
echo "w_px = $w_px, h_px = $h_px, r1 = $r1, r2 = $r2, h_in_using_r1 = $h_in_using_r1, w_in_using_r2 = $w_in_using_r2, r = $r, new_x_res = $new_x_res, new_y_res = $new_y_res"
fi
fi
done
I tested in 4 files, and, I dont know why, it did not work in 1 of them. IM did not report any error.
Again, thanks for your help
Re: change resolution, resize and frame
I will create a larger frame. The total image should have 12" in its shortest size.
Re: change resolution, resize and frame
Odd... 'identify -format "%w-$h, %x-%y" reports for each file:
M_2013-07-18-0047.jpg: 2848-4272, 72-72
M_2013-10-15-0004.jpg: 3748-2843, 253-253
M_2015-07-11-0001.jpg: 4000-6000, 350-350
M_2015-09-15-0004.jpg: 4000-6000, 350-350
But Gimp, as well Phatch say theirs resolution is 72x72, except for M_2013-10-15-0004.jpg.
I noticed that the original file for M_2015-07-11-0001.jpg and M_2015-09-15-0004.jpg already had a resolution of 350x350 (I must have edited them manually), while M_2013-10-15-0004.jpg was originally 72x72. M_2013-07-18-0047.jpg was 72x72 and remained that way.
I'll try to understand what happened.
M_2013-07-18-0047.jpg: 2848-4272, 72-72
M_2013-10-15-0004.jpg: 3748-2843, 253-253
M_2015-07-11-0001.jpg: 4000-6000, 350-350
M_2015-09-15-0004.jpg: 4000-6000, 350-350
But Gimp, as well Phatch say theirs resolution is 72x72, except for M_2013-10-15-0004.jpg.
I noticed that the original file for M_2015-07-11-0001.jpg and M_2015-09-15-0004.jpg already had a resolution of 350x350 (I must have edited them manually), while M_2013-10-15-0004.jpg was originally 72x72. M_2013-07-18-0047.jpg was 72x72 and remained that way.
I'll try to understand what happened.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: change resolution, resize and frame
This seems to work for me on your image, though I have not printed it. Using IM 6.9.2.1 Q16 Mac OSX
Code: Select all
# assume the following three dimensions are in inches
size1=8.5
size2=11
frame=1
infile="2013-10-15-0004.jpg"
inname=`convert "$infile" -format "%t" info:`
suffix=`convert "$infile" -format "%e" info:`
ww=`identify -ping -format "%w" "$infile"`
hh=`identify -ping -format "%h" "$infile"`
#echo "ww=$ww; hh=$hh;"
# set WW and HH to match aspect of ww and hh
if [ $ww -le $hh ]; then
WW=$size1
HH=$size2
else
WW=$size2
HH=$size1
fi
#echo "WW=$WW; HH=$HH;"
# compute resolutions so wwxhh will fit to WWxHH
xres=`convert xc: -format "%[fx:$ww/$WW]" info:`
yres=`convert xc: -format "%[fx:$hh/$HH]" info:`
# compute desired image width and height to match sizes of WW*xres and HH*yres
wwd1=`convert xc: -format "%[fx:round($WW*$xres)]" info:`
hhd1=`convert xc: -format "%[fx:round($HH*$yres)]" info:`
# compute desired image width and height less white frame border
wwd2=`convert xc: -format "%[fx:round(($WW-$frame)*$xres)]" info:`
hhd2=`convert xc: -format "%[fx:round(($HH-$frame)*$yres)]" info:`
#echo "xres=$xres; yres=$yres; wwd1=$wwd1; hhd1=$hhd1; wwd2=$wwd2; hhd2=$hhd2"
convert "$infile" -resize ${wwd2}x${hhd2} \
-gravity center -background white -extent ${wwd1}x${hhd1} \
-units PixelsPerInch -density ${xres}x${yres} "${inname}_framed.$suffix"
Re: change resolution, resize and frame
Thanks!
I will try it and let you asap.
I will try it and let you asap.
Re: change resolution, resize and frame
Fred,
Thanks a lot!
I did not use your solution, as I dont have both page sizes (your 'size2' variable), but just a limit of 30.5 centimeters for one of the sides. The other can vary its length freely.
However, only studying your example was that I saw the need for '-units PixelsPerInch'!
Also thanks to your code, it'll be much easier to insert the frame (but I'll take a look at it later night).
Thanks!!
Thanks a lot!
I did not use your solution, as I dont have both page sizes (your 'size2' variable), but just a limit of 30.5 centimeters for one of the sides. The other can vary its length freely.
However, only studying your example was that I saw the need for '-units PixelsPerInch'!
Also thanks to your code, it'll be much easier to insert the frame (but I'll take a look at it later night).
Thanks!!
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: change resolution, resize and frame
Is the 30.5 cm for the shorter or longer side? If I know that, I can adjust the script.
Re: change resolution, resize and frame
It is for the shorter side.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: change resolution, resize and frame
OK. Try this
Code: Select all
# resize, pad with border and set densities
# assume the following size is in cm and frame is in inches
size=30.5
frame=1
infile="2013-10-15-0004.jpg"
inname=`convert "$infile" -format "%t" info:`
suffix=`convert "$infile" -format "%e" info:`
ww=`identify -ping -format "%w" "$infile"`
hh=`identify -ping -format "%h" "$infile"`
#convert size to inches
size=`convert xc: -format "%[fx:$size/2.54]" info:`
#echo "ww=$ww; hh=$hh; size=$size"
# set small dimension to match size
if [ $ww -le $hh ]; then
WW=$size
res=`convert xc: -format "%[fx:$ww/$WW]" info:`
HH=`convert xc: -format "%[fx:$hh/$res]" info:`
else
HH=$size
res=`convert xc: -format "%[fx:$hh/$HH]" info:`
WW=`convert xc: -format "%[fx:$ww/$res]" info:`
fi
#echo "WW=$WW; HH=$HH; res=$res;"
# compute desired image width and height to match WW*res and HH*res
wwd1=`convert xc: -format "%[fx:round($WW*$res)]" info:`
hhd1=`convert xc: -format "%[fx:round($HH*$res)]" info:`
# compute desired image width and height less white frame border
wwd2=`convert xc: -format "%[fx:round(($WW-$frame)*$res)]" info:`
hhd2=`convert xc: -format "%[fx:round(($HH-$frame)*$res)]" info:`
#echo "res=$res; wwd1=$wwd1; hhd1=$hhd1; wwd2=$wwd2; hhd2=$hhd2"
convert "$infile" -resize ${wwd2}x${hhd2} \
-gravity center -background white -extent ${wwd1}x${hhd1} \
-units PixelsPerInch -density ${xres}x${yres} "${inname}_framed2.$suffix"
Re: change resolution, resize and frame
Fred,
I didnt have time to run your code yesterday. I'll let you know as soon as I do it.
Thanks a lot!
I didnt have time to run your code yesterday. I'll let you know as soon as I do it.
Thanks a lot!