Pass a "hotspot" to Crop.crop_focus()
#91
sodapopcan
started this conversation in
Ideas
Replies: 2 comments
-
|
Andrew, very happy to accept a PR for this, it makes a lot of sense to me. And I want this library to be driven primarily by user requirement.If you can’t submit a PR I can work in this but it might take a week or so.Sent from my iPhoneOn 14 Jun 2023, at 04:11, Andrew Haust ***@***.***> wrote:
Hey Kip!
The Crop.crop_focus() option type lets us pass a one of a set of predetermined functions. At my work, we manually set a "hotspot" on each image for where we want the focus to be. Would you be interested in including this in Image? It's just a thought, I won't be offended if you won't 😅
I was thinking the API could be passing a "percentage point" to :crop.
Image.thumbnail(image, "500x400", crop: {0.5, 0.5})
This would focus on the center of the image. I know that crop_focus options are sent wholesale to Vix so I'm not sure you'd ok with that inconsistency, but it seems like a good place for Image as far as discoverability goes. It could also be its own function, of course.
This is how I'm currently doing it:
def size_to_fill_with_hotspot(image, {crop_width, crop_height}, {hotspot_x, hotspot_y}) do
image_width = Image.width(image)
image_height = Image.height(image)
ratio =
if image_width > image_height do
crop_height / image_height
else
crop_width / image_width
end
with {:ok, image} = Image.resize(image, ratio) do
image_width = Image.width(image)
image_height = Image.height(image)
# Get the bounding Box
point_x = image_width * hotspot_x - crop_width / 2
point_y = image_height * hotspot_y - crop_height / 2
# Check to see if the bounding box is going out of bounds
point_x = if point_x < 0, do: 0, else: point_x
point_y = if point_y < 0, do: 0, else: point_y
point_x =
if point_x + crop_width > image_width do
image_width - crop_width
else
point_x
end
point_y =
if point_y + crop_height > image_height do
image_height - crop_height
else
point_y
end
Image.crop(image, round(point_x), round(point_y), crop_width, crop_height)
end
end
I know I clean some parts up there and make use of thumbnail but this is actually old Ruby code ported to Elixir and I haven't bothered changing it yet.
Anyway, that is all!
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Great! I can do a PR, I was just posting code in case it triggered some immediate feedback. I can do this week. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey Kip!
The
Crop.crop_focus()option type lets us pass one of a set of predetermined functions. At my work, we manually set a "hotspot" on each image for where we want the focus to be. Would you be interested in including this inImage? It's just a thought, I won't be offended if you aren't 😅I was thinking the API could be passing a "percentage point" to
:crop.This would focus on the center of the image. I know that
crop_focusoptions are sent wholesale toVix's thumbnail operation so I'm not sure you'd ok with that inconsistency, but it seems like a good place forImageas far as discoverability goes. It could also be its own function, of course.This is how I'm currently doing it (written with some old coworkers):
Anyway, that is all!
Beta Was this translation helpful? Give feedback.
All reactions