Rust open image with other format than file extension - image

I am trying to open an image in my Rust program. The image is named foo.png, but it won't open the image with image::open("foo.png").
If I rename the file to foo.jpeg, I can open the image, so my guess is that the formatting and the file extension do not match.
My question is then. how do I open a file named foo.png, but decode it as a jpeg?
I have tried image::io::Reader, but I can't seem to make it work properly.

You can use the image::load() function, which lets you specify the format:
let image = image::load(BufReader::new(File::open("foo.png")?), ImageFormat::Jpeg)?;

Related

Cannot open PNGs created with canvas.toDataUrl

I create a png blob from a canvas with the toDataUrl method.
const pngdata = canvas.toDataURL("image/png");
I open a text editor and copy the content of pngdata into a file that I call img.png
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkMAAAiICAYAAAD...
I save this file. When I try to open it (Windows 10) I get "It looks like we don't support this file format"
Removing data:image/png;base64, from the file doesnt help
Why does this not work?
Because your file is still encoded as Base64. You need to decode it to actual binary data for it to be a correct binary file.
You can find many online tools that will do this for you, or the best is probably to directly export your canvas drawing to a binary Blob and download this Blob thanks to a blob:// URL.

How to use bash to convert a pdf to a png file?

Hey I'm new to writing bash and would like to make a script that asks a user to enter a pdf file and then converts the pdf to a png file. Is there a simple way to go about doing this? I know to use read command to ask for user input. How would I handle variables and then once the user enters the pdf file location how to change it to a png?
Thanks.
You can use convert command
convert file.pdf file.png

File created using imwrite does not appear

New to Matlab, so sorry if this is a silly question. I'm filtering a series images for my research. I'm not having a problem with the actual image processing, it's when I go to save the modified images that I run into trouble. For some reason, I can only save the modified images using imwrite as .gif files. If I try to save them as .jpg, .bmp, etc., the file does not appear in the working folder. The corresponding generic file appears, but the actual .jpg does not. Additionally, when I use imread to reopen the midified files (that actually saved as .gifs), the image is just black. But, if I open the .gif file outside Matlab, it appears as expected. Code below.
close all
N=90;
IMAGES=cell(1,N); %creates a cell to store image data
FNAMEFMT='20110805115033(1)_%d.jpg';
for i=1:N
IMAGES{i}=imread(sprintf(FNAMEFMT,i)); %reads original images into IMAGES
end
RESULT=cell(1,N); %to store modified/filtered images
for i=1:N
gray=rgb2gray(IMAGES{i}); %converts to grayscale
binary=im2bw(gray,.5); %converts to bw
filter=bwareaopen(binary,35); %removes small features
RESULT{i}=filter; %saves modified image in RESULTS
end
for i=1:N
WRITEFMT='filter_%d';
imwrite(RESULT{i},sprintf(WRITEFMT,i),'gif'); %writes RESULTS as .gif
end
If I try to save them as .jpg, .bmp, etc., the file does not appear in the working folder.
You need to change
WRITEFMT='filter_%d';
to
WRITEFMT='filter_%d.jpg';
The files you are outputting are jpeg files (as per the imwrite argument 'jpg' instead of 'gif'), but they don't have a file extension. If you manually add the extension, they open as jpgs.
For the black gif, see if this helps.
Once you export as jpg, viewing them works
imshow(imread('filter_1.jpg'))

How to modify a .png image with VBScript

I have a need to select a portion of a .png file, with specific cordinators, and delete this area then save the file back with the same name.
I would appreciate if you can help to come up with a VBScript script that can accomplish this task.
It would be great if all proesses happen in the background, but it would be ok too if the image file has to be open and visible. Thanks a bunch!!!
A PNG file, like any other binary file can be edited with CMD or VBS.
A PNG file layout is as follows:
File header
"Chunks" within the file
Pixel format
Transparency of image
Compression
Interlacing
Animation
Read the PNG format in RFC 2083 to know how to edit/create a PNG file at binary/bit level.
To speed up the editing process, libraries are available for application level editing.
Here are some VBA codes for image manipulation.
ImageMagick also provides libraries that can be accessed via VBS for image editing.
Here's a VBScript Image Class for bmp and pcx files (which PNG can be converted to before editing via WIA).
Loadpicture function described here doesn't seem to support PNG, but this discussion might solve it.
The Windows Image Acquisition Library v2.0 supports PNG, BMP, JPG, GIF and TIFF image formats, and comes with Windows Vista and later versions. Sample scripts are available to demonstrate "image manipulation using the ImageFile object". The Vector.ImageFile property also "creates an ImageFile object from raw ARGB data".
More sample codes here & here show how to rotate, flip, scale, crop, etc with WIA Image constants in vbs. To remove unwanted areas of image (with given coordinates), use the crop function.
Here's a discussion of WIA 2.0 image editing on stackoverflow.
VBScript doesn't have any image editing functions, so you need an external tool for that. For example, GIMP can do image processing from the command line (see here). ImageMagick provides a scriptable component in addition to the command-line interface (details here).
To run a command line from a VBScript script, you can use the WShShell.Run method. To create an instance of a COM scriptable component, use the CreateObject function.

Converting a visio to a JPG image

I have a .vss type file which I want to convert to an image format (like JPG/GIF). Is it possible to do this and if so, how?
Not programming related and you can simply use "File > Save as..." to save as a JPG.

Resources