what is the valid format for a tiff image in python - image

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.

Related

modify jpeg pixel data and keep all metadata intact

I'm editing a JPEG image (blurring faces) and want to modify as little data as possible, namely I'm trying to keep the metadata as intact as possible. However, all the libraries for reading and writing image files I've tried so far leave some of the metadata behind.
Here's what I'm currently doing in Python (with Pillow):
import PIL.Image
import numpy as np
image = PIL.Image.open(input_fpath)
blurred_data = blur_image(np.array(image_data))
image.frombytes(blurred_data.tobytes())
image.save(output_fpath, quality="keep", sampling="keep", qtables="keep", **image.info)
Which loses at least the comment and JFIF blocks. I'm thinking that maybe I need to interact directly with libjpeg but maybe there's some tool or library that I'm missing.

Dicom image not converting with dcmtk

The image http://www.barre.nom.fr/medical/samples/files/MR-MONO2-16-head.gz on http://www.barre.nom.fr/medical/samples/is not converting to other image formats. I tried following commands (after extracting the dicom file):
dcm2pnm --write-png MR-MONO2-16-head out.png
dcm2pnm +obr MR-MONO2-16-head out.bmp
dcm2pnm MR-MONO2-16-head out.pnm
It also did not work with dcmj2pnm and dcml2pnm. All of them just produce a gray box. The image otherwise is OK and is correctly read by proper dicom viewer softwares. Where is the problem and how can it be solved?
The problem is that no windowing settings are present in the header. You need to instruct dcm2pnm to calculate a window from the histogram (+Wm) or specify windowing values to apply.
dcm2pnm +Wm +obr MR-MONO2-16-head MR-MONO2-16-head.bmp
yields a bitmap image that looks fine to me.

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.

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.

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

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)

Resources