using get_serving_url() to resize SMALLER dimension (width or height) to a specific integer - image

I'm trying to use get_serving_url() to serve up thumbnails, but I need those those thumbnails' smaller dimension (could be width OR height) to be a certain number so that there won't be any gaps in its container div. According to the docs, the "size" argument for get_serving_url will adjust the LONGEST dimension to a given parameter. This might cause the opposite dimension to go below my required size. How can I fix this?

If you have the dimensions of the bitmap you can request a size that is larger than your smallest value by the factor of the image aspect ratio.
For example if the image is 1600x1200 and you want the thumbnail to be at least 32 pixels in in dimension, the size should be 43 (32*(1600/1200)) and the resulting image will be 43x32.
In order to get the image size you need to load the image date into the image class and use the width and height properties.

There's no way to do this currently; you will have to fetch the dimensions of the image and calculate the size of the longer size accordingly. You probably should do this anyway, or a 1x1000 image will break things for everyone.

Related

Specifying a pitch for SetDIBitsToDevice

I am displaying bitmaps with the function SetDIBitsToDevice. This function knows about the total image size via a LPBITMAPINFO structure that has Width and Height fields. It also knows about the region of interest to be drawn via the arguments XDest, YDest, Width, Height. All these are specified in pixels.
So far so good when the image is stored as a canonical one, i.e. with a row pitch (number of bytes between a pixel and the one immediately below) that matches the image width in bytes, with padding (if necessary) to reach the next multiple of four bytes.
For technical reasons, I have images with a larger pitch (but still a multiple of four). For instance, width=1000 but pitch=1024. For a grayscale image (1 byte per pixel), I can trick the function by declaring a width of 1024 in LPBITMAPINFO and a width of 1000 when passed to SetDIBitsToDevice.
But for a 3 bytes per pixels image (RGB), I am stuck because 1024 bytes do not correspond to an integer number of pixels, and I see no way to specify that pitch.
Do you see a workaround or something I missed in the documentation ? (I don't think that the field SizeImage can be of any use.)

Obtaining the ratio difference of two images

I have an image of the size 640*640*3, while another image of the size 125*314*3. I want to obtain the size ratio of the second image to the first image, but I can't find a way to do it.
I have tried the traditional divide method, as well as using rdivide but both are not working.
If I use the traditional approach of multiplying the image 3D values first, then compare, will the approach be correct?
For example, I would do something like 640*640*3 = 1,228,800 then 125*314*3 = 117,750 and finally, take 117,750 / 1,228,800 = 0.09. Is 0.09 the right answer?
I'm assuming you are referring to the ratio of the areas between the two images. If this is the case, just use the width and height. This looks like you are using RGB images, so don't use the number of channels. However, the number of channels cancels out when you use them in finding the ratio.
Therefore, yes your approach is correct:
(125*314) / (640*640) = 0.0958
This means that the smaller (or second) image occupies roughly 9.5% of the larger (or first) image.
That depends what you mean by size ratio.
Looks like you have RGB images, so if you mean the area, then it is (640*640)/(125*314), if you mean the height, then it is 640/314, more options too, be more specific in your question.

is this possible to calculate the resize image size within actually resize it?

Let say, I can read an image, and resize it to the size I want. But I would like to do something tricky. I got a image size 5MB, but I would like to resize it in not larger than 512KB. is this possible to calculate how much do I need to resize? Or it can just simply calculated by org size diverted by prefer size? thanks.
*You can use whatever language you want to implement it.
If there is no compression then it is simple as you mention; otherwise it depends on the subject...
This algorithm would work:
Calculate the scale factor to reduce the current image pixel dimensions to half size
Scale the image to half size
Calculate the file size. If file size too big GOTO step 1 otherwise the scale factor you calculated in step one is your answer.

How to resize the image width and height with same quality in Corona SDK?

I want to re-size the image for example say 1000 * 1000 to 100 * 100. And I want to display full image after resizing it. Please can anyone help me?
To scale display objects in Corona either use the scale() command or manipulate the xScale and yScale properties:
http://developer.anscamobile.com/node/2452
Note however that the scaling will look best if the image sticks to power of two divisions. That is, 1/2 size, 1/4 size, etc.
For fast animations (eg. the object transitions away over half a second) the exact scaling you describe would look fine, but otherwise I'm wondering why you would want to do that in Corona (as opposed to scaling the image in Photoshop and simply having a smaller image to begin with.)
It'd be best to create an image of a larger size than you'd like to display. This will preserve the quality. For example you have an image with dimensions of 100x100 and you'd like it to display as a 20x20 image. Do the following:
local IMAGE = display.newImageRect("IMAGE.png", 20, 20)

Enforcing a particular width on a scale?

We have the customer requirement that for a particular scale of an image ('teaser' scale) the width has to the always 160px independent of the image ratio. Specifying as (160,160)
scale does not work for images where the height is larger than the width. In this case 160px will be used.
Any idea how to ensure a fixed with of 160px in every case?
I used this scales in a site and it worked OK:
image_scales = {'thumb': (150,600), 'mini': (200, 800)}
The idea is to have a very long height so no matter the image ratio of the picture, it will always be 150 or 200 px width.
In Plone 4 there is an entirely new way of generating scales which can help with this problem. Using this approach you can tell it to scale the image 'down' instead of 'up', which means that it will scale the short side of the image to the specified size, rather than the long side (so the image ends up getting cropped, but always fills the specified area).
With this approach, you don't have to define the scales in your schema, but can simply include something like the following in your template. The scale will be generated on demand.
<img tal:define="scale context/##images"
tal:replace="structure python: scale.scale('image',
width=160, height=160, direction='down').tag()" />
See the plone.app.imaging page for more examples of this approach to scaling.

Resources