Showing posts with label gnuplot binary. Show all posts
Showing posts with label gnuplot binary. Show all posts

Friday, September 30, 2011

Rotate a picture using gnuplot

Last time I promised rotating a picture using gnuplot will be talked. Today I come to realize my promise.Let us look at the script at first.
#Utility:Convert a rgb colorized image to a gray one
#Author:数声风笛离亭晚,我想潇湘君想秦!
#Email:qinjieli@gmail.com
#Usage:
##call "rotate.gnuplot" angle inputfile outputfile
#parameters:
##angle: the angle to be rotated
##inputfile: input filename (without ".png")
##outputfile: outputfilename (without ".png")
reset
angle=$0    #angle to be rotated
angle=angle*pi/180    #change degrees to radians
inputfile="$1.png"    #input file
outputfile="$2.png"    #ouput file
set size ratio -1
# Set the scales so that the unit has the same length
#on both the x and y axes
set lmargin 0    #nO margin
set rmargin 0
set tmargin 0
set bmargin 0
unset tics    #no tics and border line
unset border
#plot and get the size of the rotated picture
plot inputfile binary filetype=png rotate=angle w rgbima notitle
xmax=GPVAL_DATA_X_MAX
xmin=GPVAL_DATA_X_MIN
ymin=GPVAL_DATA_Y_MIN
ymax=GPVAL_DATA_Y_MAX
set xrange [xmin:xmax]    #set x and y range
set yrange [ymin:ymax]
set term png truecolor size (xmax-xmin),(ymax-ymin)    #set the terminal
set output outputfile
plot inputfile binary filetype=png rotate=angle w rgbima notitle
The script first plot the plot the rotated picture on screen. This plot has two functions, determining the size of the output picture and letting the user have view at the rotated picture. Save the script as "rotate.gnuplot", and then it can be called like this:
gnuplot> call "rotate.gnuplot" 30 input output
At last is one of our finished work.

Input picture file (initial file download from here)

Picture rotated by 30 degree using our script


Thursday, September 29, 2011

Crop picture using gnuplot

We talked about converting a rgb image to a gray one using gnuplot last time. Except this, Gnuplot also can do some other manipulations such as cropping, rotating and so on. In this article and next few ones I will talk about them respectively. This time we deal with cropping.

Cropping is selecting specific area from the picture, then drawing it to an output file. In gnuplot this can be done by setting the xrange and yrnage to a proper value and then plot. The script is shown below.
#Utility: crop png pictures using gnuplot.
#Author:数声风笛离亭晚,我想潇湘君想秦!
#Email:qinjieli@gmail.com
#Usage:
##call "crop.gnuplot" x1 y1 x2 y2 inputfile outputfile
#parameters:
##x1: x-value of left-bottom point
##y1: y-value of left-bottom point
##x2: x-value of right-top point
##y2: y-value of right-top point
##inputfile: input filename (without ".png")
##outputfile: output filename (without ".png")
reset
x1="$0"   #The left bottom point
y1="$1"
x2="$2"   #The right top point
y2="$3"
inputfile="$4.png"    #inputfile name
outputfile="$5.png"   #outputfile name
set xrange [x1:x2]
set yrange [y1:y2]
set size ratio -1
# Set the scales so that the unit has the same length
#on both the x and y axes
#There shold be no margin
set lmargin 0
set rmargin 0
set tmargin 0
set bmargin 0
#There shold be no key tics and border
unset key
unset tics
unset border
set term png truecolor size (x2-x1),(y2-y1)
set output outputfile
plot inputfile binary filetype=png w rgbimage
Save this script as "crop.gnuplot". Then it can be called from gnuplot like this:
gnuplot> call "crop.gnuplot" x1 y1 x2 y2 inputfile outputfile
In the end as usual let me show a finished work.

Input picture file (initial file download from here)

Picture cropped using command "call "plot.gplt" 250 150 500 400 input output"

Tuesday, September 27, 2011

Convert a rgb colorized png picture to a gray one using gnuplot

I will talk about convert a colorized rgb image to a gray one using gnuplot in this article.
Amazing it is to edit pictures with gnuplot. Yes, it is a little amazing, this kind of works should left to picture editors in fact. But I will show you gnuplot can also do it well.
In a rgb image, the color of a point is marked with (r,g,b) where r,g and b can be and always are diffrent values. While for a gray one r,g and b must be the same. So to convert a rgb image to a gray one, we should convert (r,g,b) to (gray,gray,gray). Once we known how to do it, the problem becomes easy. Now we come to write our gnuplot script.
#Utility:Convert a rgb colorized image to a gray one
#Author:数声风笛离亭晚,我想潇湘君想秦!
#Email:qinjieli@gmail.com
#Usage:
##call "rgbtogray.gnuplot" inputfile outputfile
#parameters:
##inputfile: input filename (without ".png")
##outputfile: outputfilename (without ".png")
reset
inputfile="$0.png"
outputfile="$1.png"
plot inputfile binary filetype=png w rgbimage
xmax=GPVAL_DATA_X_MAX
xmin=GPVAL_DATA_X_MIN
ymin=GPVAL_DATA_Y_MIN
ymax=GPVAL_DATA_Y_MAX
set xrange [xmin:xmax]
set yrange [ymin:ymax]
#get the size of the picture
gray(r,g,b)=0.299*r + 0.587*g + 0.114*b
#The function which convert rgb color to gray.
#At first I choose the three weighting all to be 0.333,
#but a firend told me it will be better to use these values
set size ratio -1
# Set the scales so that the unit has the same length
#on both the x and y axes
#########################
#There should not be any margin in the output picture
set lmargin 0
set rmargin 0
set tmargin 0
set bmargin 0
unset key   #The key, border and tics are all do not need
unset border
unset tics
#set terminal and output file name
set term png size (xmax-xmin),(ymax-ymin)
set output outputfile
#Plot with gray color
plot inputfile\
     u (gray(column(1),column(2),column(3))):\
       (gray(column(1),column(2),column(3))):\
       (gray(column(1),column(2),column(3)))\
     binary filetype=png w rgbimage
Save the script as "rgbtogray.gnuplot", then it can be called like this:
gnuplot> call "rgbtogray.gnuplot" inputfile outputfile
The following is two groups of our input and ouput files. The initial input files are copied from here and here (Thanks the original picture author!).
 

Input file-1

Output file-1

Input file-2

Output file-2
Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.