Transformasi

Potong dan edit

Beberapa fungsi transformasi mengambil geometry parameter yang memerlukan sintaks khusus dari bentuk AxB+C+Ddi mana setiap elemen bersifat opsional. Beberapa contoh:

library(magick)
## Linking to ImageMagick 6.9.12.3
## Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fontconfig, x11
# Example image
naruto <- image_read("naruto.png")
plot(naruto)

# Add 20px left/right and 10px top/bottom
image_border(image_background(naruto, "hotpink"), "#000080", "20x10")

# Trim margins
image_trim(naruto)

# Passport pica
image_crop(naruto, "200x250+200")

# Resize
image_scale(naruto, "300") # width: 300px

image_scale(naruto, "x300") # height: 300px

# Rotate or mirror
image_rotate(naruto, 45)

image_flip(naruto)

image_flop(naruto)

# Brightness, Saturation, Hue
image_modulate(naruto, brightness = 80, saturation = 120, hue = 90)

# Paint the shirt orange
image_fill(naruto, "orange", point = "+100+200", fuzz = 20)

Dengan image_fill kita dapat mengisi mulai dari piksel point. Parameter fuzz memungkinkan isian menyilang untuk piksel yang berdekatan dengan warna serupa. Nilainya harus antara 0 dan 256^2 menentukan jarak geometris maksimal antara warna yang dianggap sama.

Filter dan Efek

ImageMagick juga memiliki banyak efek standar :

# Add randomness
image_blur(naruto, 10, 5)

image_noise(naruto)

# Silly filters
image_charcoal(naruto)

image_oilpaint(naruto)

image_negate(naruto)

Konvolusi Kernel

Fungsi image_convolve() menerapkan kernel di atas gambar. Konvolusi kernel berarti bahwa setiap nilai piksel dihitung ulang menggunakan jumlah lingkungan berbobot yang ditentukan dalam matriks kernel. Sebagai contoh mari kita lihat kernel sederhana ini:

kern <- matrix(0, ncol = 3, nrow = 3)
kern[1, 2] <- 0.25
kern[2, c(1, 3)] <- 0.25
kern[3, 2] <- 0.25
kern
##      [,1] [,2] [,3]
## [1,] 0.00 0.25 0.00
## [2,] 0.25 0.00 0.25
## [3,] 0.00 0.25 0.00

Kernel ini mengubah setiap piksel menjadi rata-rata piksel tetangga horizontal dan vertikalnya, yang menghasilkan sedikit efek buram pada gambar sebelah kanan di bawah ini:

img <- image_resize(logo, "300x300")
img_blurred <- image_convolve(img, kern)
image_append(c(img, img_blurred))

atau menggunakan salah satu kernel standar :

img %>% image_convolve('Sobel') %>% image_negate()

img %>% image_convolve('DoG:0,0,2') %>% image_negate()

Anotasi Teks

Akhirnya berguna untuk mencetak beberapa teks di atas gambar:

# Add some text
image_annotate(naruto, "Uzumaki Naruto", size = 70, gravity = "southwest", color = "black")

# Customize text
image_annotate(naruto, "CONFIDENTIAL", size = 100, color = "black", boxcolor = "red",
  degrees = 70, location = "+250+100")

# Fonts may require ImageMagick has fontconfig
image_annotate(naruto, "Uzumaki Naruto", font = 'Comic Sans', size = 70)

Font yang didukung pada sebagian besar platform meliputi "sans", "mono", "serif", "Times", "Helvetica", "Trebuchet", "Georgia", "Palatino"atau "Comic Sans".

Menggabungkan dengan Pipa

Setiap fungsi transformasi gambar mengembalikan salinan yang dimodifikasi dari gambar asli. Itu tidak mempengaruhi gambar asli.

naruto <- image_read("naruto.png")
naruto2 <- image_scale(naruto, "100")
image_info(naruto)
##   format width height colorspace matte filesize density
## 1    PNG   880    830       sRGB  TRUE   161414   28x28
image_info(naruto2)
##   format width height colorspace matte filesize density
## 1    PNG   100     94       sRGB  TRUE        0   28x28

Karenanya untuk menggabungkan transformasi, Anda perlu merantainya:

test <- image_rotate(naruto, 90)
test <- image_background(test, "yellow", flatten = TRUE)
test <- image_border(test, "orange", "10x10")
test <- image_annotate(test, "This is how we combine transformations", color = "black", size = 30)
print(test)
##   format width height colorspace matte filesize density
## 1    PNG   850    900       sRGB  TRUE        0   28x28

Menggunakan magrittr sintaks pipa membuatnya sedikit lebih mudah dibaca

image_read("naruto.png") %>%
  image_rotate(270) %>%
  image_background("yellow", flatten = TRUE) %>%
  image_border("orange", "10x10") %>%
  image_annotate("The same thing with pipes", color = "black", size = 30)