It's question more about "know-how". I have a bunch of images of completely different sizes: one could be 360x360 and another 1200x800. And I would to make thumbnails for a webpage of exact size, for example 150x150. Because of different sizes I can't just use convert -resize or just crop it some way, I need both.
How would you solve this?
This question is quite old, but a current answer would be to use resize:
convert "input_filename" -resize '400x400!' "output_filename"
Note the exclamation point "!", which at least some shells require you to quote or escape to avoid history expansion.
Note that in requesting exact size, you may be asking for distortion.
You need a function that will calculate what the thumbnail sizes should be based on the source image width and height, and the max thumbnail width and height:
function setWidthHeight($srcWidth, $srcHeight, $maxWidth, $maxHeight){
$ret = array($srcWidth, $srcHeight);
$ratio = $srcWidth / $srcHeight;
if($srcWidth > $maxWidth || $srcHeight > $maxHeight){
$ret[0] = $maxWidth;
$ret[1] = $ret[0] / $ratio;
if($ret[1] > $maxHeight){
$ret[1] = $maxHeight;
$ret[0] = $maxHeight * $ratio;
}
}
$ret[0] = intval(ceil($ret[0]));
$ret[1] = intval(ceil($ret[1]));
return $ret;
}
You can then use whichever thumbnail image generating procedure you like imagecopyresampled(...) or the $imageMagick->thumbnailImage($newWidth, $newHeight);
Related
I am using image magick for .net to cropping and resizing the images. But the problem with the library is that it only crop the bottom of the image. Isn't there any way by means which we can crop it evenly from both up and down or left and right?
Edited question :
MagickGeometry size = new MagickGeometry(width, height);
size.IgnoreAspectRatio = maintainAspectRatio;
imgStream.Crop(size);
Crop will always use the specified width and height in Magick.NET/ImageMagick so there is no need to set size.IgnoreAspectRatio. If you want to cut out a specific area in the center of your image you should use another overload of Crop that also has a Gravity as an argument:
imgStream.Crop(width, height, Gravity.Center);
If the size variable is an instance of MagickGeometry, than there should be an X & Y offset property. I'm not familiar with .net, but I would imagine it would be something like...
MagickGeometry size = new MagickGeometry(width, height);
size.IgnoreAspectRatio = maintainAspectRatio;
// Adjust geometry offset to center of image (same as `-gravity Center`)
size.Y = imgStream.Height / 2 - height / 2;
size.X = imgStream.Width / 2 - width / 2;
imgStream.Crop(size);
I'm try to create application where I'm use a UIScrolView and I can draw different jpeg images on the appropriate point for each of this images. It is must looks like a video, for example:
I have a first jpeg image (background), and this image (for example image has coordinates: x = 0, y = 0, and dimentions: width = 1024, height = 768), I send to UIScrolView for showing. Then I get enother jpeg image with their own coordinatates and dimentions(for example x = 10, y = 10, width = 100, height = 100) and I need show this "smal" image over "background".
How I can di that?
Or another way of this issue. Example:
I have a BMP data of first image(bacground). How I can send this BMP data to the UIScrolView for showing?\
P.S. One more question: Which of presented above ways will run faster for processing and presentation of data?
WBR
Maxim Tartachnik
I'm using imagemagick to resize uploaded files but while they're processing I want to show a rotating gif wheel to the user where the thumbnail would normally be. I serve about 7 sizes of thumbs and would like the wheel to remain at it's 32x32 size in the middle, that's the simple bit.
What I need to know is, can I do the above while still retaining the animation
Example:
This Image:
Starting at this size
With Animation
Check out this fiddle, it might contain what you want: http://jsfiddle.net/TGdFB/1/
It uses jQuery, but should be easily adaptable...
Ended up doing this manually by Photoshop after not being able to find an automated way of doing this through imagemagick. I found the 'coalesce' flag but not much else.
there is a solution in php witch i use to watermark gif animated images ....
it create an black bacground put an image on it and then put watermark ...
watermarkpath = 'path to wathermarkimage.jpg|gif|png';
$imagepath= 'path to the image';
$watermark = new Imagick($watermarkpath);
$GIF = new Imagick();
$GIF->setFormat("gif");
$animation = new Imagick($imagepath);
foreach ($animation as $frame) {
$iWidth = $frame->getImageWidth();
$iHeight = $frame->getImageHeight();
$wWidth = $watermark->getImageWidth();
$wHeight = $watermark->getImageHeight();
if ($iHeight < $wHeight || $iWidth < $wWidth) {
// resize the watermark
$watermark->scaleImage($iWidth, $iHeight);
// get new size
$wWidth = $watermark->getImageWidth();
$wHeight = $watermark->getImageHeight();
}
$bgframe = new Imagick();
$bgframe->newImage(($iWidth), ($iHeight + 80), new ImagickPixel('Black'));
$bgframe->setImageDelay($frame->getImageDelay());
$x = ($iWidth) - $wWidth - 5;
$y = ($iHeight + 80) - $wHeight - 5;
$bgframe->compositeImage($frame, imagick::COMPOSITE_DEFAULT, 0, 0);
$bgframe->flattenImages();
$bgframe->compositeImage($watermark, imagick::COMPOSITE_OVER, $x, $y);
$bgframe->flattenImages();
$GIF->addImage($bgframe);
}
$GIF->writeimages($imagepath,true);
in a school project i would like to do the following step to have a watermaked image in matlab
extract the edges from an image
insert a mark on this edge
reconstruct the image
extract the mark
could some one give me a link to have a good idea how to do it or help me to do that?
thank you in advance
You want to add a watermark to an image? Why not just overlay the whole thing.
if you have an image
img = imread('myimage.jpg')
wm = imread('watermark.jpg')
You can just resize the watermark to the size of the image
wm_rs = imresize(wm, [size(img,1) size(img,2)], 'lanczos2');
img_wm(wm_rs ~= 0) = wm_rs; %This sets non-black pixels to be the watermark. (You'll have to slightly modify this for color images)
If you want to put it on the edges of the image, you can extract them like this
edges = edge(rgb2gray(img),'canny')
Then you can set the pixels where the edges exist to be watermark pixels
img_wm = img;
img_wm(edges ~= 0) = wm_rs(edges~=0);
Instead of direct assignment you can play around with using a mix of the img and wm_rs pixel values if you want transparency.
You'll probably have to adjust some of what I said to color images, but most should be the same.
Here, is a nice and simple example how you can embed watermarks using MATLAB (in the spatial domain): http://imageprocessingblog.com/digital-watermarking/
see example below(R2017b or later release):
% your params
img = imread('printedtext.png');
Transparency = 0.6;
fontColor = [1,1,1]; % RGB,range [0,1]
position = [700,200];
%% add watermark
mask = zeros(size(img),'like',img);
outimg = insertText(mask,position,'china', ...
'BoxOpacity',0,...
'FontSize',200,...
'TextColor', 'white');
bwMask = imbinarize(rgb2gray(outimg));
finalImg = labeloverlay(img,bwMask,...
'Transparency',Transparency,...
'Colormap',fontColor);
imshow(finalImg)
I'm facing the following problem : I have several UIImage (not squared) and I need to resize and crop them. I have read almost every question on StackOverflow but the results that I get are not good, I mean the image produced has a poor quality(blurry).
This is the scenario :
1) Original images size : width 208 pixel - height variable (i.e. from 50 to 2500)
2) Result images : width 100 pixel - height max 200 pixel
That is what I've done so far to achieve this result :
..... // missing code
CGFloat height = (100*image.size.height)/image.size.width;
self.thumbnail=[image resizedImage:CGSizeMake(100,height)
interpolationQuality:kCGInterpolationHigh];
..... // missing code
The method that I use to resize the image can be found here , once the image is resized I crop it using the following code :
CGRect croppedRect;
croppedRect = CGRectMake(0, 0, self.thumbnail.size.width, 200);
CGImageRef tmp = CGImageCreateWithImageInRect([self.thumbnail CGImage],
croppedRect);
self.thumbnail = [UIImage imageWithCGImage:tmp];
CGImageRelease(tmp);
Long story short, the image is resized and cropped but the quality is really poor considering that the original image had a really good quality.
So the question is how to achieve this keeping an high quality of the image?
If you target iOS 4 and later you should use ImageIO to resize images.
http://www.cocoabyss.com/coding-practice/uiimage-scaling-using-imageio/