codeigniter png resize not working - codeigniter

copy($image, $target);
// resize to 'md' size
$resize_config['source_image'] = $target;
$resize_config['new_image'] = $target;
$resize_config['width'] = $dimensions['width'];
$resize_config['height'] = $dimensions['height'];
$resize_config['master_dim'] = 'width';
$ci->image_lib->initialize($resize_config);
$ci->image_lib->resize();
It works for jpg images but not png images. For a png image it is doing the copy to the target directory then stopping when it comes to the resize
The original size is Width = 620px Height = 413px
The array for setting the new size is array ('width' => 685, 'height' => 526)

Related

Saved an image sequence as a Tiff file but cannot view or open it outside of Matlab

Im trying to save an image sequence as a Tiff file to be later used in another program. I read each image in, applied color thresholding to get the mask, and then append that to my 3d array that I initialized earlier.
I read up on the Tiff class needed to write Tiff images and I think I set all the tags properly, but although the file gets created, I cannot open it in an image viewer. I get the error:
Invalid or Unsupported Tif file
There are 290 images in folder, so tiff file will have 290 planes. Below is how I am saving the tiff file:
clear;
path = 'D:\complete stack';
img_list = dir(fullfile(path, '*.tif'));
img = imread(fullfile(img_list(1).folder, img_list(1).name));
tiff_array = zeros(size(img, 1), size(img, 2), numel(img_list), 'uint8');
clear img;
for i = 1:numel(img_list)
img = imread(fullfile(img_list(i).folder, img_list(i).name));
[bw, ~] = createMask(img);
tiff_array(:,:,i) = bw;
end
t = Tiff('myfile.tif', 'w');
setTag(t,'Photometric',Tiff.Photometric.Mask);
setTag(t, 'Compression', Tiff.Compression.None);
setTag(t,'BitsPerSample',8);
setTag(t,'SamplesPerPixel',size(tiff_array, 3));
setTag(t,'ImageLength',size(tiff_array, 1));
setTag(t,'ImageWidth',size(tiff_array, 2));
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
write(t, tiff_array);
close(t);
thanks
edit:
A more reproducible, more minimal example:
clear;
img = ones(750, 750, 'uint8');
tiff_array = zeros(size(img, 1), size(img, 2), 290, 'uint8');
clear img;
for i = 1:290
bw = ones(750, 750, 'uint8');
% [bw, ~] = createMask(img);
tiff_array(:,:,i) = bw;
end
t = Tiff('myfile.tif', 'w');
setTag(t,'Photometric',Tiff.Photometric.Mask);
setTag(t, 'Compression', Tiff.Compression.None);
setTag(t,'BitsPerSample',8);
setTag(t,'SamplesPerPixel',size(tiff_array, 3));
setTag(t,'ImageLength',size(tiff_array, 1));
setTag(t,'ImageWidth',size(tiff_array, 2));
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
write(t, tiff_array);
close(t);

Do I need all these statements to get the data array of an image in HTML5 canvas?

I am loading a pixel font file as a png image. I then use it to draw each character to canvas by writing to the buffer imageData.data clamped array and then using putImageData.
Is there a simpler way to get the data array from the png image apart from loading the image as fontImage and then these 7 lines ...
let fontCanvas = document.createElement("canvas");
fontCanvas.width = fontImage.width;
fontCanvas.height = fontImage.height;
let fontContext = fontCanvas.getContext("2d");
fontContext.drawImage(fontImage, 0, 0);
let fontBank = fontContext.getImageData(0,0,fontCanvas.width,fontCanvas.height);
let fontData = fontBank.data;
Thanks.

how to resize an image to a fit within max width and max height?

