Ruby: how to crop and resize image from center - ruby

I am trying to resize an image and crop it from the center. i.e. if the resolution is 1024x512 and I want to resize by 256x256 I would resize the smallest side to 256 and maintain aspect ratio, i.e. resize the image to 512x256 and then I want to crop the 256x256 square from the centre.
I have seen solutions with paperclip but only for Rails, but I'm not sure how to do this in just a regular script.
Any ideas?
Thanks

You can use,
Magick::ImageList.crop!(CenterGravity,256,256)
More options

Related

Corrupted resized textures with Metal on Retina screens

I want to draw a series of textures into METAL view in order to present a complete image. On a regular screen, the images are presented on exactly 1:1 scale (Meaning a 100x100 pixels texture will be presented in a 100x100 pixels square)
Drawing it on a retina display, will actually give me a 200x200 square.
Now, there may be 2 different approaches:
1) Generate entire image into 100x100 square and let Metal View to upscale it to 200x200 square - It works.
2) Upscale each texture and generate image directly into 200x200 square. Why to take this approach? Because some of the textures (Like texts) are generated dynamically, and can be generated in a better resolution. Something impossible if you take the first approach.
Unfortunately, in this approach, some ugly square is visible around each texture.
I tried to play with sizes, clamp options etc, yet I could not find any solution.
Any help would be highly appreciated!
Image from regular screen
Image from retina screen
Found a solution. In Fragment shader, texture sampler was defined as:
constexpr sampler s = sampler(coord::normalized, address::repeat, filter::nearest);
instead of:
constexpr sampler s = sampler(coord::normalized, address::clamp_to_edge, filter::nearest);

How to resize the image pixels in xamarin forms

My actual Image is
Here I need to resize the image 140*80(Width*height)
But after resize the circle in the image becomes oval how to prevent that.
Your current image resolution is 700*312 has a ratio of 1:2.2435897435897435897435897435897..
The resolution you are trying to get is 140*80 has a ratio of 1:1.75.
In order to get a perfect circle, you must maintain the same ratio between the images.
If acceptable, try resizing your image to 140*62.4 (62.4 = 140/(700/312)).
More info about Aspect Ratio on Wikipedia .
Specifically for Xamarin, please show us what components are you using, or some example code so answers could be more specific.

How to keep sprite border the same size when scaling sprite in Unity?

I've created an image in Photoshop to be used as a sprite in Unity and everything works fine while the sprite is scaled at X: 1; Y: 1.
The problem starts when I scale the image up as the border of the image stretches out with the rest of the image. Is there any way to scale an image from its centre or to ignore the image's border when it's scaled?
Here's the example now that I am able to show it:
The rectangle on top is the original image without being scaled up or down and the rectangle on the bottom is scaled at X:5, Y:0.5 but the borders are stretched.
I think that the borders are stretched because it's part of the image and when it's being scaled, the image (including the borders) is just being stretched.
Is there any way to stretch the sprite image but by ignoring the borders?
Are you trying to scale the image and keep the original ratio?
If so, here are the steps:
Hope this helps. Please let me know if you were trying to do something else.
You can use a sliced sprite. The center of the image is scaled to fit the control rectangle but the borders maintain their sizes regardless of the scaling. Check out the Unity doc here: Unity - Manual: Image

Dragonfly/imagemagick resize and crop and maybe add white padding

With dragonfly I can do the following:
image.thumb('300x300#')
Which will resize the image, maintaining aspect ratio and cropping it centrally (effectively chopping off the ends of the longest edge).
However, if the image has an edge smaller than 300px, then it is scaled upwards. What I would prefer is that in this case the image is not resized, but instead white padding is added where necessary.
So basically, if both edges are 300px or over I want the normal behaviour from 300x300#, but if any edge is smaller than 300px, then the image is not resized at all, but still cropped to 300x300 with whitespace added where necessary.
Is this possible with either of Dragonfly's built in processors (#thumb or #convert? Or do I need to build my own processor? If so, what sort of imagemagick commands do I need to be looking at?
Best solution would be to create a white canvas image that is 300x300 then composite your image, centered, on top of the canvas image. Then crop it with center gravity (centered). This would yield a 300x300 image with a white canvas on any vertical or horizontal edges that had a dimension smaller than 300.
** For this solution you may need to install the RMagick gem, as I do not believe Dragonfly has extended the ImageMagick operations that you will need.
This is how I would approach it:
#Set file path first and load a white image called canvas that is 300x300 into Imagmagik
canvas_file_path = "#{Rails.root}/app/assets/images/canvas.png"
canvas_image = Magick::Image.read(canvas_file_path).first
#Then perform the compositing operation. This overlays your image on top of the canvas, center gravity ensures that your image is centered on the canvas.
canvas_image.composite!(<YOUR IMAGE>, CenterGravity, Magick::OverCompositeOp)
#then write the file to a temporary file path so you can do something with it
temporary_file_path = "#{Rails.root}/tmp/#{#model.id}"
canvas_image.write(temporary_file_path)
Be sure to add the require statement in your file, pay close attention to the capitalization, it is RMagick not Rmagick
require "RMagick"
For reference here is the ImageMagick example from the documentation to perform the compositing operation that you will need
composite -gravity center smile.gif rose: rose-over.png
Rmagick Documentation on how to composite images - http://www.imagemagick.org/RMagick/doc/image1.html#composite
Rmagick Gem - https://github.com/rmagick/rmagick
ImageMagick reference to compositing - http://www.imagemagick.org/script/composite.php
I have a habit of answering my own questions of SO...
The following can be done with Dragonfly:
def thumb_url
if image.width < 300 || image.height < 300
image.convert('-background white -gravity center -extent 300x300').url
else
image.thumb('300x300#').url
end
end

Cropping an image with a focus area (face) using ImageMagick

I'm struggling to find the right approach to resize and crop and image, with a focus area. In my case the focus area is a face detected in the image, and I need to make sure that this area is visible in the cropped version.
I have focus area given by eg. face_height, face_width, face_center_x and face_center_y. These values are percentages of dimensions of the original image.
What I want to do, is getting a eg. 60x60 thumbnail. The normal approach would be to resize so either height or width of the image is equal 60px and then crop a 60x60 from center, like this:
mogrify -resize 60x -gravity 'Center' -crop 60x60 image.jpg
What approach can be taken focus my crop around a given area instead?
I'm thinking of a solution that includes several paths:
If the face area is bigger than the wanted thumbnail, resize the image just enough to make the whole face visible in 60x60 pixels, then crop
If the face area is smaller than the wanted thumbnail, then crop "expand" my face area until my wanted thumb can fit inside the area. Then crop. I guess I need to make sure that this doesn't exceed the bounds of the original image.
Is there a smarter approach? Can you try make some example code?
Thanks!
I'd first do the arithmetic in script or program, then feed exact coordinates to ImageMagick.
The arithmetic steps:
It'll be easier to operate with exact pixel values than percentages, so convert face_height, face_width, face_center_x and face_center_y to pixel values.
You'll want rectangular thumbnail, so pick the longest side and operate with that:
longest_side = max(face_height, face_width)
Now you can calculate top left point for your crop:
crop_x = face_center_x - longest_side / 2
crop_y = face_center_y - longest_side / 2
If any of the four crop corners fall outside your picture dimensions, adjust for that:
crop_x and crop_y should both be >= 0
crop_x + longest_side should be less than image width
crop_y + longest_side should be less than image height
Having calculated these, ImageMagick call gets quite straightforward:
mogrify -crop {longest_side}x{longest_side}+{crop_x}+{crop_y} -resize 60x60 image.jpg

Resources