I have an image, and I am storing it in the core data (its a very small image, and I'm using external storage).
Code to convert to data is:
imageData = NSData(data: UIImageJPEGRepresentation(profilePic.backgroundImage(for: UIControlState.normal)!, 0.5)!)
then for debugging purposes I am trying to convert straight back into an image:
let image = imageData
let secondimage = UIImage(data: image as Data, scale: 0.5)
profilePic.setBackgroundImage(secondimage, for: UIControlState.normal)
However, when it converts back and displays the results of the conversion, the image is gone and it displays nothing, but gives no error.
Where am I going wrong to convert back to the image?
Related
I am using below method to convert base64 to blob:
var blobData = new Blob([base64Str], {type: 'image/png'});
Now once the image file is created using this Blob:
var imageData = new FormData();
var counter = 1;
imageData.append("file", blobData, 'file1.png');
I do not see the metadata. Is there a way to add the metadata being stripped off the image file?
I download the image file created above and view it in http://metapicz.com/#landing
When viewing original image it shows the location info and other metadata, but when viewing the downloaded image it does not show any metadata.
Figured it out myself, the datatype returned from iOS native (swift) is UIImage which removed the EXIF information and only returns base64 of image data.
Changed the code to return entire image data + metadata at iOS native side.
Very weird behavior, but I have narrowed the problem down as far as I can go I think
I have a NSImage, let's call it inputImage. It is represented by a NSBitmapImageRep in a CGColorSpaceCreateDeviceGray, if that matters
I want to create a CGImageRef from it, but with inverted colors.
NSData *data = [inputImage TIFFRepresentation];
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
NSBitmapImageRep *maskedRep = (NSBitmapImageRep*)[inputImage representations][0];
CGFloat decode[] = {0.0, 1.0};
maskRef = CGImageMaskCreate(maskedRep.pixelsWide,
maskedRep.pixelsHigh,
8,
maskedRep.bitsPerPixel,
maskedRep.bytesPerRow,
provider,
decode,
false);
NSImage *testImage = [[NSImage alloc] initWithCGImage:maskRef size:NSMakeSize(1280,1185)]; //the size of the image, hard-coded for testing
NSLog(#"testimage: %#", testImage);
The problem is when I look at testImage, all of the pixels are slightly offset to the right from the original image.
inputImage:
testImage:
It's much easier to see if you save the pictures off, but you'll notice that everything in testImage is offset to the right by about 5 pixels or so. You'll see a white gap to the left of the black content in testImage
Somewhere in my 5 lines of code I am somehow moving my image over. Does anybody have any idea how this could be happening? I'm currently suspecting TIFFRepresentation
The data provider you pass to CGImageMaskCreate() is supposed to be raw pixels in the form specified by the other parameters, not an image file format. You shouldn't be passing TIFF data. Frankly, I'm surprised you got anything remotely resembling your original image, rather than noise/garbage. I'm guessing the TIFF data wasn't compressed, by sheer chance. You can actually see a bit of noise, which is the TIFF header interpreted as pixels, at the upper-left of your mask.
Probably, your best bet is to create a CGImage from your inputImage (or, depending on how inputImage was created, skip NSImage and create the CGImage directly from a file using the CGImageSource API or CGImageCreateWith{JPEG,PNG}DataProvider()). To get a CGImage from an NSImage, use -CGImageForProposedRect:context:hints:.
Then, get the data provider from that CGImage and create the mask CGImage from that, using the various properties (width, height, bpp, etc.) queried from the first CGImage using the various CGImageGet... functions.
I am getting an ImagesServiceFailureException exception from the app engine ImagesServiceFactory when specifying JPEG as the output format. It works fine when I specify PNG as the output format. I have only tried this on the app engine development server, not yet on the production server. JPEG is supposed to work according to the app engine docs.
Any idea why this doesn't work? I really don't want to use PNG because of the much greater size images relative to JPEG.
Following is my code:
byte[] sourceData = fetchBlobData(sourceKey);
Image sourceImage = ImagesServiceFactory.makeImage(sourceData);
// Image sourceImage = ImagesServiceFactory.makeImageFromBlob(thumbSourceKey);
ImagesService imagesService = ImagesServiceFactory.getImagesService();
// Create the transform and the new image
Transform resize = ImagesServiceFactory.makeResize(NEW_WIDTH,NEW_HEIGHT);
Image newImage = imagesService.applyTransform(resize, sourceImage, OutputEncoding.JPEG);
When I output as JPEG, I get the following exception:
com.google.appengine.api.images.ImagesServiceFailureException: Failed to encode image
Any help would be greatly appreciated.
I use the below command to display the image
imshow(img,[]);
when i use the following command to save the image it is saved as an empty white page
imsave;
how to save the image in this case any command would do
You are probably running into an issue with matrix type and range. If img is type double it needs to be scaled between 0 and 1.
A common issue is to load an image in uint8 (scaled between 0 and 255), convert to double in order to do some processing on it, without scaling, and then try and save it out. When you do that, MATLAB tries to convert back to uint8, and any values in the image outside the [0 1] range are clipped. On many images this means that the file comes out all white.
To get around this, use functions like im2double and im2uint8 rather than just double or uint8 when converting images.
Try at the command line the difference between:
img = imread('pout.tif');
img = double(img);
imshow(img,[]);
imsave;
and
img = imread('pout.tif');
img = im2double(img);
imshow(img,[]);
imsave;
Convert image data into an actual image and try again:
h = image(img); %Convert to object
imsave(h); %Save image object
Notice that if you close the figure window generated by image(), the object is deleted and the handle has will point to nothing. Though this may be beyond of what you are asking for.
Hope this adjustment solved your problem
First convert the image to rgb using
img1=label2rgb(img);
then again convert the image into an gray image using
img2=rgb2gray(img1);
then u can use imshow to show the image and save it using imsave
imshow(img2);
imsave();
In Google AppEngine I want to transform a boolean[] into an image then serve the image. I want the boolean[] to be transformed to black and white pixels. I can see that AppEngine provides
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
// ...
byte[] imageData; // ...
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image image = ImagesServiceFactory.makeImage(imageData);
but I don't know what the format of the byte[] imageData should be, i.e., how to transform the boolean[] to byte[]
And once I have this image, how can the client get it?
The Image service API accepts data in any of the supported image file formats. According to this page, these formats include "JPEG, PNG, WEBP, GIF (including animated GIF), BMP, TIFF and ICO". The Image service does not provide a way to create new image data from scratch. But you can use a graphics library to produce the image in one of the accepted data formats, then use the service to convert it to another format, or transform it. Of course, depending on the graphics library, you may be able to get the final image directly from the library, and not use the service.
To serve an image, just set the appropriate Content-Type header for the data format you're using, then write the bytes of the image data to the response's output stream:
// byte[] pngData = ...
resp.addHeader("Content-Type", "image/png");
resp.getOutputStream().write(pngData);
If you want to try generating the image data without a library, the BMP format might be easiest. You can use the Images service to convert this to PNG.