module ImageParadise
#¶ ↑
require ‘image_paradise/base/base.rb’ module ImageParadise
; class Foobar < Base
#¶ ↑
#¶ ↑
require ‘image_paradise/colours/colours.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/constants/constants.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/constants/image_file_types.rb’ ImageParadise::ARRAY_IMAGE_FILE_TYPES
#¶ ↑
#¶ ↑
require ‘image_paradise/gm_support.rb’
#¶ ↑
#¶ ↑
#¶ ↑
#¶ ↑
Original Author: David Stokar
This class perfoms the y coordinats conversion for the bar class. There are three cases: 1. Bars all go from zero in positive direction 2. Bars all go from zero to negative direction 3. Bars either go from zero to positive or from zero to negative
#¶ ↑
#¶ ↑
Gruff
. Graphs.¶ ↑
- Author
-
Geoffrey Grosenbach boss@topfunky.com
- Originally Created
-
October 23, 2005
Extra thanks to Tim Hunter for writing RMagick, and also contributions by Jarkko Laine, Mike Perham, Andreas Schwarz, Alun Eyre, Guillaume Theoret, David Stokar, Paul Rogers, Dave Woodward, Frank Oxener, Kevin Clark, Cies Breijs, Richard Cowin, and a cast of thousands.
See Gruff::Base#theme=
for setting themes.
#¶ ↑
#¶ ↑
A mixin for methods that need to be deleted or have been replaced by cleaner code.
Graph with dots and labels along a vertical access see: ‘Creating More Effective Graphs’ by Robbins
#¶ ↑
Makes a small bar graph suitable for display at 200px or even smaller.
#¶ ↑
#¶ ↑
Makes a small pie graph suitable for display at 200px or even smaller.
#¶ ↑
Makes a small pie graph suitable for display at 200px or even smaller.
Here’s how to make a Pie graph:
g = Gruff::Pie.new g.title = "Visual Pie Graph Test" g.data 'Fries', 20 g.data 'Hamburgers', 50 g.write("test/output/pie_keynote.png")
To control where the pie chart starts creating slices, use zero_degree.
Here’s how to set up an XY Scatter Chart
g = Gruff::Scatter.new(800)
g.data(:apples, [1,2,3,4], [4,3,2,1]) g.data(‘oranges’, [5,7,8], [4,1,7]) g.write(‘test/output/scatter.png’)
A scene is a non-linear graph that assembles layers together to tell a story. Layers are folders with appropriately named files (see below). You can group layers and control them together or just set their values individually.
Examples:
-
A city scene that changes with the time of day and the weather conditions.
-
A traffic map that shows red lines on streets that are crowded and green on free-flowing ones.
Usage:
g = Gruff::Scene.new("500x100", "path/to/city_scene_directory") # Define order of layers, back to front g.layers = %w(background haze sky clouds) # Define groups that will be controlled by the same input g.weather_group = %w(clouds) g.time_group = %w(background sky) # Set values for the layers or groups g.weather = "cloudy" g.time = Time.now g.haze = true # Write the final graph to disk g.write "hazy_daytime_city_scene.png"
There are several rules that will magically select a layer when possible.
-
Numbered files will be selected according to the closest value that is less than the input value.
-
‘true.png’ and ‘false.png’ will be used as booleans.
-
Other named files will be used if the input matches the filename (without the filetype extension).
-
If there is a file named ‘default.png’, it will be used unless other input values are set for the corresponding layer.
Graph with individual horizontal bars instead of vertical bars.
New gruff graph type added to enable sideways stacking bar charts (basically looks like a x/y flip of a standard stacking bar chart)
alun.eyre@googlemail.com
#¶ ↑
Experimental!!! See also the Net graph.
Submitted by Kevin Clark glu.ttono.us/
#¶ ↑
#¶ ↑
#¶ ↑
#¶ ↑
#¶ ↑
require ‘image_paradise/project/project.rb’ ImageParadise.project_base_directory?
#¶ ↑
#¶ ↑
require ‘image_paradise/requires/require_toplevel_methods.rb’ ImageParadise.require_toplevel_methods
#¶ ↑
#¶ ↑
The rotate command-line option of ImageMagick takes one argument, which is the number of degrees by which to rotate the image. A positive number is the number of degrees to the right, and a negative number is the number of degrees to the left.
For specific rotate-actions you can look at the following website:
https://imagemagick.org/script/command-line-options.php#rotate
The specific API would then be:
-rotate degrees{<}{>}
#¶ ↑
require ‘image_paradise/rotate/rotate.rb’ ImageParadise.rotate_left(these_images, n_degrees, background_colour, output_file_here) ImageParadise.rotate_right(these_images, n_degrees, background_colour, output_file_here)
#¶ ↑
#¶ ↑
require ‘image_paradise/svg/circle.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/svg/feature.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/svg/rectangle.rb’
#¶ ↑
#¶ ↑
Shared coded for the SVG component of the ImageParadise
project be stored here.
#¶ ↑
require ‘image_paradise/svg/svg.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/add_black_border_to_this_image.rb’
#¶ ↑
#¶ ↑
require ‘create_animated_gif.rb’
#¶ ↑
#¶ ↑
We will tap into the project crop next.
#¶ ↑
require ‘image_paradise/toplevel_methods/crop.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/e.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/esystem.rb’
#¶ ↑
#¶ ↑
Note that this also contains directory-related code, but the file was not yet renamed.
#¶ ↑
require ‘image_paradise/toplevel_methods/file_related_code.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/flip_image_left_right.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/font_related_methods.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/grayscale_this_image.rb’ ImageParadise.grayscale_this_image(i, ‘NEW_FILE_NAME_GOES_HERE.png’)
#¶ ↑
#¶ ↑
This file contains code that can be used to load ruby-gtk3-specific files.
#¶ ↑
require ‘image_paradise/toplevel_methods/gui.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/help.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/interlace.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/make_this_image_transparent.rb’
#¶ ↑
#¶ ↑
This menu parses the commandline.
#¶ ↑
require ‘image_paradise/toplevel_methods/menu.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/misc.rb’ ImageParadise.draw_as_specified_from_the_yaml_file ImageParadise.magic_card_border ImageParadise.rounded_corner(ARGV) ImageParadise.img2pdf(ARGV)
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/negate.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/png_to_svg.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/quality.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/resized.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/roebe.rb’ ImageParadise.is_on_roebe?
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/thumbnail.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/wallpaper.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/toplevel_methods/write_this_text.rb’
#¶ ↑
#¶ ↑
require ‘image_paradise/version/version.rb’
#¶ ↑
Constants
- ALPHABET
#¶ ↑
ALPHABET
¶ ↑#¶ ↑
- ARRAY_IMAGE_FILE_TYPES
#¶ ↑
ImageParadise::ARRAY_IMAGE_FILE_TYPES
¶ ↑#¶ ↑
- CONVERT
#¶ ↑
CONVERT
¶ ↑#¶ ↑
- DEFAULT_CROP_COMMAND_TO_USE
#¶ ↑
DEFAULT_CROP_COMMAND_TO_USE
¶ ↑#¶ ↑
- DEFAULT_FONT
#¶ ↑
DEFAULT_FONT
¶ ↑#¶ ↑
- DEFAULT_TEXT
#¶ ↑
DEFAULT_TEXT
¶ ↑This is the default text.
#¶ ↑
- DELAY_FOR_THE_CREATION_OF_ANIMATED_GIFS
#¶ ↑
DELAY_FOR_THE_CREATION_OF_ANIMATED_GIFS
¶ ↑#¶ ↑
- INPUT_IMAGES_FOR_THE_CREATION_OF_ANIMATED_GIFS
#¶ ↑
INPUT_IMAGES_FOR_THE_CREATION_OF_ANIMATED_GIFS
¶ ↑#¶ ↑
- LAST_UPDATE
#¶ ↑
LAST_UPDATE
¶ ↑#¶ ↑
- N
#¶ ↑
N
¶ ↑#¶ ↑
- PROJECT_BASE_DIRECTORY
#¶ ↑
ImageParadise::PROJECT_BASE_DIRECTORY
¶ ↑#¶ ↑
- VERSION
#¶ ↑
VERSION
¶ ↑#¶ ↑
Public Class Methods
#¶ ↑
ImageParadise.add_black_border_to_this_image¶ ↑
This method will add a (black) border around the given input-image.
Since as of February 2021 this does no longer have to be confined solely to black as colour; other colours can be used as well. See the third argument to this method.
The -bordercolor commandline can be used to specify the colour of the border at hand.
The -border 5x10 variant means to have a 5px border left-right, and a 10px border top-bottom, each. Note that a single value of 5 here means exactly the same as 5x5. USe the variant you prefer.
Invocation example:
ImageParadise.add_black_border_to_this_image('foobar.png',3)
#¶ ↑
# File lib/image_paradise/toplevel_methods/add_black_border_to_this_image.rb, line 34 def self.add_black_border_to_this_image( file_that_is_to_be_modified = 'foobar.png', n_pixels = '1', use_this_colour = 'black', # Provide the colour here. new_filename = nil ) n_pixels = n_pixels.to_s if file_that_is_to_be_modified.is_a? Array file_that_is_to_be_modified = file_that_is_to_be_modified.first end if File.exist? file_that_is_to_be_modified extname = File.extname(file_that_is_to_be_modified) # Also has a leading '.' character. unless new_filename new_filename = file_that_is_to_be_modified.sub( /#{File.extname(file_that_is_to_be_modified)}/,'' ) new_filename << '_'+use_this_colour.to_s+'_border_'+n_pixels+'px' new_filename << extname end _ = "convert #{file_that_is_to_be_modified} "\ "-density 300 "\ "-shave #{n_pixels}x#{n_pixels} "\ "-bordercolor '#{use_this_colour}' "+ # Specify the colour of the border. "-border #{n_pixels} "\ "#{new_filename}" esystem _ if File.exist? new_filename e "The file `#{new_filename}` has been created." end return new_filename # Return it here as well. else e 'No file exists at `'+file_that_is_to_be_modified+'`.' end end
#¶ ↑
ImageParadise.add_green_border_to_this_image¶ ↑
Usage example:
ImageParadise.add_green_border_to_this_image('TANAKA.png','12')
#¶ ↑
# File lib/image_paradise/toplevel_methods/add_black_border_to_this_image.rb, line 77 def self.add_green_border_to_this_image( file_that_is_to_be_modified = 'foobar.png', n_pixels = '1', new_filename = nil ) ::ImageParadise.add_border_to_this_image( file_that_is_to_be_modified, n_pixels, 'green', new_filename ) end
#¶ ↑
ImageParadise.add_this_comment_to_that_image¶ ↑
This method can be used to add a comment to an image.
The first argument is the comment that the user wants to add to a particular image.
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 113 def self.add_this_comment_to_that_image( comment, image = 'foobar.png', output_file = :default ) case output_file # ======================================================================= # # === :default # ======================================================================= # when :default output_file = "output_#{File.basename(image)}" end _ = 'convert -comment "'+comment.to_s+'" '+image+' '+ output_file esystem _ end
#¶ ↑
ImageParadise.add_this_text_to_the_image¶ ↑
This method can be used to add text to a given image.
Usage examples:
ImageParadise.add_this_text_to_the_image('fun inside','Susanne_03.jpg') ImageParadise.add_this_text_to_the_image('fun inside','Susanne_03.jpg', :lightgreen)
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 318 def self.add_this_text_to_the_image( this_text = 'Foobar and Hello World!', image = 'foobar.png', use_this_background_colour = :white, # :khaki output_image = 'output.png', append_or_prepend = :append ) _ = 'convert '+image.to_s+ ' -background '+use_this_background_colour.to_s+' ' _ << '-font Times-New-Roman -pointsize 25 ' _ << " label:'#{this_text}' ".dup _ << '+swap ' if append_or_prepend == :prepend _ << '-gravity center '\ '-append '+ # -append means "add to the bottom of the image. output_image esystem _ end
#¶ ↑
ImageParadise.black_hole¶ ↑
This method, alias aliased onto ImageParadise.implode, can transform an image starting from the center of said image, to look as if a black hole emerged. It will thus distort the image as if it is sucking the pixels into it.
The implode command takes only one argument, which is the factor by which to apply the effect. The size of the effect on the image is that factor argument.
Invocation example:
ImageParadise.implode("foobar.jpg", 2, 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 483 def self.black_hole( i = 'foobar.png', implode_by_n = 1, output_file = 'output.png' ) esystem "convert "\ "-implode #{implode_by_n} "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.blur¶ ↑
The blur command-line option of ImageMagick applies a Gaussian-based blurring operation to the given image at hand. It is also called “gaussian”.
The argument that has to be provided is the radius of the blur, with an optional standard deviation.
The default for this method is to specify just a radius, without a standard deviation. If you want to specify a standard deviation as well, use something like “12x2” rather than e. g. “12” for the radius_to_apply argument.
Invocation example:
ImageParadise.blur "Water.jpg"
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 408 def self.blur( i = 'foobar.png', radius_to_apply = 12, # 5px output_file = 'output.png' ) esystem "convert "\ "-blur #{radius_to_apply} "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.charcoal¶ ↑
Invocation example:
ImageParadise.charcoal "Water.jpg"
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 428 def self.charcoal( i = 'foobar.png', radius_to_apply = 4, # 5px output_file = 'output.png' ) esystem "convert "\ "-charcoal #{radius_to_apply} "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.chop_away_outer_pixels¶ ↑
This method will chop away outer pixels from a given image.
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 377 def self.chop_away_outer_pixels( i = 'foobar.png', chop_away_n_pixels = 5, # 5px output_file = 'output.png' ) esystem "convert -gravity NorthWest -chop "\ "#{chop_away_n_pixels}x#{chop_away_n_pixels} "\ "-trim #{i} "\ "#{output_file}" end
#¶ ↑
ImageParadise.circle_masked¶ ↑
This will “paint” a circle over the main image in use.
Usage example:
ImageParadise.circle_masked('SANDRA.jpg') ImageParadise.circle_masked('SANDRA.jpg', 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 222 def self.circle_masked( i = '*.jpg', output_file = 'output.png' ) _ = 'convert '+i.to_s+' -alpha set \( +clone -distort DePolar 0 '\ '-virtual-pixel HorizontalTile -background None -distort Polar '\ '0 \) -compose Dst_In -composite -trim +repage '+ output_file.to_s esystem _ end
#¶ ↑
ImageParadise.colour_fill_this_image¶ ↑
This method can be used to fill an image with a specific colour, such as blue or green. It’s like you overlay that colour over the given image at hand.
Invocation example:
ImageParadise.colour_fill_this_image("Water.jpg", :blue, '33%', 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 452 def self.colour_fill_this_image( i = 'foobar.png', colour_to_use = :steelblue, n_percent = '10%', output_file = 'output.png' ) esystem "convert "\ "-fill #{colour_to_use} "\ "-colorize #{n_percent.to_s.delete('%')} "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.contrast¶ ↑
Invocation example:
ImageParadise.contrast("foobar.jpg", '1', 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 663 def self.contrast( i = 'foobar.png', n_times = '1', output_file = 'output.png' ) plus_or_minus = '-' if n_times.is_a?(String) and n_times.start_with?('-') plus_or_minus = '+' end contrast_string = "#{plus_or_minus}contrast " * n_times.to_i esystem "convert "\ "#{contrast_string} "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.create_animated_gif¶ ↑
This method will create an animated gif.
The input should be either an Array of input_images, or a string which will be used to create the command.
A delay of 100 means 1 second delay.
#¶ ↑
# File lib/image_paradise/toplevel_methods/create_animated_gif.rb, line 29 def self.create_animated_gif( input_images = INPUT_IMAGES_FOR_THE_CREATION_OF_ANIMATED_GIFS, delay = DELAY_FOR_THE_CREATION_OF_ANIMATED_GIFS ) output_name = 'animated_gif.gif' _ = 'convert -delay 100 -loop 0 '+input_images+' '+output_name esystem _ e 'Finished creating `'+sfile(output_name)+'`.' if File.exist? output_name end
#¶ ↑
ImageParadise.create_file_width_and_height_of_images_in_the_fotogallery¶ ↑
This is a custom method for my home system. Other users won’t need this really.
#¶ ↑
# File lib/image_paradise/toplevel_methods/roebe.rb, line 26 def self.create_file_width_and_height_of_images_in_the_fotogallery require 'yaml' require 'image_paradise/toplevel_methods/misc.rb' hash = {} all_images = Dir['/home/x/data/images/fotos/**/**'].select {|entry| is_an_image?(entry) } all_images.each {|this_image_file| # ======================================================================= # # Show some output, as that will take a while to create the .yml file. # ======================================================================= # e 'Now working on '+this_image_file+' ...' width_height = ::ImageParadise.width_height_of_this_image?(this_image_file).to_s hash[File.absolute_path(this_image_file)] = width_height e ' Width and height is: '+width_height } # ======================================================================= # # The custom file for my home system is stored via the following method: # ======================================================================= # into = ::ImageParadise.file_width_and_height_of_images_in_the_fotogallery what = YAML.dump(hash) e "Now storing into #{into}." require 'save_file' ::SaveFile.write_what_into(what, into) end
#¶ ↑
ImageParadise.create_shape¶ ↑
This method can be used to create various pre-determined shapes, such as a gel.
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 1018 def self.create_shape( which_shape = :gel ) case which_shape # ========================================================================= # # === :gel # # This will create a gel-shape - a fat ellipse. # ========================================================================= # when :gel, :default _ = "convert -size 100x60 xc:none -fill red -draw 'circle 25,30 10,30' -draw 'circle 75,30 90,30' -draw 'rectangle 25,15 75,45' gel_shape.png".tr("\n", ' ').squeeze(' ') esystem _ end end
#¶ ↑
ImageParadise.crop¶ ↑
Use this toplevel-method in order to crop a local image.
The first argument to this method is simply the local image file that you wish to crop, such as “foobar.png”.
If the second argument to this method is omitted then we will use the default settings for the used crop-parameters, which should be sufficiently useful enough in order to yield a cropped image. Otherwise, if a second argument is supplied, then these will be used as the crop-parameters for the current invocation run.
The “-crop” operator of the ImageMagick suite also understands how to crop an image to just a percentage of its original size. Let’s look at the following example, which would half the size of the given input-image at hand:
convert foo.png -crop 50%x+0+0 CROPPED_foo.png
For a typical usage example for -crop, look at the following example:
convert -crop 1104x372+58+100 input.jpg output.jpg
This means that the top-left corner is set at 58 pixels in, and 100 pixels down. Then, a rectangle will be used, with a width of 1104 pixels and a height of 372 pixel. That is the one that will be shown in the resulting, newly created image file.
Specific usage examples for this API:
ImageParadise.crop('foo.png') ImageParadise.crop('foo.png','40x30+10+10') ImageParadise.crop('foo.png','40x30+10+10','bla.png')
#¶ ↑
The official documentation for crop-related actions in regards to ImageMagick can be found here:
https://www.imagemagick.org/script/command-line-options.php?#crop
#¶ ↑
# File lib/image_paradise/crop/crop.rb, line 412 def self.crop( main_file, # The input-file is a mandatory argument. crop_parameters_to_use = :default, new_filename = :infer ) _ = ImageParadise::Crop.new { :do_not_run_yet_and_be_verbose } _.set_input_file(main_file) _.set_crop_parameters(crop_parameters_to_use) _.set_output_file(new_filename) _.do_crop return _.output_file? end
#¶ ↑
ImageParadise.darken_the_borders¶ ↑
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 1041 def self.darken_the_borders( of_this_image = 'gel_shape.png' ) _ = "convert "+of_this_image+" \( +clone -alpha extract -blur 0x2 -shade 0x90 -normalize -blur 0x2 +level 60,100% -alpha On \) -compose Multiply -composite "\ "gel_border.png". tr("\n", ' ').squeeze(' ') esystem _ end
#¶ ↑
ImageParadise.default_colour=¶ ↑
This method can be used to set (assign) a default colour. Curiously enough, ImageMagick appears to default to black colour for text when writing something.
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 1133 def self.default_colour=(i = :default) # :steelblue is the default colour case i # ======================================================================= # # === :default # ======================================================================= # when :default i = :steelblue end @hash[:default_colour] = i end
#¶ ↑
ImageParadise.default_position=¶ ↑
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 1106 def self.default_position=(i = :default) case i # ======================================================================= # # === :default # # This value can be changed to other values. # ======================================================================= # when :default i = '35,125' end @hash[:default_position] = i end
#¶ ↑
ImageParadise.draw_as_specified_from_the_yaml_file¶ ↑
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 925 def self.draw_as_specified_from_the_yaml_file( target_file = # ::ImageParadise.project_base_directory?+'yaml/rectangle.yml', ::ImageParadise.project_base_directory?+'yaml/circle.yml', output_file = 'output.png' ) require 'yaml' if File.exist? target_file key = 'circle' dataset = YAML.load_file(target_file) if dataset[key].has_key? 'resolution' width, height = dataset[key]['resolution'].split('x') else e 'A key called `resolution` is mandatory.' exit end _ = ''.dup _ << 'convert' if dataset[key].has_key? 'total_size' _ << " -size #{dataset[key]['total_size']}" else _ << " -size #{500}x#{500}" end _ << ' -stroke '+dataset[key]['strokecolour'].to_s _ << ' -strokewidth '+dataset[key]['strokewidth'].to_s _ << ' -fill '+dataset[key]['fill_colour'] case key when 'circle' # The 250,250 part should be half the total size. _ << ' -draw "circle 250,250 '+width.to_s+','+width.to_s+'"' when 'rectangle' if dataset[key]['round_rectangle'] == true _ << ' -draw "roundRectangle 0,0 '+width.to_s+','+height.to_s+' 12,12"' else _ << ' -draw "rectangle 0,0 '+width.to_s+','+height.to_s+'"' end end _ << ' xc:white' _ << " #{output_file}" end esystem _ end
#¶ ↑
ImageParadise.drop_shadow_effect¶ ↑
Note that this will create a border that is currently invariant. In the long run this may be different, allowing us to pick a border width, but for now, this is the way it is.
Usage example:
ImageParadise.drop_shadow_effect 'RandomBeachGal.jpg'
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 980 def self.drop_shadow_effect( i = 'input_image.png', use_this_colour = :black, output_file = :default ) if i.is_a? Array i = i.join(' ').strip end i = i.to_s case output_file when :default output_file = File.absolute_path(File.basename("output_#{i}")) end _ = 'convert '+i+' '+ '\( -clone 0 -background '+use_this_colour.to_s+ ' -shadow 80x3+12+12 \) \( -clone 0 -background '+use_this_colour+ ' -shadow 80x3-12-12 \) -reverse -background none '\ '-layers merge +repage '+ output_file.to_s esystem _ return output_file end
#¶ ↑
ImageParadise.embed_the_filename_into_this_image¶ ↑
This method will automatically attach the filename onto a particular images. So if the filename is “foobar.jpg” then the method will attach the word “foobar” to the image.
The gravity-argument determines where the text will appear.
Usage examples:
ImageParadise.embed_the_filename_into_this_image('TANAKA.jpg') ImageParadise.embed_the_filename_into_this_image('/foo/bar/') ImageParadise.embed_the_filename_into_this_image('/Depot/jjj/NJOY/')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 1168 def self.embed_the_filename_into_this_image( this_image = 'foobar.jpg', hash_options = { font_size: 20, colour_to_use: :orangered # Or :white. } ) font_size_to_use = hash_options[:font_size] colour_to_use = hash_options[:colour_to_use] this_image = [this_image].flatten.compact if this_image and this_image.first and File.directory?(this_image.first) # ===================================================================== # # Working on a directory instead. # ===================================================================== # _ = "#{this_image.first}/".squeeze('/') array = Dir[_+'*.jpg']+ Dir[_+'*.png']+ Dir[_+'*.gif'] array.flatten! this_image = array end # ======================================================================= # # Batch process this next - treat it as an Array: # ======================================================================= # this_image.each {|this_image_specifically| output_filename = 'output_'+File.basename(this_image_specifically). delete_suffix( File.extname(this_image_specifically) ) extname = File.extname(this_image_specifically) filename_to_use = File.basename(this_image_specifically). delete_suffix( File.extname(this_image_specifically) ).tr('_', ' ') cmd_to_use = "convert #{this_image_specifically} "\ "-font Luxi-Mono "\ "-pointsize #{font_size_to_use} "\ "-draw \"gravity northwest fill #{colour_to_use} text 0,15 '#{filename_to_use}' \" "\ "#{output_filename}"+extname e esystem(cmd_to_use) e } end
#¶ ↑
ImageParadise.extract_text_from_this_image¶ ↑
This method will use tesseract to extract/return text from the given image at hand.
Invocation example:
ImageParadise.extract_text_from_this_image('foobar.png') ImageParadise.extract_text_from_this_image('greyscaled_foo.jpg') ImageParadise.extract_text_from_this_image 'greyscaled_image.png'
#¶ ↑
# File lib/image_paradise/toplevel_methods/extract_text_from_this_image.rb, line 24 def self.extract_text_from_this_image( i, use_this_oem_value = 1, # :default do_show_the_command = false ) i = i.join(' ').strip if i.is_a? Array case use_this_oem_value when :default use_this_oem_value = 1 end case do_show_the_command when :do_show_the_command do_show_the_command = true end use_this_command = "tesseract #{i} stdout -l eng --oem #{use_this_oem_value} --psm 3 2>&1" if do_show_the_command puts use_this_command end # ========================================================================= # # An alternative to `` may be Open3.capture3(), such as like this: # require 'open3' # text, _, _ = Open3.capture3(use_this_command) # puts text # ========================================================================= # result = `#{use_this_command}` return result.strip end
#¶ ↑
ImageParadise.flip_image_left_right¶ ↑
This will do a 180° flip action on the image at hand.
The new file location will be returned by this method.
Note that -density and -quality are probably needed for .jpg, as .jpg loses quality otherwise.
#¶ ↑
# File lib/image_paradise/toplevel_methods/flip_image_left_right.rb, line 21 def self.flip_image_left_right( this_image, options = {} ) if this_image.is_a? Array this_image.each {|entry| flip_image_left_right(entry, options) } else unless this_image.include? '/' this_image = File.absolute_path(this_image) end base_dir = File.dirname(this_image) # ===================================================================== # # Designate a default output-file to use. # ===================================================================== # name_of_the_output_file = base_dir+'/flipped_image_left_to_right_'+ File.basename(this_image) if options and !options.empty? # =================================================================== # # === :use_this_as_the_filepath # # Designate a file-path to use for the output file. # =================================================================== # if options.has_key? :use_this_as_the_filepath name_of_the_output_file = options[:use_this_as_the_filepath] end end _ = 'convert -density 300 -quality 100 '+this_image+' \( -flop \) +append '+ name_of_the_output_file esystem _ return name_of_the_output_file end end
#¶ ↑
ImageParadise.flip_this_image_up_to_bottom¶ ↑
Invocation example:
ImageParadise.flip_this_image_up_to_bottom("/Depot/NJOY/WaterGirl.png", 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 699 def self.flip_this_image_up_to_bottom( i = 'foobar.png', output_file = 'output.png' ) esystem "convert "\ "-flip "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.font_via_tile_pattern¶ ↑
#¶ ↑
# File lib/image_paradise/toplevel_methods/font_related_methods.rb, line 14 def self.font_via_tile_pattern( this_text = 'Anthony Quinn', this_font = 'Hack-Regular', # 'Hack' width_and_height = '500x120' ) store_here = 'output_file.jpg' esystem 'convert '\ '-size '+width_and_height.to_s+' '\ 'xc:lightblue '\ '-font '+this_font+' '\ '-pointsize 72 '\ '-tile pattern:checkerboard '\ '-annotate +28+68 "'+ this_text+ '" '+ store_here end
#¶ ↑
ImageParadise.gamma_correct_this_image¶ ↑
Note that red, green and blue specifiy the gamma correction for each colour channel in the image.
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 136 def self.gamma_correct_this_image( i, red = :default, green = :default, blue = :default ) case red # ======================================================================= # # === :default # ======================================================================= # when :default red = 0.7 end case green # ======================================================================= # # === :default # ======================================================================= # when :default green = red end case blue # ======================================================================= # # === :default # ======================================================================= # when :default blue = red end _ = 'convert -gamma '+red.to_s+ ','+green.to_s+','+ blue.to_s+' '+i+' '+ 'output_'+File.basename(i) esystem _ end
#¶ ↑
ImageParadise.grayscale_this_image¶ ↑
This is just for “support” of american english. The resulting image files will have a prepended string called ‘grayscaled_’ rather than ‘greyscaled_’.
#¶ ↑
# File lib/image_paradise/toplevel_methods/greyscale_this_image.rb, line 66 def self.grayscale_this_image( i, use_this_filename = :default_prepended_grayscaled ) ::ImageParadise.greyscale_this_image(i, use_this_filename) end
#¶ ↑
ImageParadise.greyscale_this_image¶ ↑
This method will greyscale an image file. Right now we depend on the gem mini_magick for this task.
This method may be useful when you wish to pre-process a coloured image, turn it into black-white only, before using OCR (“optical character recognition”) on it.
#¶ ↑
# File lib/image_paradise/toplevel_methods/greyscale_this_image.rb, line 22 def self.greyscale_this_image( i, use_this_filename = :default_prepended_greyscaled ) begin require 'mini_magick' i = [i].flatten.compact i.each {|this_file| this_file = this_file.dup if this_file.frozen? img = MiniMagick::Image.open(this_file) img.colorspace('Gray') # Turn it into grayscale here. # =================================================================== # # Next we must determine the correct filename. The method will # assume that if a Symbol is given, that Symbol will have a # special meaning. Based on that, the name will be determined # accordingly. # =================================================================== # case use_this_filename when :default_prepended_grayscaled use_this_filename = "grayscaled_#{this_file}" when :default_prepended_greyscaled use_this_filename = "greyscaled_#{this_file}" end store_here = use_this_filename e "Storing into the file `#{store_here}`." img.write(store_here) } rescue LoadError e 'This method requires the gem mini_magick.' e e 'You can install it via:' e e ' gem install mini_magick' e end end
#¶ ↑
ImageParadise.height_of_this_image?¶ ↑
Usage example:
ImageParadise.height_of_this_image?("/home/x/data/images/fotos/urlaub/10.10.2004_Trieste_Miramare_Teich.jpg")
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 1260 def self.height_of_this_image?(i) return `convert #{i} -print "%h" /dev/null`.to_i end
#¶ ↑
ImageParadise.horizontal_append¶ ↑
This will append images together, in a vertical manner.
Invocation example:
ImageParadise.horizontal_append(["foobar1.jpg", "foobar2.jpg"], 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 793 def self.horizontal_append( these_images = ARGV, output_file = 'output.png', use_this_as_border = nil ) cmd = "convert +append #{these_images.join(' ')}".dup cmd << " #{output_file}" if use_this_as_border cmd << "-border #{use_this_as_border}" end esystem cmd return output_file end
#¶ ↑
ImageParadise.img2pdf¶ ↑
This method depends on the external software (written in python) called img2pdf.
This method will return the output-file that is generated.
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 1222 def self.img2pdf( work_on_these_files = '*.jpg' ) if work_on_these_files.is_a?(String) and work_on_these_files.include?('*') work_on_these_files = Dir[work_on_these_files] # Assume input such as '*.jpg' end work_on_these_files_as_string = work_on_these_files.join(' ').strip # ======================================================================= # # Next we will determine the output-filename to use. This is important # because if we use this method in a batch-manner then we do not want # to overwrite the same output name - thus we need to make this a # little bit more dynamic in its behaviour. # ======================================================================= # output_file = 'output_aggregated_document_'+ 'taken_from_'+ work_on_these_files_as_string.delete_suffix('.jpg')+ '.pdf' cmd = 'img2pdf -o '+output_file+' '+ work_on_these_files_as_string esystem(cmd) return output_file end
#¶ ↑
ImageParadise.increment_this_number_by_one¶ ↑
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 73 def self.increment_this_number_by_one(basename) basename = basename.dup scanned = basename.scan(/\d+$/).flatten.first.to_s # ======================================================================= # # scanned may now be '019'. # ======================================================================= # new_number = scanned.to_i + 1 # ======================================================================= # # We must also look to expand to its original size again. # ======================================================================= # new_number = new_number.to_s.rjust(scanned.size, '0') basename.sub!(/\d+$/, new_number) basename end
#¶ ↑
ImageParadise.interlace¶ ↑
The term “interlace” refers to an image that can be progressively displayed as they are downloaded from a remote location.
It was used in the past quite a bit for low-bandwidth connections, to get an indicator as to how the image may look like, before it has been fully download.
#¶ ↑
# File lib/image_paradise/toplevel_methods/interlace.rb, line 21 def self.interlace( i, line_or_plane = 'line' ) if i.is_a? Array i = i.join(' ').strip end output_file = 'interlaced_'+File.basename(i) _ = 'convert -interlace '+line_or_plane+' '+i+' '+output_file esystem _ end
#¶ ↑
ImageParadise.magic_card_border¶ ↑
This method will try to achieve a card border style similar to the card game “magic the gathering”.
Usage example:
ImageParadise.magic_card_border 'foobar.jpg'
#¶ ↑
# File lib/image_paradise/utility_scripts/magic_card_border.rb, line 203 def self.magic_card_border( i = ARGV, output_file = :default ) _ = ::ImageParadise::MagicCardBorder.new(i) {{ output_file: output_file }} return _.output_file? end
#¶ ↑
ImageParadise.make_this_image_transparent¶ ↑
This method depends on the program called ImageMagick.
#¶ ↑
# File lib/image_paradise/toplevel_methods/make_this_image_transparent.rb, line 16 def self.make_this_image_transparent( i, output_filename = :default ) i = [i].flatten.compact array_file_paths = [] i.each {|this_image| base_dir = File.dirname(File.absolute_path(this_image)) case output_filename when :default output_filename = base_dir+"/output_#{File.basename(this_image)}" end _ = 'convert '+this_image+ ' -transparent white ' _ << output_filename esystem _ array_file_paths << output_filename } return array_file_paths end
#¶ ↑
ImageParadise.merge_these_images¶ ↑
This method can be used to merge several input-image files together.
This functionality depends on the project called ImageMagick.
#¶ ↑
# File lib/image_paradise/toplevel_methods/merge_these_images.rb, line 19 def self.merge_these_images( i, use_this_as_output_name = 'MERGED_IMAGES' ) i = [i].flatten.compact _ = 'convert '.dup i.each {|this_image| _ << "#{this_image} " } unless use_this_as_output_name.include? '.' # ===================================================================== # # In this case we assume that it has no proper end, such as # .png or .jpg and so forth. # ===================================================================== # if use_this_as_output_name.frozen? use_this_as_output_name = use_this_as_output_name.dup end use_this_as_output_name << File.extname(i.first) end _ << '+append '+use_this_as_output_name esystem _ if File.exist? use_this_as_output_name e "Stored as `#{use_this_as_output_name}`." end return use_this_as_output_name end
#¶ ↑
ImageParadise.mirror_image¶ ↑
#¶ ↑
# File lib/image_paradise/toplevel_methods/mirror_image.rb, line 12 def self.mirror_image(this_image) if this_image.is_a? Array this_image.each {|entry| mirror_image(entry) } else name_of_the_output_file = 'mirrored_image_'+this_image _ = 'convert '+this_image+' \( -clone 0 -flop \) +append '+name_of_the_output_file esystem _ end end
#¶ ↑
ImageParadise.montage¶ ↑
This will aggregate the images together into one big image, the “montage”.
By default this method will pick up all .jpg files from the current working directory.
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 345 def self.montage( i = '*.jpg', output_file = :default, shall_we_autolabel_the_images = false, frame_value_to_use = '-frame 2' ) case shall_we_autolabel_the_images when :do_autolabel shall_we_autolabel_the_images = true end case output_file # ======================================================================= # # === :default # ======================================================================= # when :default output_file = 'output.png' end cmd = "montage".dup if shall_we_autolabel_the_images cmd << ' -label %f' end cmd << " #{frame_value_to_use}" cmd << " #{i}" cmd << " #{output_file}" esystem cmd end
#¶ ↑
ImageParadise.morph_these_images¶ ↑
This method can be used to morph together two images.
This is currently hardcoded/set to two images as input.
Any files that will be newly created OTHER than the main output file will be deleted.
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 180 def self.morph_these_images( image1, image2, output_file = :default ) case output_file # ======================================================================= # # === :default # ======================================================================= # when :default output_file = 'output.png' end _ = 'convert -morph 1,2 '+image1.to_s+' '+image2.to_s+' '+ output_file esystem _ modified_output_filename = output_file.sub(/\./,'-1.') if File.exist? modified_output_filename FileUtils.mv(modified_output_filename, output_file) end # ======================================================================= # # Next, clean up a bit. # ======================================================================= # if File.exist? output_file.sub(/\./,'-0.') File.delete(output_file.sub(/\./,'-0.')) end if File.exist? output_file.sub(/\./,'-2.') File.delete(output_file.sub(/\./,'-2.')) end return output_file end
#¶ ↑
ImageParadise.negate¶ ↑
#¶ ↑
# File lib/image_paradise/toplevel_methods/negate.rb, line 14 def self.negate( i, # <- The name of the file output_filename = :infer ) if i.is_a? Array i = i.join(' ').strip end case output_filename when :infer, :default output_filename = 'negated_'+File.basename(i) end _ = 'convert '+i+' -negate '+output_filename e esystem _ e return output_filename # We also return the filename here. end
#¶ ↑
ImageParadise.noise¶ ↑
Invocation example:
ImageParadise.noise("foobar.jpg", 2, 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 503 def self.noise( i = 'foobar.png', by_n = 1, output_file = 'output.png' ) esystem "convert "\ "-noise #{by_n} "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.normalize¶ ↑
Normalization is the process of improving the contrast in an image so that it uses all the available color range.
Invocation example:
ImageParadise.normalize("foobar.jpg", 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 743 def self.normalize( i = 'foobar.png', output_file = 'output.png' ) esystem "convert "\ "-normalize "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.png_to_svg¶ ↑
Use this method to convert a .png file to a .svg file.
This has several limitations since we use potrace for this.
potrace can not work with .png files, only with .pnm files, so we first have to convert these files.
The biggest limitation here is that it will remove all colours. :)
Usage example:
ImageParadise.png_to_svg 'COOKBOOKS_LOGO.png'
#¶ ↑
# File lib/image_paradise/toplevel_methods/png_to_svg.rb, line 28 def self.png_to_svg(this_input_file) pnm_file = this_input_file.gsub(/\.png$/,'')+'.pnm' svg_file = this_input_file.gsub(/\.png$/,'')+'.svg' _ = 'convert '+this_input_file+' '+pnm_file esystem _ _ = 'potrace '+pnm_file+' -s -o '+svg_file esystem _ return svg_file end
#¶ ↑
ImageParadise.quality¶ ↑
#¶ ↑
# File lib/image_paradise/toplevel_methods/quality.rb, line 14 def self.quality( i, n_percent = '30', output_filename = :guess ) if output_filename.is_a? Array output_filename = output_filename.first end n_percent = n_percent.to_s if i.is_a? Array i = i.join(' ').strip end case output_filename when nil, :guess output_file = 'quality_modified_'+File.basename(i) end _ = 'convert -quality '+n_percent.to_s+'% '+i+' '+output_file esystem _ end
#¶ ↑
ImageParadise.raise¶ ↑
The raise effect provides beveled edges for an image. You can specify the height and width for this bevel, as well as a direction for the bevel.
2x3 here means a horizontal bevel of 2 pixels and a vertical bevel size of 3 pixels.
Invocation example:
ImageParadise.raise("foobar.jpg", '2x3', 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 530 def self.raise( i = 'foobar.png', raise_by = '2x2', output_file = 'output.png' ) esystem "convert "\ "-raise #{raise_by} "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.remove_metadata_from_this_image¶ ↑
This method will strip meta-data from an image.
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 32 def self.remove_metadata_from_this_image( i, output_file = :default ) case output_file # ======================================================================= # # === :default # ======================================================================= # when :default output_file = "output_#{i}" end _ = "convert -strip #{i} #{output_file}" esystem _ end
#¶ ↑
ImageParadise.require_toplevel_methods¶ ↑
#¶ ↑
# File lib/image_paradise/requires/require_toplevel_methods.rb, line 15 def self.require_toplevel_methods Dir["#{PROJECT_BASE_DIRECTORY}toplevel_methods/*.rb"].each {|this_image| require "image_paradise/toplevel_methods/#{File.basename(this_image)}" } end
#¶ ↑
ImageParadise.resized¶ ↑
The second argument is n% resized.
#¶ ↑
# File lib/image_paradise/toplevel_methods/resized.rb, line 16 def self.resized( i, by_n_percent = '35%', output_filename = nil ) unless by_n_percent.is_a? String by_n_percent = by_n_percent.to_s end by_n_percent = by_n_percent.dup by_n_percent << '%' unless by_n_percent.end_with? '%' if i.is_a? Array i = i.join(' ').strip end case output_filename when nil output_filename = 'resized_'+File.basename(i) end _ = 'convert '+i+' -resize '+by_n_percent.to_s+' '+output_filename e esystem _ e return output_filename # We also return the filename here. end
#¶ ↑
ImageParadise.return_draw_line¶ ↑
We name the start point “Point A” and the end point “Point B”.
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 904 def self.return_draw_line( start_position = [10, 10], # This is the x,y start position. Point A. end_position = [70, 90], # This is the x,y end position. Point B. use_this_colour = :black, optional_hash = {} ) _ = ''.dup _ << "-fill #{use_this_colour} " if optional_hash.has_key? :strokewidth _ << "-stroke #{use_this_colour} " _ << "-strokewidth #{optional_hash[:strokewidth].to_s} " end _ << '-draw ' _ << "'line #{start_position[0]},#{start_position[1]} "\ "#{end_position[0]},#{end_position[1]}'" return _ end
#¶ ↑
ImageParadise.return_draw_rectangle¶ ↑
This method will return the commandline option to use for drawing a rectangle via the commandline.
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 94 def self.return_draw_rectangle( start_position = [10, 10], # This is the x,y start position. end_position = [70, 90], # This is width, then height. use_this_colour = :black ) "-fill #{use_this_colour} "\ "-draw "\ "'rectangle #{start_position[0]},#{start_position[1]} "\ "#{end_position[0]},#{end_position[1]}'" # Width. end
#¶ ↑
ImageParadise.return_random_image¶ ↑
This method will return a random image from the given argument, which is assumed to be a directory. It defaults to my home setup.
A random image will then be returned, if there is at the least one image in that directory.
Usage example for my home setup:
ImageParadise.return_random_image(:njoy)
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 1067 def self.return_random_image( i = Dir.pwd ) # ======================================================================= # # Handle special cases first. # ======================================================================= # case i when 0, :adoptopn i = ENV['IMG_RPG'].to_s+'/ADOPTION/' # ======================================================================= # # === njoy # ======================================================================= # when 1, :njoy i = ENV['IMG'].to_s+'/NJOY/' end i = i.to_s.dup unless i.end_with? '/' i << '/' end if File.directory? i all_entries = Dir["#{i}*"] return all_entries.sample else raise 'This method (.return_random_image()) '\ 'requires a valid directory as argument.' end end
#¶ ↑
ImageParadise.rotate_left¶ ↑
The second argument specifies the degree-rotation.
#¶ ↑
# File lib/image_paradise/rotate/rotate.rb, line 95 def self.rotate_left( these_images = ARGV, by_n_degrees = :rotate_left, use_this_background_colour = nil, output_file = :infer ) ImageParadise.rotate_this_image( these_images, by_n_degrees, use_this_background_colour, output_file ) end
#¶ ↑
ImageParadise.rotate_right¶ ↑
#¶ ↑
# File lib/image_paradise/rotate/rotate.rb, line 109 def self.rotate_right( these_images = ARGV, by_n_degrees = :rotate_right, use_this_background_colour = nil, output_file = :infer ) ImageParadise.rotate_this_image( these_images, by_n_degrees, use_this_background_colour, output_file ) end
#¶ ↑
ImageParadise.rotate_this_image¶ ↑
This method will always operate on the first input argument(s) in batch form - hence we convert the first argument given to an Array, inside of the method. Doing so allows the user to provide a bunch of images that will all receive the same rotate-operation.
The second input-argument to this method specifies the degree for the rotation; a value with a ‘+’ or without any modified token means “clockwise rotation”, that is rotation towards the right. A value that is negative here, aka has a leading ‘-’, means “counter-clockwise rotation”, that is to the left. Thus, rotate-left is counter-clockwise; and rotate-right is clockwise.
When we use convert, the commandline flags should look like this:
convert -resize "600>" image.png Newimage/image.png
#¶ ↑
# File lib/image_paradise/rotate/rotate.rb, line 47 def self.rotate_this_image( these_images = ARGV, by_n_degrees = '-45', # Default is -45. use_this_background_colour = nil, # ← Fill with this background colour. output_file = :infer ) case by_n_degrees when :rotate_left, :left by_n_degrees = '-90' when :rotate_right, :right by_n_degrees = '+90' end these_images = [these_images].flatten.compact these_images.each {|this_image| case output_file when :infer, :default, nil output_file = 'new_file_'+File.basename(this_image) end # ===================================================================== # # Note that the -density 300 part was added in March 2021, # after I noticed a significant loss in quality after # rotate-operations. # ===================================================================== # _ = 'convert -density 300 -rotate "'+by_n_degrees.to_s+'"'.dup _ << " #{this_image}" if use_this_background_colour case use_this_background_colour.to_s when :default use_this_background_colour = nil when 'black' use_this_background_colour = 'FFFFFF' end if use_this_background_colour _ << ' -background "#'+use_this_background_colour.to_s+'"' end end _ << " #{output_file}" esystem _ } end
#¶ ↑
ImageParadise.rounded_corner¶ ↑
This method can create a rounded corner to an existing image.
The polygon value allows one to create ellipsoid areas, which ultimately is the effect that you can see in a “rounded corner” image.
Usage example:
ImageParadise.rounded_corner 'RandomBeachGal.jpg'
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 871 def self.rounded_corner( i = 'input_image.png', output_file = :default, black_polygon = '0,0 0,15 15,0' ) if i.is_a? Array i = i.join(' ').strip end i = i.to_s case output_file # ======================================================================= # # === :default # ======================================================================= # when :default output_file = File.absolute_path(File.basename("output_#{i}")) end _ = 'convert '+i+' '+ '\( +clone -alpha extract '+ '-draw \'fill black polygon '+black_polygon.to_s+ ' fill white circle 15,15 15,0\' '+ '\( +clone -flip \) -compose Multiply -composite '+ '\( +clone -flop \) -compose Multiply -composite '+ '\) -alpha off -compose CopyOpacity -composite '+ output_file.to_s esystem _ return output_file end
#¶ ↑
ImageParadise.sepia_tone¶ ↑
Invocation example:
ImageParadise.sepia_tone("foobar.jpg", '80', 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 550 def self.sepia_tone( i = 'foobar.png', n_percent = '80', output_file = 'output.png' ) esystem "convert "\ "-sepia-tone #{n_percent.to_s.delete('%')}% "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.shade_transformation¶ ↑
This will apply a shade onto the main image.
A shade effect in this context refers to simulating having a distant light source cast a shadow on the image.
This effect can be created by specifying an azimuth and elevation for the light source. The azimuth is the angle of the light source, with the following values:
0 degrees: north 90 degrees: east 180 degrees: south 270 degrees: west
The elevation is the height of the light source. For example, to specify light from the east at height of 100, the following commandline can be used:
convert -shade 90x100 input.jpg output.jpg
Invocation example:
ImageParadise.shade_transformation("foobar.jpg", '80', 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 590 def self.shade_transformation( i = 'foobar.png', modify_how = '90x100', output_file = 'output.png' ) esystem "convert "\ "-shade #{modify_how.to_s.delete('%')} "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.shadow¶ ↑
This will create a shadow effect. The second argument is the colour that can be used, defaulting to :steelblue if omitted.
Usage example:
ImageParadise.shadow('Leeann_Tweeden.png', :blue)
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 282 def self.shadow( i = '*.jpg', colour_to_use = :default, # This means 50, basically. output_file = :default, width = 3 # 3px width. ) case output_file # ======================================================================= # # === :default # ======================================================================= # when :default output_file = 'output.png' end case colour_to_use when :default colour_to_use = :steelblue end _ = 'convert '+i.to_s+' -alpha set '\ '\( +clone -background '+ colour_to_use.to_s+' -shadow 60x0+'+width.to_s+'+'+width.to_s+' \) '\ '+swap -background none -mosaic '+ output_file.to_s esystem _ end
#¶ ↑
ImageParadise.sharpen¶ ↑
This method can be used to sharpen an existing image.
Invocation example:
ImageParadise.sharpen("foobar.jpg", '3', 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 628 def self.sharpen( i = 'foobar.png', modify_how = '1', output_file = 'output.png' ) esystem "convert "\ "-sharpen #{modify_how.to_s} "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.shear¶ ↑
The shear effect puts the input image at an angle. You can tilt the image horizontally and vertically in both directions (in other words, from left to right, from right to left, from top to bottom, and from bottom to top).
The tilt is specified as an angle; for example, in the horizontal direction, a negative tilt is a tilt to the left, and a positive tilt is a tilt to the right.
Invocation example:
ImageParadise.shear("foobar.jpg", 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 771 def self.shear( i = 'foobar.png', modify_how = '45', output_file = 'output.png' ) esystem "convert "\ "-shear #{modify_how.to_s} "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.sheared_shadow_text¶ ↑
#¶ ↑
# File lib/image_paradise/toplevel_methods/font_related_methods.rb, line 35 def self.sheared_shadow_text( this_text = 'Anthony Quinn', this_font = 'Hack-Regular', # 'Hack' width_and_height = '550x150' ) store_here = 'output_file.jpg' esystem 'convert '\ '-size '+width_and_height.to_s+' '\ 'xc:lightblue '\ '-font '+this_font+' '\ '-pointsize 50 '\ '-fill Navy -annotate 0x0+8+55 "'+this_text.to_s+'" '\ '-fill RoyalBlue -annotate 0x30+35+90 "'+this_text.to_s+'" '+ # =============================================================== # # Format is: # # -annotate XdegreesxYdegrees{+-}tx{+-}ty text # # =============================================================== # store_here end
#¶ ↑
ImageParadise.show_help (help tag)¶ ↑
Invocation example:
image_paradise --help
#¶ ↑
# File lib/image_paradise/toplevel_methods/help.rb, line 17 def self.show_help heredoc_variable = <<-EOF Right now we only support hese options: --autogenerate-black-wallpaper --wallpaper --cfdg # batch-generate the cfdg files --merge # merge together the given input image-files EOF e heredoc_variable end
#¶ ↑
ImageParadise.solarize¶ ↑
This method can be used to lighten-up an existing image.
Invocation example:
ImageParadise.solarize("foobar.jpg", '3', 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 720 def self.solarize( i = 'foobar.png', modify_how = '90', output_file = 'output.png' ) esystem "convert "\ "-solarize #{modify_how.to_s} "\ "#{i} "\ "#{output_file}" return output_file end
#¶ ↑
ImageParadise.store_image_files_in_this_directory=¶ ↑
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 49 def self.store_image_files_in_this_directory=( i = '/home/x/data/images/STUDIUM/' ) i << '/' unless i.end_with? '/' # Directories must have a trailing '/' token. @hash[:store_image_files_in_this_directory] = i end
#¶ ↑
ImageParadise.thumbnail¶ ↑
This method can be used to generate a thumbnail from a local, existing image.
The third argument is the filename for the generated-file. Normally this is inferred automatically, hence why the symbol :infer is used for this, but the user can also provide his or her own filename here.
#¶ ↑
# File lib/image_paradise/toplevel_methods/thumbnail.rb, line 22 def self.thumbnail( i, width_height = :default, output_filename = :infer ) case width_height # ======================================================================= # # === :default # ======================================================================= # when :default, :default_width_and_size width_height = '250x90' end if i.is_a? Array i = i.join(' ').strip end case output_filename when :infer output_filename = 'thumbnail_'+File.basename(i) end _ = 'convert '+i+' -thumbnail '+ width_height.to_s+' '+output_filename e esystem _ e return output_filename # We also return the filename here. end
#¶ ↑
ImageParadise.to_webp¶ ↑
This will convert the given image to the WebP format.
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 836 def self.to_webp( this_image, output_file = :append, quality_to_use = 80 ) case output_file # ======================================================================= # # === :append # ======================================================================= # when :append, :default output_file = this_image.dup.delete_suffix( File.extname(this_image) )+'.webp' end esystem 'magick '+this_image+ ' -quality '+quality_to_use.to_s+ ' -define webp:lossless=true '+ output_file end
#¶ ↑
ImageParadise.torn_paper¶ ↑
This will create a torn-paper effect.
Usage example:
ImageParadise.torn_paper('SANDRA.jpg') ImageParadise.torn_paper('SANDRA.jpg', 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 244 def self.torn_paper( i = '*.jpg', threshold = :default, # This means 50, basically. output_file = :default ) case output_file # ======================================================================= # # === :default # ======================================================================= # when :default output_file = 'output.png' end case threshold # ======================================================================= # # === :default # ======================================================================= # when :default threshold = 50 end _ = 'convert '+i.to_s+' \( +clone -alpha extract -virtual-pixel '\ 'black -spread 10 -blur 0x3 -threshold '+threshold.to_s+ '% -spread 1 -blur 0x.7 \) '\ ' -alpha off -compose Copy_Opacity -composite '+ output_file.to_s esystem _ end
#¶ ↑
ImageParadise.vertical_append¶ ↑
This will append images together, in a vertical manner.
Invocation example:
ImageParadise.vertical_append(["foobar1.jpg", "foobar2.jpg"], 'output.png')
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 817 def self.vertical_append( these_images = ARGV, output_file = 'output.png', use_this_as_border = nil ) cmd = "convert -append #{these_images.join(' ')}".dup cmd << " #{output_file}" if use_this_as_border cmd << "-border #{use_this_as_border}" end esystem cmd return output_file end
#¶ ↑
ImageParadise.wallpaper¶ ↑
Use this method if you need to create a wallpaper.
#¶ ↑
# File lib/image_paradise/toplevel_methods/wallpaper.rb, line 16 def self.wallpaper( colour_to_use = 'black', # 1st argument: colour to use width = :default, # 2nd argument: width of the image height = :default # 3rd argument: height of the image ) case width when :default width = '1980' end case height when :default height = '1020' end if colour_to_use.is_a? Hash if colour_to_use.has_key? :colour colour_to_use = colour_to_use[:colour] end end _ = "convert -size #{width}x#{height} xc:#{colour_to_use} "\ "#{colour_to_use}image.png" esystem _ end
#¶ ↑
ImageParadise.width_height_of_this_image?¶ ↑
Usage example:
ImageParadise.width_height_of_this_image?("/home/x/data/images/fotos/urlaub/10.10.2004_Trieste_Miramare_Teich.jpg")
#¶ ↑
# File lib/image_paradise/toplevel_methods/misc.rb, line 1272 def self.width_height_of_this_image?(i) return `convert #{i} -print "%wx%h" /dev/null` end
#¶ ↑
ImageParadise.write_this_text¶ ↑
This will write to an image file.
Invocation example:
ImageParadise.write_this_text('Bachelor', {colour: 'red', height: 80, width: 250})
#¶ ↑
# File lib/image_paradise/toplevel_methods/write_this_text.rb, line 21 def self.write_this_text( text = DEFAULT_TEXT, hash_options = {} ) hash = hash_options # Shorter pointer. height = hash[:height].to_s width = hash[:width].to_s # ======================================================================= # # use_this_font = ' -font Rekha' # use_this_font = " -font 'Nimbus Mono L'" # ======================================================================= # use_this_font = " -font 'Luxi-Serif-Bold'" _ = 'convert '.dup # ======================================================================= # # === :colour # ======================================================================= # if hash.has_key? :colour _ << " -fill #{hash[:colour]}" else _ << ' -fill white' end _ << use_this_font _ << ' -background white' # ======================================================================= # # If the user supplied the value for :pointsize, then we will use it. # ======================================================================= # if hash.has_key? :pointsize _ << ' -pointsize '+hash[:pointsize].to_s else # Otherwise we will use a default value. _ << ' -pointsize 28' end # ======================================================================= # # Next add the label (aka the corresponding text). # ======================================================================= # _ << " label:'"+text+"'" _ << ' -weight 600' _ << ' -gravity center' # Align into the middle. # ======================================================================= # # width-height setting. # # See: # # https://www.imagemagick.org/script/command-line-options.php#size # # ======================================================================= # _ << " -size #{width}x#{height}" _ << " -extent #{width}x#{height}" # ======================================================================= # # Add a border. # ======================================================================= # _ << ' -bordercolor black -border 1' sanitized_text = text.tr(' ','_').dup if hash[:upcase_filename] sanitized_text.upcase! end # ======================================================================= # # Next, decide where to store the image file. # ======================================================================= # store_here = "#{sanitized_text}.png" if store_image_files_in_this_directory? store_here.prepend( store_image_files_in_this_directory? ) end _ << " #{store_here}" esystem _ end
Public Instance Methods
#¶ ↑
create_animated_gif
¶ ↑
#¶ ↑
# File lib/image_paradise/toplevel_methods/create_animated_gif.rb, line 42 def create_animated_gif( input_images = INPUT_IMAGES_FOR_THE_CREATION_OF_ANIMATED_GIFS, delay = DELAY_FOR_THE_CREATION_OF_ANIMATED_GIFS ) ImageParadise.create_animated_gif(input_images, delay) end
#¶ ↑
crop¶ ↑
Delegate towards the class-method above, called ImageParadise.crop()
#¶ ↑
# File lib/image_paradise/toplevel_methods/crop.rb, line 18 def crop( i = filename?, crop_command_to_use = nil, optional_hash = {}, &block ) ImageParadise.crop( i, crop_command_to_use, optional_hash, &block ) end