I have a CI image upload form, and I want to make sure the images I upload will fit in a given rectangle. If they are too big - I want to downsize them. If they fit within that rectangle - I don't need resizing.
I know CI supports max-height and max-width (as a limitation on how big the uploaded images can be), and it supports resize, but I couldn't find how to resize the image to a set max-height and max-width (while maintining the ratio).
Well, it was easy enough, as expected from CI (-:
When using $this->image_lib->resize(), you set the desired width & height. If you also set maintain_ratio to true, the new image will be resized to the closest possible values of your set width & height, while preserving the original aspect ratio.
So this is the code I used, after uploading the image:
$file_data = $this->upload->data();
$max_height = 115;
$max_width = 225;
if ($file_data['image_width']>$max_width || $file_data['image_height']>$max_height)
{
$configResize = array(
'source_image' => $file_data['full_path'],
'width' => $max_width,
'height' => $max_height,
'maintain_ratio' => TRUE
);
$this->load->library('image_lib',$configResize);
$this->image_lib->resize())
}
You need to calculate the height and width you would like the resized image to be. For example:
Suppose your input image is 1200x1000 and you want your resized image to be 300x200.
Find the size ratios 1200/1000=1.2 and 300/200=1.5.
If the original ratio is larger than the targeted resized size ratio you want to match the width, else you want to math the height. In this case we want to match the height.
Find the scale factor to change the image to the target size. Scale factor is 200/1000=0.2.
Use the scale factor to find the new size 1200x1000 scaled by 0.2 (1200*0.2 and 1000*0.2) = 240x200.
Resize the image to 240x200 as it is your best fit for the 300x200 box.
Take from the website provided by Silviu G http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = 'originalImage.jpg';
$config['create_thumb'] = TRUE;
$config['width'] = 240;
$config['height'] = 200;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Perhaps a better way to handle this is through CSS (in conjunction with the CI image manipulation library for resizing):
.media img{
max-width:100px;
max-height:100px;
}
Use the Image Manipulation Class form CI

opencv: change image size without effecting content size

I am getting image containing character after applying my image process. All these images (having character as content) have different size. I want to resize all images in same size without effecting it's content size. see below image:
http://techsture.com/img.jpg
I have tried some function to merge my source image with other white image but it needs both source images with same size. I need common size of images for further process.
Please guide me how can I convert my images to common size ??
Thank you
I would make a new image with the destination size, set a ROI in this new image to where you want the source image to show. Then use cvResize.
It should look something like:
int newWidth = 100;
int newHeight = 100;
CvRect rect;
IplImage* source = cvLoadImage("c:/myimage");
IplImage* dest = cvCreateImage(cvSize(newWidth,newHeight),source->depth,source->nChannels);
rect.x = newWidth/2 - source->width/2;
rect.y = newHeight/2 - source->height/2;
rect.width = source->width;
rect.height = source->height;
cvSetImageROI(dest,rect);
cvResize(source,dest);
cvResetImageROI(dest);

Corona - how to make sprite sheets compatible with dynamic image resolution

Corona has a method for creating images that will be displayed dynamically based on device resolution:
img = display.newImageRect("image.png", 100, 100)
Great, but what if all your images are in a sprite sheet, which is recommended for performance? Then you have to do something like this to display the image:
local data = require("sheet1")
local tileSheet = sprite.newSpriteSheetFromData("sheet1.png", data.getSpriteSheetData())
local tileSet = sprite.newSpriteSet(tileSheet, 1, 3)
local img = sprite.newSprite(tileSet)
img.currentFrame = 1
How do you create dynamically sized images from sprite sheets?
use display.contentScaleX http://developer.anscamobile.com/reference/index/displaycontentscalex
here's how http://developer.anscamobile.com/forum/2010/12/08/dynamic-retina-spritesheets-heres-how
Here's how I resized my background animation. It consisted of 2 frames, each 794 x 446. It needed to be full-screen, landscape mode. Refer to step 6 below.
--> Initialize background animations
-- 1. Data = define size and frames
local bgData = { width=794, height=446, numFrames=2, sheetContentWidth=1588, sheetContentHeight=446 }
-- 2. Sheet = define the sprite sheet
local bgSheet = graphics.newImageSheet( "hkc.png", bgData )
-- 3. Animation = define animation characteristics
local bgAnim = {
{ name = "bgAnimation", start = 1, count = 2, time = 800, loopCount = 0, -- repeat forever
loopDirection = "forward"
}
}
-- 4. Display the sprite (can't set size here unlike display.newImageRect)
local bg = display.newSprite( bgSheet, bgAnim )
-- 5. Center the animation
bg:setReferencePoint( display.CenterReferencePoint )
bg.x = display.contentCenterX
bg.y = display.contentCenterY
-- 6. Resize to match screen size based on a ratio with the actual background pixel dimensions
bg:scale( display.contentWidth / 794, display.contentHeight / 446 )
-- 7. Play the animation
bg:play()

Resources