Reputation: 11
I am trying to use the magick package in R to make a the same adjustment in RGB for each pixel in an image
I want to reduce the r value by 3, increase the g value by 2, and increase the b value by 27
#colour corrections
install.packages("magick")
library(magick)
setwd("/Users/hollymcdougall/desktop")
# Load the image
image <- image_read("R10I1.heic")
# Convert the image to the RGB color space
image <- image_convert(image, "RGB")
# Define the subtraction values for each channel
red_sub_value <- 3
green_sub_value <- -2
blue_sub_value <- -27
# Subtract the values from each channel
image[[1]] <- image[[c]] - red_sub_value
image[[2]] <- image[[2]] - green_sub_value
image[[3]] <- image[[3]] - blue_sub_value
# Clipping the values to stay within 0-255 range
image[image < 0] <- 0
image[image > 255] <- 255
# Save the output image
image_write(image, "output.jpg")
The step where the channels are being subtracted from does not work.
Upvotes: 1
Views: 237
Reputation: 173793
We don't have your image, so let's use this one:
library(magick)
image <- image_read("https://i.sstatic.net/A5Cn3.png")
We can convert it into an array of RGB values in the 0-255 range like this:
rgb <- as.numeric(image_data(image, "rgb")) * 255
Now, let's make the changes a bit more obvious for effect:
red_change <- 30
green_change <- -2
blue_change <- -50
We can apply the differences as follows:
rgb[,,1] <- rgb[,,1] + red_change
rgb[,,2] <- rgb[,,2] + green_change
rgb[,,3] <- rgb[,,3] + blue_change
rgb <- rgb / 255
# Clip to 0-1 range
rgb[rgb < 0] <- 0
rgb[rgb > 1] <- 1
Now we convert back to an image and save:
image2 <- image_read(rgb)
image_write(image2, "image2.png")
image2.png
Upvotes: 2