image_magick 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/README.md +40 -0
- data/Rakefile +1 -0
- data/bin/shrink +20 -0
- data/image_magick.gemspec +54 -0
- data/lib/image_magick/image.rb +47 -0
- data/lib/image_magick/shrink.rb +38 -0
- data/lib/image_magick/version.rb +3 -0
- data/lib/image_magick.rb +43 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f1238d359da65f317337c1f46bb262a46310c0bd
|
4
|
+
data.tar.gz: bd1d2486006961063c5e3bd05f144b39dffc597f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 07638049dc66a828b8d7d36c0ab72426ba5ec1b3405074f8d14d5a66cbc348573a79950634f4688ec3790d60ccdf04294e8f8a37959937def34f7f658942b529
|
7
|
+
data.tar.gz: 93c0f259910e4c350ec29526910c5aebc18588073b4bf9f93642ffb4e13655602201cec97805ad0e58d3b817cfb9623340b0f2c6667d205767062d961365af70
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
Prerequisites: before this gem will work, "Image Magick" must be installed
|
2
|
+
on your computer.
|
3
|
+
|
4
|
+
On a Mac:
|
5
|
+
Given that you are using brew, use this command:
|
6
|
+
|
7
|
+
'brew install imagemagick'
|
8
|
+
|
9
|
+
I wrote this for my spouse, who needed to resize scanned artwork for various
|
10
|
+
art websites, such as 'etsy', 'artpal', and 'art.com'. Each of these sites has
|
11
|
+
specific requirements for file size, format (png, tif, and jpg), or pixel
|
12
|
+
dimensions. There are a few hard-coded parts of the script, that don't hamper
|
13
|
+
my use of it, but that I should bring to attention.
|
14
|
+
|
15
|
+
Execute the script like this
|
16
|
+
shrink ./path/to/image [--size 100] [--type tif]
|
17
|
+
|
18
|
+
You only need to provide image. Size and type are defaulted to 100 megabytes
|
19
|
+
and tif respectively.
|
20
|
+
|
21
|
+
|
22
|
+
The output files are hard-coded to be placed in an 'out' directory, at the same
|
23
|
+
level as the image. For example, if we're working with a file called
|
24
|
+
~/images/myimage.tif
|
25
|
+
the output file will be in
|
26
|
+
~/images/out/myimage.tif
|
27
|
+
|
28
|
+
The script is very primitive; it runs the 'identify' command to find out
|
29
|
+
the size of the image. the 'image' class will respond to things like 'size',
|
30
|
+
'width', and 'length'.
|
31
|
+
|
32
|
+
the 'shrink' algorithm is also fairly primitive. It iteratively tries
|
33
|
+
different sizes, using a binary search algorithm to find the best fit that
|
34
|
+
is as close to the maximum size as possible, without exceeding that size.
|
35
|
+
|
36
|
+
Run ./bin/shrink.rb -h
|
37
|
+
|
38
|
+
Example usage:
|
39
|
+
./bin/shrink.rb ./image.tif [--size=100] [--type=png]
|
40
|
+
to see command line arguments
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/shrink
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
require_relative '../lib/image_magick/image_magick'
|
6
|
+
|
7
|
+
type = '.tif'
|
8
|
+
new_size = 100
|
9
|
+
OptionParser.new do |opt|
|
10
|
+
opt.on('--size SIZE') { |o| new_size = o.to_f }
|
11
|
+
opt.on('--type TYPE') { |o| type = "." + o }
|
12
|
+
opt.on('-h') { |o| puts o ; exit }
|
13
|
+
opt.parse!
|
14
|
+
end
|
15
|
+
|
16
|
+
file = ARGV.shift
|
17
|
+
|
18
|
+
p @new_size
|
19
|
+
|
20
|
+
ImageMagick.shrink(file,new_size,type)
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'image_magick/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "image_magick"
|
8
|
+
spec.version = ImageMagick::VERSION
|
9
|
+
spec.authors = ["Timothy Nordloh"]
|
10
|
+
spec.email = ["tnordloh@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple interface on Image Magick, for shrinking images.}
|
13
|
+
spec.description = <<-EOS
|
14
|
+
This is a very trivial implementation of an interface for Image Magick. It
|
15
|
+
relies on the binaries for Image Magic (identify and convert) to be available
|
16
|
+
on the system, and to be in the $PATH. I wrote this for my spouse, who needed
|
17
|
+
to resize scanned artwork for various art websites, such as 'etsy', 'artpal',
|
18
|
+
and 'art.com'. Each of these sites has specific requirements for file size,
|
19
|
+
format (png, tif, and jpg), or pixel dimensions. There are a few hard-coded
|
20
|
+
parts of the script, that don't hamper my use of it, but that I should bring
|
21
|
+
to attention.
|
22
|
+
Execute the script like this
|
23
|
+
shrink ./path/to/image [--size 100] [--type tif]
|
24
|
+
|
25
|
+
You only need to provide image. Size and type are defaulted to 100 megabytes
|
26
|
+
and tif respectively.
|
27
|
+
|
28
|
+
|
29
|
+
The script is very primitive; it runs the 'identify' command to find out
|
30
|
+
the size of the image. the 'image' class will respond to things like 'size',
|
31
|
+
'width', and 'length'.
|
32
|
+
|
33
|
+
the 'shrink' algorithm is also fairly primitive. It iteratively tries
|
34
|
+
different sizes, using a binary search algorithm to find the best fit that
|
35
|
+
is as close to the maximum size as possible, without exceeding that size.
|
36
|
+
EOS
|
37
|
+
spec.homepage = "https://github.com/tnordloh/image_magick"
|
38
|
+
|
39
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
40
|
+
# delete this section to allow pushing this gem to any host.
|
41
|
+
if spec.respond_to?(:metadata)
|
42
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
43
|
+
else
|
44
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
45
|
+
end
|
46
|
+
|
47
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
48
|
+
spec.bindir = "exe"
|
49
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
50
|
+
spec.require_paths = ["lib"]
|
51
|
+
|
52
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
53
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
54
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module ImageMagick
|
2
|
+
class Image
|
3
|
+
|
4
|
+
require 'pathname'
|
5
|
+
require_relative '../image_magick'
|
6
|
+
|
7
|
+
def initialize(file)
|
8
|
+
ImageMagick.exists?
|
9
|
+
@image = file
|
10
|
+
image_data
|
11
|
+
end
|
12
|
+
|
13
|
+
attr_accessor :width, :length, :size
|
14
|
+
|
15
|
+
def output_dir(out: "/out")
|
16
|
+
output_dir = File.dirname(@image) + out
|
17
|
+
Dir.mkdir(output_dir) unless File.exists?(output_dir)
|
18
|
+
p output_dir
|
19
|
+
end
|
20
|
+
|
21
|
+
def image_data
|
22
|
+
@fields = identify.split(/\s+/)
|
23
|
+
fields = identify.split(/\s+/)
|
24
|
+
@width,@length = fields[2].split('x').map {|x| x.to_i}
|
25
|
+
@size = fields[6][/^\d+\.*\d*/].to_f
|
26
|
+
end
|
27
|
+
|
28
|
+
def identify
|
29
|
+
@identify ||= `identify #{@image}`
|
30
|
+
end
|
31
|
+
|
32
|
+
def name
|
33
|
+
@name ||= Pathname.new(@fields[0].split('[')[0])
|
34
|
+
end
|
35
|
+
|
36
|
+
def shortname
|
37
|
+
name.basename(name.extname)
|
38
|
+
end
|
39
|
+
|
40
|
+
def compress(type: '.tif', test: false, newsize: 1.0 )
|
41
|
+
new_file = output_dir + "/" + shortname.to_s + type
|
42
|
+
p command = "convert #{name} -resize #{newsize} -compress lzw #{new_file}"
|
43
|
+
test == true ? "#{command}" : `#{command}`
|
44
|
+
self.class.new(new_file)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Shrink
|
2
|
+
|
3
|
+
require_relative 'image'
|
4
|
+
|
5
|
+
def self.shrink(file,size,type)
|
6
|
+
|
7
|
+
temp_file="./temp.#{type}"
|
8
|
+
|
9
|
+
p "file exists?"
|
10
|
+
p File.exist?(file)
|
11
|
+
original_image = Image.new(file)
|
12
|
+
|
13
|
+
p original_image
|
14
|
+
image = original_image.compress(newsize: original_image.width, type: type)
|
15
|
+
exit if image.size < @new_size
|
16
|
+
|
17
|
+
low = 0
|
18
|
+
high = original_image.width
|
19
|
+
|
20
|
+
while (high - low) > 1 do
|
21
|
+
@try_size = ( high + low ) / 2
|
22
|
+
p "low: #{low}, high #{high}, try #{@try_size}"
|
23
|
+
p "reducing to (#{@try_size})"
|
24
|
+
image = original_image.compress(newsize: @try_size, type: type)
|
25
|
+
p image.size
|
26
|
+
if image.size < @new_size
|
27
|
+
low = @try_size
|
28
|
+
else
|
29
|
+
high = @try_size
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
if image.size > @new_size
|
34
|
+
image = original_image.compress(newsize: high - 1, type: type)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
data/lib/image_magick.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require_relative 'image_magick/version'
|
2
|
+
|
3
|
+
module ImageMagick
|
4
|
+
|
5
|
+
require 'mkmf'
|
6
|
+
|
7
|
+
require_relative 'image_magick/image'
|
8
|
+
|
9
|
+
def self.exists?
|
10
|
+
raise RuntimeError,"convert binary doesn't exist" unless !!find_executable('convert')
|
11
|
+
raise RuntimeError,"identify binary doesn't exist" unless !!find_executable('identify')
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.shrink(file,size,type)
|
15
|
+
|
16
|
+
temp_file="./temp.#{type}"
|
17
|
+
original_image = ImageMagick::Image.new(file)
|
18
|
+
|
19
|
+
p original_image
|
20
|
+
image = original_image.compress(newsize: original_image.width, type: type)
|
21
|
+
p image
|
22
|
+
exit if image.size < size
|
23
|
+
|
24
|
+
low = 0
|
25
|
+
high = original_image.width
|
26
|
+
|
27
|
+
while (high - low) > 1 do
|
28
|
+
@try_size = ( high + low ) / 2
|
29
|
+
image = original_image.compress(newsize: @try_size, type: type)
|
30
|
+
p image.size
|
31
|
+
if image.size < size
|
32
|
+
low = @try_size
|
33
|
+
else
|
34
|
+
high = @try_size
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
if image.size > size
|
39
|
+
image = original_image.compress(newsize: high - 1, type: type)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: image_magick
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Timothy Nordloh
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: " This is a very trivial implementation of an interface for Image Magick.
|
42
|
+
\ It\n relies on the binaries for Image Magic (identify and convert) to be available\n
|
43
|
+
\ on the system, and to be in the $PATH. I wrote this for my spouse, who needed\n
|
44
|
+
\ to resize scanned artwork for various art websites, such as 'etsy', 'artpal',\n
|
45
|
+
\ and 'art.com'. Each of these sites has specific requirements for file size,\n
|
46
|
+
\ format (png, tif, and jpg), or pixel dimensions. There are a few hard-coded\n
|
47
|
+
\ parts of the script, that don't hamper my use of it, but that I should bring\n
|
48
|
+
\ to attention.\n Execute the script like this\n shrink ./path/to/image [--size
|
49
|
+
100] [--type tif]\n \n You only need to provide image. Size and type are defaulted
|
50
|
+
to 100 megabytes\n and tif respectively.\n\n\n The script is very primitive; it
|
51
|
+
runs the 'identify' command to find out\n the size of the image. the 'image' class
|
52
|
+
will respond to things like 'size', \n 'width', and 'length'. \n\n the 'shrink'
|
53
|
+
algorithm is also fairly primitive. It iteratively tries \n different sizes, using
|
54
|
+
a binary search algorithm to find the best fit that\n is as close to the maximum
|
55
|
+
size as possible, without exceeding that size.\n"
|
56
|
+
email:
|
57
|
+
- tnordloh@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- Gemfile
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- bin/shrink
|
66
|
+
- image_magick.gemspec
|
67
|
+
- lib/image_magick.rb
|
68
|
+
- lib/image_magick/image.rb
|
69
|
+
- lib/image_magick/shrink.rb
|
70
|
+
- lib/image_magick/version.rb
|
71
|
+
homepage: https://github.com/tnordloh/image_magick
|
72
|
+
licenses: []
|
73
|
+
metadata:
|
74
|
+
allowed_push_host: https://rubygems.org
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.2.2
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: A simple interface on Image Magick, for shrinking images.
|
95
|
+
test_files: []
|