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

Transformations

Cara terbaik untuk memahami transformasi yang tersedia adalah menelusuri contoh-contoh di halaman bantuan transformasi di RStudio. Di bawah ini beberapa contoh untuk memahami apa yang mungkin.

Cut and edit

Beberapa fungsi transformasi mengambil parameter geometri yang memerlukan sintaks khusus dari bentuk AxB+C+D di mana setiap elemen bersifat opsional.

# Example image
frink <- image_read("https://jeroen.github.io/images/frink.png")
print(frink)
##   format width height colorspace matte filesize density
## 1    PNG   220    445       sRGB  TRUE    73494   72x72

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

# Trim margins
image_trim(frink)

# Passport pica
image_crop(frink, "100x150+50")

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

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

# Rotate or mirror
image_rotate(frink, 45)

image_flip(frink)

image_flop(frink)

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

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

Dengan image_fill kita dapat mengisi banjir mulai dari titik piksel. 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. Di sini kami memberi profesor frink kemeja oranye untuk Piala Dunia.

Filters and effects

ImageMagick juga memiliki banyak efek standar yang patut dicoba.

# Add randomness
image_blur(frink, 10, 5)

image_noise(frink)

# Silly filters
image_charcoal(frink)

image_oilpaint(frink)

image_negate(frink)

Kernel convolution

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 gunakan salah satu kernel standar

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

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

Text annotation

Akhirnya berguna untuk mencetak beberapa teks di atas gambar:

# Add some text
image_annotate(frink, "I like R!", size = 70, gravity = "southwest", color = "green")

# Customize text
image_annotate(frink, "CONFIDENTIAL", size = 30, color = "red", boxcolor = "pink",
  degrees = 60, location = "+50+100")

# Fonts may require ImageMagick has fontconfig
image_annotate(frink, "The quick brown fox", font = 'Times', size = 30)

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

Combining with pipes

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

frink <- image_read("https://jeroen.github.io/images/frink.png")
frink2 <- image_scale(frink, "100")
image_info(frink)
##   format width height colorspace matte filesize density
## 1    PNG   220    445       sRGB  TRUE    73494   72x72
image_info(frink2)
##   format width height colorspace matte filesize density
## 1    PNG   100    202       sRGB  TRUE        0   72x72

Karenanya untuk menggabungkan transformasi, Anda perlu merantainya:

test <- image_rotate(frink, 90)
test <- image_background(test, "blue", flatten = TRUE)
test <- image_border(test, "red", "10x10")
test <- image_annotate(test, "This is how we combine transformations", color = "white", size = 30)
print(test)
##   format width height colorspace matte filesize density
## 1    PNG   465    240       sRGB  TRUE        0   72x72

Menggunakan sintaks pipa magrittr membuatnya sedikit lebih mudah dibaca

image_read("https://jeroen.github.io/images/frink.png") %>%
  image_rotate(270) %>%
  image_background("blue", flatten = TRUE) %>%
  image_border("red", "10x10") %>%
  image_annotate("The same thing with pipes", color = "white", size = 30)