Rで解析:画像からカラーパレットを作成「lterpalettefinder」パッケージ

Rの解析に役に立つ記事
スポンサーリンク

画像からカラーパレットを作成するパッケージの紹介です。PNG、JPEG、TIFF
、HEIC形式の画像からカラーパレットの作成が可能です。また、作成したカラーパレットをプロットで確認するコマンドも収録されています。

パッケージバージョンは1.0.0。windows11のR version 4.2.2で確認しています。

スポンサーリンク

パッケージのインストール

下記コマンドを実行してください。

#パッケージのインストール
install.packages("lterpalettefinder")

実行コマンド

詳細はコマンド、パッケージのヘルプを確認してください。

画像例:https://www.karada-good.net/wp/wp-content/uploads/2022/08/TEST.jpg

#パッケージの読み込み
library("lterpalettefinder")

###画像ファイルの準備#####
#画像例を作業フォルダにダウンロード
download.file('https://www.karada-good.net/wp/wp-content/uploads/2022/08/TEST.jpg',
              'KARADA.jpg', mode = 'wb') 
#画像例を指定
library("tcltk")
Path <- paste0(as.character(tkgetOpenFile(title = "&#30011;&#20687;&#12501;&#12449;&#12452;&#12523;&#12434;&#36984;&#25246;",
                                          filetypes = '{"&#30011;&#20687;&#12501;&#12449;&#12452;&#12523;" {".*"}}',
                                          initialfile = c("*.*"))))
########


#&#30011;&#20687;&#12363;&#12425;25&#33394;&#12398;&#12497;&#12524;&#12483;&#12488;&#12434;&#20316;&#25104;:palette_extract&#12467;&#12510;&#12531;&#12489;
#&#30011;&#20687;[PNG,JPEG,TIFF,HEIC]&#12434;&#25351;&#23450;:image&#12458;&#12503;&#12471;&#12519;&#12531;
#Hex&#12467;&#12540;&#12489;&#12398;&#33394;&#30456;,&#24425;&#24230;&#12391;&#20006;&#12403;&#26367;&#12360;:sort&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE/FALSE
#&#20966;&#29702;&#20013;&#12398;&#12503;&#12525;&#12464;&#12524;&#12473;&#12496;&#12540;&#12434;&#34920;&#31034;:progress_bar&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE/FALSE
ResultPalette <- palette_extract(image = Path,
                                 sort = TRUE,
                                 progress_bar = TRUE)
#&#32080;&#26524;:&#23455;&#34892;&#27598;&#12395;&#12497;&#12524;&#12483;&#12488;&#20869;&#23481;&#12364;&#33509;&#24178;&#30064;&#12394;&#12426;&#12414;&#12377;
ResultPalette
#[1] "#c04a4b" "#888787" "#dc5d5c" "#ee766e" "#7e6d50"
#[6] "#61594b" "#847c6d" "#ccb480" "#b8a16d" "#a28e5c"
#[11] "#a9a69d" "#ece3c6" "#dbd4bd" "#dfce8f" "#cbc6b2"
#[16] "#989793" "#b9b6a9" "#faf4d4" "#f1e79d" "#74767b"
#[21] "#66676a" "#4f5b9a" "#6f7bbe" "#555661" "#4a4a4f"

#&#20316;&#25104;&#12375;&#12383;&#12497;&#12524;&#12483;&#12488;&#12398;&#30906;&#35469;:palette_demo/palette_ggdemo&#12467;&#12510;&#12531;&#12489;
#&#22522;&#26412;&#12398;plot&#12467;&#12510;&#12531;&#12489;&#12434;&#21033;&#29992;&#12375;&#12390;&#30906;&#35469;
palette_demo(palette = ResultPalette, export = FALSE)

#ggplot2&#12497;&#12483;&#12465;&#12540;&#12472;&#12434;&#21033;&#29992;&#12375;&#12390;&#30906;&#35469;
palette_ggdemo(palette = ResultPalette)

#&#12459;&#12521;&#12540;&#12497;&#12524;&#12483;&#12488;&#12363;&#12425;&#25351;&#23450;&#25968;&#12398;&#33394;&#12434;&#12521;&#12531;&#12480;&#12512;&#12395;&#25277;&#20986;:palette_subsample&#12467;&#12510;&#12531;&#12489;
#&#25277;&#20986;&#25968;&#12398;&#35373;&#23450;:wanted&#12458;&#12503;&#12471;&#12519;&#12531;
#&#20877;&#29694;&#24615;&#25285;&#20445;&#12398;&#12383;&#12417;seed&#12434;&#25351;&#23450;:random_seed&#12458;&#12503;&#12471;&#12519;&#12531;
palette_subsample(palette = ResultPalette,
                  wanted = 5, random_seed = 42)
#[1] "#b9b6a9" "#7e6d50" "#c04a4b" "#a28e5c" "#ee766e"

###&#20363;&#12360;&#12400;&#12300;ggplot2&#12301;&#12497;&#12483;&#12465;&#12540;&#12472;&#12391;&#21033;&#29992;&#12377;&#12427;#####
#&#12300;tidyverse&#12301;&#12497;&#12483;&#12465;&#12540;&#12472;&#12364;&#12394;&#12369;&#12428;&#12400;&#12452;&#12531;&#12473;&#12488;&#12540;&#12523;
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}

#&#12503;&#12525;&#12483;&#12488;
ggplot(data = diamonds,
       aes(x = carat, y = price, col = color)) +
  geom_point() +
  scale_colour_manual(values = ResultPalette[1:7])
########

出力例

・基本のplotコマンドを利用して確認:「palette_demo」コマンド

・ggplot2パッケージを利用して確認:「palette_ggdemo」コマンド

・例えば「ggplot2」パッケージで利用する


少しでも、あなたの解析が楽になりますように!!

タイトルとURLをコピーしました