Article: Tạo chữ ký trong ảnh kỹ thuật số 2136

Chưa phân loại
ngocdaothanh.myopenid.com 149
Updated over 2 years ago
Máy ảnh kỹ thuật số đang “à la mode” hiện nay và được khá nhiều người nghiệp dư sử dụng để thay thế cho máy ảnh phim. Việc sử dụng máy tính và các chương trình xử lý ảnh kỹ thuật số tương đương với việc xử lý ảnh phim trong phòng tối (darkroom). Hiện nay có rất nhiều chương trình dùng để xử lý ảnh kể cả phần mềm thương mại, miễn phí cũng như phần mềm mã nguồn mở. Có thể kể một số phần mềm thông dụng như: Photoshop, Photoshop Element, Gimp, LightZone, Picasa…Một số chương trình có hỗ trợ việc xử lý ảnh hàng loạt với batch, một số lại không có tính năng đó.

 

Trong bài này, các bạn sẽ xem qua cách sử dụng thư viện RMagickEXIFR để tạo chữ ký trong ảnh kỹ thuật số.

RMagick và EXIFR

RMagick là thư viện Ruby dùng để gọi đến các tính năng được cung cấp bởi chương trình ImageMagick (hay bản fork của nó là GraphicsMagick).

EXIFR là thư viện để đọc thông tin về hình ảnh trong cái tệp JPEGTIFF. Ví dụ như các thông tin sau có thể lấy từ hình như: tiêu cự, tốc độ màn trập, ISO, thời gian chụp, khẩu độ …

Script tạo chữ ký cho hình ảnh

Xem xét đoạn code dưới đây.

# This script will produce signature which form below# Canon 20D, at 150mm, F/5.6, 1/300s, ISO 100 - (c) 2007 tndrequire 'rubygems'require 'RMagick'require 'exifr'include Magickinclude EXIFRif !ARGV[0]  puts "Usage: signed.rb <image-file-name> -s"   puts " -s: simple signature option"   exitenddef gen_signature(img_path, simple_signature)  taken_year = Time.now.year  current_user = ENV['USER'].upcase  if simple_signature || !JPEG.new(img_path).exif?    signature = "© #{taken_year} #{current_user}"   else    exif = JPEG.new(img_path).exif    taken_year    = exif.date_time.year  # set year taken from EXIF if exists    current_user  = ENV['USER'].upcase    camera_model  = exif.model    focal_length  = exif.focal_length.to_i.to_s    fnumber       = exif.f_number.to_f.to_f.to_s    exposure_time = exif.exposure_time.numerator.to_s + "/" + exif.exposure_time.denominator.to_s    iso_speed     = exif.iso_speed_ratings.to_s    signature     = "#{camera_model}, at #{focal_length}mm, F/#{fnumber}, #{exposure_time}s, ISO #{iso_speed}, © #{taken_year} #{current_user}"   endenddef gen_image(image_path, signature)  image = Image.read(img_path).first  border = image.columns.div 55  image.border!(border,border,"#f0f0ff")  mark = Image.new(image.columns+border, image.rows+border)  gc = Draw.new  gc.gravity = Magick::SouthEastGravity  gc.pointsize = 16  gc.font_family = "Helvetica"   gc.font_weight = BoldWeight  gc.stroke = 'none'  gc.annotate(mark, 0, 0, border, border, signature)  mark = mark.shade(true, 310,30)  image.composite!(mark, Magick::SouthEastGravity, Magick::HardLightCompositeOp)  out = ARGV[0].swapcase.sub(/\./, "-wm.")  puts "Writing #{out}"   image.write(out)endimg_path  = ARGV[0]mode      = ARGV[1] ? true : falsesignature = gen_signature(img_path, mode)gen_image(img_path, signature)

Dòng exif = JPEG.new(img_path).exif đọc EXIF của tệp JPEG.

Các dòng trong method gen_image sử dụng thư viện RMagick để

  1. Tạo khung hình: image.border!(border,border,"#f0f0ff")
  2. Tạo chữ: gc.annotate(mark, 0, 0, border, border, signature)
  3. Ghép chữ vào hình: image.composite!(mark, Magick::SouthEastGravity, Magick::HardLightCompositeOp)

1 2 3 

Editors
tnd.myopenid.com 17
id.cntt.tv/[Anonymous] 9
ngocdaothanh.myopenid.com 149

Comments

ngocdaothanh.myopenid.com 149
over 3 years ago
Nghe nói anh Javi đang soạn bài giới thiệu về RMagick, nên xin cắt bớt cụm “Sử dụng RMagick và EXIFR để” trong tiêu đề để nó gọn lại.

Canon 20D, at 150mm, F/5.6, 1/300s, ISO 100 – (c) 2007 tnd

Chương trình chỉ dùng được cho ảnh chụp bằng Canon thôi anh nhỉ?

phananhvu.myopenid.com 125
over 2 years ago
hay quá, nhưng code lộn xộn nên chả đọc chả hiểu gì

You must login to be able to comment

Uploaded files

No file uploaded yet

You must login to be able to upload

Nhà tài trợ:

Mọi người đều tự do viết bài, sửa bài của người khác, và bình luận ở trang web này. Bạn muốn chủ động tạo bài mới để chia sẻ kinh nghiệm với mọi người? Xin click link ở dưới.

Create new content