Image Size in Windows 8 File Picker - windows

How to know the size of the picked image , or how to put a limit for the picked image , like Maximum size for Image is 1 MB , Here is my code
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
openPicker.FileTypeFilter.Add(".png");
openPicker.FileTypeFilter.Add(".Jpeg");
openPicker.FileTypeFilter.Add(".Jpg");
StorageFile file = await openPicker.PickSingleFileAsync();

Unfortunately there is no way to restrict the size of the picked file in advance.
After the file has been picked, you can call http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefile.getbasicpropertiesasync.aspx to get its size.

Related

Use imwrite to save processed images to different folder in Matlab

I need help with saving processed images to different folder with imwrite.
Currently, I can save all processed images to a single folder.
Img_filename=ls('C:\Users\User\Desktop\Guanlin_CNN1D\CNN1D\GF_BSIF\*.jpg');
imageSize = size(Img_filename);
Img_filenum = size(Img_filename,1);
for img=1:Img_filenum
img_temp=double((rgb2gray(imread(Img_filename(img,:)))));
-----------processing--------
count = count+1;
FileName = fullfile('C:\Users\User\Desktop\Guanlin_CNN1D\CNN1D\GF_BSIF\folder_1',sprintf('%03d_circle_cropped.jpg',count));
imwrite(MM, FileName)
end
However, I have 1000 different images in 1 folder and after processing, it will generate 500 images and I want to save the first 500 processed images into folder_1. And the second 500 processed images to folder_2 and the third 500 images to folder_3 and so on...
How to re-write the imwrite function?
Thank you!
The root folder I used here is named Image_Folder and resides on the desktop. The output folders are named Folder_1, Folder_2 and Folder_3 and also reside on the desktop. I used two nested loops to control the saving of the images. The outer loop controls which folder to write to and the inner loop controls the writing images 1 through 500. The variable Image_File_Names can be used to access the input image file names.
%Folder holding the 1000 images%
Image_Path = "/Users/michael/Desktop/Image_Folder";
%Prefix path for output folders%
Export_Path = "/Users/michael/Desktop/Folder_";
%Adding path with input images%
addpath(Image_Path);
%Listing all images in folder directory%
Image_File_Names = ls(Image_Path);
Image_File_Names = split(Image_File_Names);
Number_Of_Images = length(Image_File_Names) - 1;
for Folder_Index = 1: 3
Export_Folder_Path = Export_Path + num2str(Folder_Index) + "/";
mkdir(Export_Folder_Path);
for Image_Index = 1: 500
%Grab the images as needed%
Input_Image_Index = 1; %Change this index to another variable to grab images within input folder%
Image = imread(string(Image_File_Names(Input_Image_Index)));
%***************************************%
%PROCESS IMAGE%
%***************************************%
%***************************************%
Output_File_Path = Export_Folder_Path + "Image_" + num2str(Image_Index) +".jpg";
imwrite(Image,Export_Folder_Path+num2str(Image_Index)+".jpg");
end
end
Using MATLAB version: R2019b

Rmagick compress when filesize is bigger than X without write (upload to S3)

I would like to compress an image when its size is bigger than an amount (i.e. 1000000 - 1MB). I see compress is used with write method in Image object. I have the image in memory and I don´t want to write it in my server but in an Amazon S3.
This code is working. It´s uploading the image to my s3 path, however I would like to include the filesize check and the compress:
photo = Magick::Image.read(mmkResourceImage.href).first
s3 = AWS::S3.new
bucket = s3.buckets[bucketName]
obj = bucket.objects[key]
obj.write(photo)
Please, let me know if there is another approach to accomplish this.
To get the current size you can use Magick::Image#to_blob which will return a binary string. You can get the string's size with String#bytesize. You can scale it down with Magick::Image#scale!.
Here is an example that would resize it by 75% until it was under a 1,000,000 bytes.
while photo.to_blob.bytesize > 1_000_000
photo.scale!(0.75)
end
Here is an example that would reduce the quality instead
(0..100).step(5).reverse_each do |quality|
blob = photo.to_blob { self.quality = quality }
if blob.bytesize <= 1_000_000
photo = Magick::Image.from_blob(blob).first
break
end
end

Image went from 11MB to 500MB in size after converted?

I have a jpeg image 10,000px by 10,000px in Format24bppArgb 11MB, but after I clone it and save it as bmp in Format32bppArgb, it goes up to 500MB, any reason why?
let file = new Bitmap("planet.jpg")
let rect = new Rectangle(0, 0, file.Width, file.Height)
let img = file.Clone(rect, PixelFormat.Format32bppArgb)
img.Save("copy.bmp", ImageFormat.Bmp)
I went ahead an ran the following in FSI:
open System.Drawing
open System.Drawing.Imaging
let file = new Bitmap("planet.jpg")
let rect = new Rectangle(0, 0, file.Width, file.Height)
let img = file.Clone(rect, PixelFormat.Format32bppArgb)
img.Save("copy.bmp", ImageFormat.Bmp)
I used a blank 10,000px by 10,000px JPG file clocking in at 1,563,127 bytes according to Windows Explorer. The resulting copy.bmp is 400,000,054 bytes, which is indeed a lot bigger than the input.
As #PaulAbbott said, this is because there's no compression in bitmap files. A 10,000 by 10,000 image at 32 bits per pixel will end up being stored as 3,200,000,000 bits, which is exactly 400,000,000 bytes or roughly 400 MB - which is around the size you posted.
If the file you got really was 500 MB and not around 400 MB, could you try hosting it somewhere? I'd like to take a look at it and see if I'm missing something here.

Base64String in Windows 8

I've got a windows 8 program that uses an image picker and downloads the selected image on the server.
The server provides an API which needs the image to be converted in base64string. And an image must be less than 7Mb.
I'm using the code below:
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
bitmap = new BitmapImage();
byte[] buf;
using (var stream = await file.OpenStreamForReadAsync())
{
buf = ReadToEnd(stream);
}
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
base64String = Convert.ToBase64String(buf);
bitmap.SetSource(stream);
}
}
And the bitmap goes to the server.
But there is a problem: the bitmap size is much more bigger than jpg's size, for example. And none of small jpgs go to the server, because their bitmap version is larger than 7 Mb.
Can I convert an image to base64string without converting it to a bitmap?
In this code, you read the image (encoded in jpeg) and convert it to a base 64 string.
You can not reduce the size of the base 64 without reducing the size of the image.
To do so, you can use a BitmapEncoder/Decoder and resize the image to a smaller size.
Regards

How to resize the image to be uploaded in JSP to a fix resolution?

I am uploading images for my site , I want every image uploaded to the server be of constant size.
So, when I display them from server to my site they can be uploaded quickly and the images use less server space.
code I am using to upload image using JSP.
logo_name = System.currentTimeMillis() + ".png";
File uploadedFile = new File("/www/static.appcanvas.com/"+logo_name);
item.write(uploadedFile);
Any related articles , some hints will be of great help
You didn't show us how you are parsing the upload. But, if "item is a org.apache.commons.fileupload.FileItem then you could use something lik the following.
BufferedImage bi = ImageIO.read(item.getInputStream());
Image img = bi.getScaledInstance(100,100,Image.SCALE_SMOOTH);
int w = img.getWidth();
int h = img.getHeight();
BufferedImage scaled = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
Graphics2D g = scaled.createGraphics();
g.drawImage(img,0,0,null);
if(g != null) g.dispose();
ImageIO.write(scaled,"png", uploadedFile);

Resources