Image shows properly with .show() but saves with artifacts - image

I have unpickled an image, and utilizing PIL when I use img.show() I can see the image in external viewer properly but when I try to save it using img.save() it is plagued by horizontal color artifacts. As img.show() is using bmp to temporarily save and show the file, I tried using img.save(filepath, "bmp") as well as other file formats. Other formats, such as jpg and png, totally corrupt the image, while bmp creates color artifacts. I checked the documentation for PIL but it was not helpful. What method can I use to save the image I see properly?

After many tries, it turns out instead of:
with open("img.bmp", 'w') as f:
image.save(f)
opening the file with the binary attribute "b" added solves the issue. Like this:
with open("img.bmp", 'wb') as f:
image.save(f)

Related

Converting TIFF to PDF with GraphicsMagick MediaBox / CropBox resolution

We are currently converting TIFF files to PDF using GraphicsMagick. The TIFF is coming from an eFAX and has a (pixel) resolution of 1728x2200.
If you do the conversion with tiff2pdf or just open it on Preview and convert export it to PDF, it is generated with a MediaBox value of 612x792 point, which is what is expected.
However graphics magick generates a MediaBox of 1728x4400 and a CropBox of 610x792. It all looks good if you open it on a PDF viewer because it's using the CropBox but if you're feeding it to GhostScript after, you don't get the Image on the full page but as a small square inside the document.
The lazy solutions would be to change for Tiff2PDF or add -dUseCropBox to our GhostScript command but I'd like to know what GraphicsMagick option should be used to have the PDF with the good MediaBox. It's like it doesn't understand that the resolution is in Pixels and not in Point. Hope somebody has insights

what is the valid format for a tiff image in python

How do I write a valid tiff image in python with my own data for the tiff file?
I've tried doing a few things using PIL and it's not a valid image.
If you could answer with some sample code of making a tiff image then editing the data using PIL that would be more than enough and go a long way in moving my program along
Are you required to exactly use PIL? Otherwise, have you tried using it's open source clone called Pillow? Refer to https://python-pillow.github.io/
Pillow seems to be able to directly save image data in tiff file format.
Try something like:
img = Image()
# any editing of 'img'
img.save( 'mytiff.tif' )
Otherwise, you should clarify your needs or problems in depth.

Issue when changing dpi of a tiff using Bit Miracle libtiff

I am trying to just change the resolution of a tiff image using Bit Miracle libtiff. Below is the code snippet. When I do this, tiff image resolution is changed, but the other tiff parameters are gone and I am unable to open the tiff file. Could you please help if I am doing something wrong here?
using (Tiff image = Tiff.Open(fileName, "a"))
{
image.SetField(TiffTag.XRESOLUTION, 200);
image.SetField(TiffTag.YRESOLUTION, 300);
image.WriteDirectory();
}
Please note that changing TiffTag.XRESOLUTION or TiffTag.YRESOLUTION won't change image data and will only change the way the data is interpreted by viewers or printers.
As for the code itself, you should:
Call SetDirectory(0) before calling SetField methods. That's because you open file in append mode.
Use RewriteDirectory instead of WriteDirectory. Otherwise you might corrupt image data.

Inkscape - Not fully converting png into svg

I opened one PNG file in Inkscape and exported it as SVG. When I opened that file with notepad I came to know that the PNG file is embedded within it. If the conversion happened then the resulting file should have only SVG related tags. It shouldn't embed the image within it. Or Am I doing anything wrong.
Note: Save as option also producing the same kind of file. I am using Inkscape version 0.48 in Windows 7 - 64 bit.
This is a bit of an old thread, but it comes up early in Google so I thought I'd contribute something.
In Inkscape, you must do a trace to change the image into SVG. Look at the Path | Trace bitmap menu item and play with the options on that screen.
After creating the trace, you can remove your source image and have a pure svg in your saved file.
I've found it helpful to create layers in Inkscape and move the source image to one layer and put the trace on another layer to let me make quick comparisons using the 'hide layer' buttons.
BTW, your source image can be anything - bmp, jpg, png, etc.
A .png file is a raster image file. In order to convert it to a vector graphic based format like .svg and have it be "native" svg rather than an included image you are going to either have to use a program that can rasterize it or in Inkscape trace the bitmap and turn it into paths. Inkscape provides information on tracing: http://inkscape.org/doc/tracing/tutorial-tracing.html

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'))

Resources