Add initial handling of ICC color profiles

This commit is contained in:
byte[] 2021-08-01 23:19:20 -04:00
parent 7686f4c2d6
commit f26d138a05
2 changed files with 30 additions and 7 deletions

View file

@ -23,19 +23,38 @@ defmodule Philomena.Processors.Jpeg do
intensities intensities
end end
defp requires_lossy_transformation?(file) do
with {output, 0} <-
System.cmd("identify", ["-format", "%[orientation]\t%[profile:icc]", file]),
[orientation, profile] <- String.split(output, "\t") do
orientation != "Undefined" or profile != ""
else
_ ->
true
end
end
defp strip(file) do defp strip(file) do
stripped = Briefly.create!(extname: ".jpg") stripped = Briefly.create!(extname: ".jpg")
# ImageMagick always reencodes the image, resulting in quality loss, so # ImageMagick always reencodes the image, resulting in quality loss, so
# be more clever # be more clever
case System.cmd("identify", ["-format", "%[orientation]", file]) do case requires_lossy_transformation?(file) do
{"Undefined", 0} -> true ->
# Strip EXIF without touching orientation # Transcode: strip EXIF, embedded profile and reorient image
{_output, 0} = System.cmd("jpegtran", ["-copy", "none", "-outfile", stripped, file]) {_output, 0} =
System.cmd("convert", [
file,
"-profile",
srgb_profile(),
"-auto-orient",
"-strip",
stripped
])
{_output, 0} -> _ ->
# Strip EXIF and reorient image # Transmux only: Strip EXIF without touching orientation
{_output, 0} = System.cmd("convert", [file, "-auto-orient", "-strip", stripped]) {_output, 0} = System.cmd("jpegtran", ["-copy", "none", "-outfile", stripped, file])
end end
stripped stripped
@ -85,4 +104,8 @@ defmodule Philomena.Processors.Jpeg do
scaled scaled
end end
defp srgb_profile do
Path.join(File.cwd!(), "priv/icc/sRGB.icc")
end
end end

BIN
priv/icc/sRGB.icc Normal file

Binary file not shown.