How do I set an ImageDocument not to be dirty anymore in python dm-script without saving?
I have the python code posted below which can be represented by the following dm-script code.
String file_path = GetApplicationDirectory(0, 1).PathConcatenate("test-image.dm4");
Image img := realimage("test", 4, 64, 64);
ImageDocument doc = img.ImageGetOrCreateImageDocument();
doc.ImageDocumentSaveToFile("Gatan Format", file_path);
doc.ImageDocumentShowAtRect(100, 100, 164, 164);
The (python code below) creates and shows an ImageDocument. The image is saved already. But even saving it directly in DigitalMicrograph with its own module it does not recognize that it is saved. I can link the file manually (by executing dm-script code from python) but I cannot tell the program that the images are not modified.
There is a function ImageDocumentIsDirty(). But this function only returns whether the image is modified or not. I cannot set it.
My program creates a new workspace and loads more than 100 images. When closing DigitalMicrograph, it asks for every single of the 100 images if it should be saved. I cannot leave the user with 100 times clicking No. Especially because the files are saved.
So, how do I tell dm-script that the image is saved already?
try:
import DigitalMicrograph as DM
import numpy as np
import execdmscript
import os
name = "Test image"
file_path = os.path.join(os.getcwd(), "test-image.dm4")
# create image
image_data = np.random.random((64, 64))
image = DM.CreateImage(image_data)
image.SetName(name)
# create, save and show image document
image_doc = image.GetOrCreateImageDocument()
image_doc.SetName(name)
image_doc.SaveToFile("Gatan Format", file_path)
print("Saving image to", file_path)
image_doc.ShowAtRect(100, 100, 164, 164)
# link the image to the file
dmscript = "\n".join((
"for(number i = CountImageDocuments() - 1; i >= 0; i--){",
"ImageDocument img_doc = GetImageDocument(i);",
"if(img_doc.ImageDocumentGetName() == name){",
"img_doc.ImageDocumentSetCurrentFile(path);",
"break;",
"}",
"}"
))
svars = {
"name": image_doc.GetName(),
"path": file_path
}
with execdmscript.exec_dmscript(dmscript, setvars=svars):
pass
except Exception as e:
print("{}: ".format(e.__class__.__name__), e)
import traceback
traceback.print_exc()
the command you're looking for is
void ImageDocumentClean( ImageDocument imgDoc )
as in
image img := realimage("test",4,100,100)
img.ShowImage()
imageDocument doc = img.ImageGetOrCreateImageDocument()
Result("\n Dirty? " + doc.ImageDocumentIsDirty())
doc.ImageDocumentClean()
Result("\n Dirty? " + doc.ImageDocumentIsDirty())
Also: The reason it becomes dirty in a first place is, that window-positions are stored as part of the document. (Other things, like tags, could also apply.)
Related
I'm working with compressed DICOM images I would like to decompress, with Python 2.7 on Ubuntu 14. I'm using gdcm, which I've installed following this link (sudo apt-get install python-gdcm)
I'm using this example to decompress an image (at least ImageJ calls it a "compressed dicom image" when I try to open it), but I get an error I can't solve. Code follows (it is simply the example in the link)
import gdcm
import sys
if __name__ == "__main__":
file1 = sys.argv[1]
file2 = sys.argv[2]
r = gdcm.ImageReader()
r.SetFileName(cin)
if not r.Read():
sys.exit(1)
image = gdcm.Image()
ir = r.GetImage()
image.SetNumberOfDimensions( ir.GetNumberOfDimensions() );
dims = ir.GetDimensions();
print ir.GetDimension(0);
print ir.GetDimension(1);
print "Dims:",dims
image.SetDimension(0, ir.GetDimension(0) );
image.SetDimension(1, ir.GetDimension(1) );
pixeltype = ir.GetPixelFormat();
image.SetPixelFormat( pixeltype );
pi = ir.GetPhotometricInterpretation();
image.SetPhotometricInterpretation( pi );
pixeldata = gdcm.DataElement( gdcm.Tag(0x7fe0,0x0010) )
str1 = ir.GetBuffer()
#print ir.GetBufferLength()
pixeldata.SetByteValue( str1, gdcm.VL( len(str1) ) )
image.SetDataElement( pixeldata )
w = gdcm.ImageWriter()
w.SetFileName(path_save+"uncompressed.png")
w.SetFile( r.GetFile() )
w.SetImage( image )
if not w.Write():
sys.exit(1)
At the print dims mark program indeed prints the correct dimensions of the image. But when it reaches w.SetImage(image), I get an error, and I also get a bunch of warnings :
Warning: In /build/gdcm-uIgnvq/gdcm-2.6.3/Source/MediaStorageAndFileFormat/gdcmOverlay.cxx, line 205, function void gdcm::Overlay::Update(const gdcm::DataElement&)
Warning: In /build/gdcm-uIgnvq/gdcm-2.6.3/Source/MediaStorageAndFileFormat/gdcmPixmapReader.cxx, line 544, function bool gdcm::DoOverlays(const gdcm::DataSet&, gdcm::Pixmap&)
Bits Allocated are wrong. Correcting.
Error: In /build/gdcm-uIgnvq/gdcm-2.6.3/Source/MediaStorageAndFileFormat/gdcmOverlay.cxx, line 265, function bool gdcm::Overlay::GrabOverlayFromPixelData(const gdcm::DataSet&)
Could not find Pixel Data. Cannot extract Overlay.
Warning: In /build/gdcm-uIgnvq/gdcm-2.6.3/Source/MediaStorageAndFileFormat/gdcmPixmapReader.cxx, line 550, function bool gdcm::DoOverlays(const gdcm::DataSet&, gdcm::Pixmap&)
Could not extract Overlay from Pixel Data
Warning: In /build/gdcm-uIgnvq/gdcm-2.6.3/Source/MediaStorageAndFileFormat/gdcmPixmapReader.cxx, line 575, function bool gdcm::DoOverlays(const gdcm::DataSet&, gdcm::Pixmap&)
Invalid BitPosition: 0 for overlay #0 removing it.
python2.7: /build/gdcm-uIgnvq/gdcm-2.6.3/Source/Common/gdcmObject.h:58: virtual gdcm::Object::~Object(): Assertion `ReferenceCount == 0' failed.
Is this example only valid for certain kinds of images ? Or am I missing something ?
Since you are trying to simply decompress the image using python, why not use simply this:
import gdcm
import sys
if __name__ == "__main__":
file1 = sys.argv[1] # input filename
file2 = sys.argv[2] # output filename
reader = gdcm.ImageReader()
reader.SetFileName( file1 )
if not reader.Read():
sys.exit(1)
change = gdcm.ImageChangeTransferSyntax()
change.SetTransferSyntax( gdcm.TransferSyntax(gdcm.TransferSyntax.ImplicitVRLittleEndian) )
change.SetInput( reader.GetImage() )
if not change.Change():
sys.exit(1)
writer = gdcm.ImageWriter()
writer.SetFileName( file2 )
writer.SetFile( reader.GetFile() )
writer.SetImage( change.GetOutput() )
if not writer.Write():
sys.exit(1)
When using:
$ python decompress.py gdcm/Testing/Data/012345.002.050.dcm raw.dcm
This leads to:
$ gdcminfo raw.dcm
MediaStorage is 1.2.840.10008.5.1.4.1.1.4 [MR Image Storage]
TransferSyntax is 1.2.840.10008.1.2 [Implicit VR Little Endian: Default Transfer Syntax for DICOM]
NumberOfDimensions: 2
Dimensions: (256,256,1)
SamplesPerPixel :1
BitsAllocated :16
BitsStored :16
HighBit :15
PixelRepresentation:1
ScalarType found :INT16
PhotometricInterpretation: MONOCHROME2
PlanarConfiguration: 0
TransferSyntax: 1.2.840.10008.1.2
Origin: (-85,21.6,108.7)
Spacing: (0.664062,0.664062,1.5)
DirectionCosines: (1,0,0,0,0,-1)
Rescale Intercept/Slope: (0,1)
Orientation Label: CORONAL
Update, it seems the original bug
gdcmObject.h:58: virtual gdcm::Object::~Object(): Assertion
`ReferenceCount == 0' failed.
has been resolved upstream here:
Fix an issue with SmartPointer of an Image in Python
This is related to another question I posted, but is more specific (and hopefully gets more specific answers).
I am trying to display png images on an IPython notebook, and update the display as the png files are updated.
One possible solution is below. Unfortunately, my implementation does not update the file, and creates a new HTML block at the end of the old one. What I want instead, is to replace the old HTML block with the new one -i.e. replace the content only.
As example code, I have two notebooks. One notebook generates pngs and saves them in a figures directory.
import os
import glob
import time
import matplotlib.pyplot as plt
import numpy as np
for ix in range(20):
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2 # 0 to 15 point radiuses
fig = plt.figure(figsize=(8,8))
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
fig.savefig(os.path.join("figures", "fig1.png"), bbox_inches="tight")
plt.close(fig)
time.sleep(3)
A second notebook shows the pngs It is supposed top show a single png which updates. Instead it stacks the same png on itself, every time the png is updated.
import os
import time
from IPython.html.widgets import interact, interactive, fixed
from IPython.html import widgets
from IPython.display import clear_output, display, HTML
def get_latest_file_ts(directory="figures", file_name="fig1.png", strip_directory=True):
"""
Continuously check for modifications to the file file_name in directory. If file has been
modified after touched_on, return the Unix timestamp of the modification time.
:param directory: string / the directory where the file is
:param file_name: string / the file name
:param strip_directory: boolean / if True, strip the directory part of the file name
:return:
"""
if strip_directory:
fname = os.path.join(directory, file_name)
else:
fname = file_name
try:
return os.stat(fname).st_mtime
except:
print "FileNotFoundException: Could not find file %s" % fname
return None
def check_if_modified_file(directory="figures", file_name="fig1.png",
touched_on=1420070400, sleep_time=1, strip_directory=True):
"""
Continuously check for modifications to the file file_name in directory. If file has been
modified after touched_on, return the Unix timestamp of the modification time.
:param directory: string / the directory where the file is
:param file_name: string / the file name
:param touched_on: float / the Unix timestamp on which the file was last modified
:param sleep_time: float / wait time between interactions
:param strip_directory: boolean / if True, strip the directory part of the file name
:return:
"""
if strip_directory:
fname = os.path.join(directory, file_name)
else:
fname = file_name
while True:
try:
latest_touch = os.stat(fname).st_mtime
if latest_touch == touched_on:
time.sleep(sleep_time)
else:
return latest_touch
except:
print "FileNotFoundException: Could not find %s" % fname
return None
def show_figs(directory="figures", file_name="fig1.png"):
s = """<figure>\n\t<img src="%s" alt="The figure" width="304" height="228">\n</figure>""" % os.path.join(directory, file_name)
display(HTML(s))
timestamp = get_latest_file_ts(directory="figures", file_name="fig1.png", strip_directory=True)
show_figs(directory="figures", file_name="fig1.png")
cnt = 1
while True and cnt < 4:
timestamp = check_if_modified_file(directory="figures", file_name="fig1.png", touched_on=timestamp, sleep_time=1, strip_directory=True)
display(HTML(""))
show_figs(directory="figures", file_name="fig1.png")
time.sleep(1)
cnt += 1
(as you can see I have added upper limits on the executions of both the generator and consumer loops)
Any help on how to make widgets update HTML content would be awesome.
The problem that you only see the first image is very likely related to caching of the browser. To overcome this issue a simple solution is to add a varying query string to the image src as shown e.g. here.
Thus your show_figs method could look like:
import time
def show_figs(directory="figures", file_name="fig1.png"):
s = """<figure>\n\t<img src="{0}?{1}" alt="The figure" width="304" height="228">\n</figure>""".format(os.path.join(directory, file_name),time.time())
display(HTML(s))
In combination with the clear_output function you should be able to get your updated image.
I am trying to extract the Dpi value of an image Using python in one of my django powered web application.I am using following function to achieve my desired output that is the Dpi value of an image but i am facing an exception.
This is the Function to get DPI value of an Image
def get_exif_data(fname):
"""Get embedded EXIF data from image file."""
ret = {}
try:
img = Image.open(fname)
if hasattr( img, '_getexif' ):
exifinfo = img._getexif()
if exifinfo != None:
for tag, value in exifinfo.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
except IOError:
print 'IOERROR ' + fname
return ret
and this is the view where i have used that above function to get the DPI value of an Image.
def get_dpi(request,image_id):
image = get_object_or_404(Photo,pk = image_id)
img = Image.open(image.photo)
dpi_info = get_exif_data(img)
context = RequestContext(request)
ctx = {'dpi':dpi_info}
return render_to_response('photo/download_image.html',ctx,context)
but i am facing the following exception
To begin, I have to ask why you want the DPI resolution. It's just a tag and doesn't really mean anything unless you are outputing to physical media. A 1000x1000 pixel image can be 10x10 at 100dpi or 100x100 at 10dpi, but it's still exactly the same image. Exactly the same pixels. It's hard to imagine scenarios where img.size doesn't give you everything you need.
Having said that, if you want to get the exif tags for resolution try XResolution from PIL.ExifTags:
import Image
from PIL.ExifTags import TAGS
img = Image.open("path/to/.jpg")
info = img._getexif()
exifObj = {}
if info != None:
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
exifObj[decoded] = value
exifObj now either empty or equals something like:
{'YResolution': (720000, 10000), 'BitsPerSample': (8, 8, 8), 'ImageLength': 713, 'Orientation': 1, 'Copyright': 'Mark Meyer Photography', 'ExifImageWidth': 950, 'ExifImageHeight': 713, 'ColorSpace': 1, 'ResolutionUnit': 2, 'DateTime': '2015:01:30 21:37:51', 'XResolution': (720000, 10000), 'ExifOffset': 296, 'PhotometricInterpretation': 2, 'ExifVersion': '0221', 'Artist': 'MarkM', 'ImageWidth': 950, 'SamplesPerPixel': 3, 'Software': 'Adobe Photoshop CC 2014 (Macintosh)'}
DPI is:
exifObj['XResolution'][0]/exifObj['XResolution'][1]
72DPI in this case.
It's not clear in your example how you are trying to access the DPI value for the context. You're getting an attribute error, so maybe in your template you are trying to access ctx.dpi or something similar which doesn't exist.
Almost I spend searching 3 hours for this. At last I find it out, how we can find dpi of an image or pdf using fitz library, you can download using this command pip install PyMuPDF and pip install fitz.
If you want to know more about this process you can check it out official documentation. If you found useful upvote it!
def dpi_finder(link : str) -> int:
doc = fitz.open(link) # opening a image or pdf.
page = doc.load_page(0) # getting first page
pix = page.get_pixmap() # getting the pixamp
return pix.xres # it will give the dpi for horizontal resolution
I'm learning Python and Django.
An image is provided by the user using forms.ImageField(). Then I have to process it in order to create two different sized images.
When I submit the form, Django returns the following error:
IOError at /add_event/
cannot identify image file
I call the resize function:
def create_event(owner_id, name, image):
image_thumb = image_resizer(image, name, '_t', 'events', 180, 120)
image_medium = image_resizer(image, name, '_m', 'events', 300, 200)
I get en error when image_resizer is called for the second time:
def image_resizer(image, name, size, app_name, length, height):
im = Image.open(image)
if im.mode != "RGB":
im = im.convert("RGB")
im = create_thumb(im, length, height)
posit = str(MEDIA_ROOT)+'/'+app_name+'/'
image_2 = im
image_name = name + size +'.jpg'
imageurl = posit + image_name
image_2.save(imageurl,'JPEG',quality=80)
url_image='/'+app_name+'/'+image_name
return url_image
Versions:
Django 1.3.1
Python 2.7.1
PIL 1.1.7
I'm trying to find the problem, but i don't know what to do. Thank you in advanced!
EDIT
I solved rewriting the function; now it creates the different images in batch:
I call the resize function:
url_array = image_resizer.resize_batch(image, image_name, [[180,120,'_t'], [300,200,'_m']], '/events/')
so:
image_thumb = url_array[0]
image_medium = url_array[1]
and the resize function:
def resize_batch(image, name, size_array, position):
im = Image.open(image)
if im.mode != "RGB":
im = im.convert("RGB")
url_array = []
for size in size_array:
new_im = create_thumb(im, size[0], size[1])
posit = str(MEDIA_ROOT) + position
image_name = name + size[2] +'.jpg'
imageurl = posit + image_name
new_im.save(imageurl,'JPEG',quality=90)
new_url_array = position + image_name
url_array.append(new_url_array)
return url_array
Thanks to all!
As ilvar asks in the comments, what kind of object is image? I'm going to assume for the purposes of this answer that it's the file property of a Django ImageField that comes from a file uploaded by a remote user.
After a file upload, the object you get in the ImageField.file property is a TemporaryUploadedFile object that might represent a file on disk or in memory, depending on how large the upload was. This object behaves much like a normal Python file object, so after you have read it once (to make the first thumbnail), you have reached the end of the file, so that when you try to read it again (to make the second thumbnail), there's nothing there, hence the IOError. To make a second thumbnail, you need to seek back to the beginning of the file. So you could add the line
image.seek(0)
to the start of your image_resizer function.
But this is unnecessary! You have this problem because you are asking the Python Imaging Library to re-read the image for each new thumbnail you want to create. This is a waste of time: better to read the image just once and then create all the thumbnails you want.
I'm guessing that is a TemporaryUploadedFile ... find this with type(image).
import cStringIO
if isinstance(image, TemporaryUploadedFile):
temp_file = open(image.temporary_file_path(), 'rb+')
content = cStringIO.StringIO(temp_file.read())
image = Image.open(content)
temp_file.close()
I'm not 100% sure of the code above ... comes from 2 classes I've got for image manipulation ... but give it a try.
If is a InMemoryUploadedFile your code should work!
I'm trying to convert an UploadedFile to a PIL Image object to thumbnail it, and then convert the PIL Image object that my thumbnail function returns back into a File object. How can I do this?
The way to do this without having to write back to the filesystem, and then bring the file back into memory via an open call, is to make use of StringIO and Django InMemoryUploadedFile. Here is a quick sample on how you might do this. This assumes that you already have a thumbnailed image named 'thumb':
import StringIO
from django.core.files.uploadedfile import InMemoryUploadedFile
# Create a file-like object to write thumb data (thumb data previously created
# using PIL, and stored in variable 'thumb')
thumb_io = StringIO.StringIO()
thumb.save(thumb_io, format='JPEG')
# Create a new Django file-like object to be used in models as ImageField using
# InMemoryUploadedFile. If you look at the source in Django, a
# SimpleUploadedFile is essentially instantiated similarly to what is shown here
thumb_file = InMemoryUploadedFile(thumb_io, None, 'foo.jpg', 'image/jpeg',
thumb_io.len, None)
# Once you have a Django file-like object, you may assign it to your ImageField
# and save.
...
Let me know if you need more clarification. I have this working in my project right now, uploading to S3 using django-storages. This took me the better part of a day to properly find the solution here.
I've had to do this in a few steps, imagejpeg() in php requires a similar process. Not to say theres no way to keep things in memory, but this method gives you a file reference to both the original image and thumb (usually a good idea in case you have to go back and change your thumb size).
save the file
open it from filesystem with PIL,
save to a temp directory with PIL,
then open as a Django file for this to work.
Model:
class YourModel(Model):
img = models.ImageField(upload_to='photos')
thumb = models.ImageField(upload_to='thumbs')
Usage:
#in upload code
uploaded = request.FILES['photo']
from django.core.files.base import ContentFile
file_content = ContentFile(uploaded.read())
new_file = YourModel()
#1 - get it into the DB and file system so we know the real path
new_file.img.save(str(new_file.id) + '.jpg', file_content)
new_file.save()
from PIL import Image
import os.path
#2, open it from the location django stuck it
thumb = Image.open(new_file.img.path)
thumb.thumbnail(100, 100)
#make tmp filename based on id of the model
filename = str(new_file.id)
#3. save the thumbnail to a temp dir
temp_image = open(os.path.join('/tmp',filename), 'w')
thumb.save(temp_image, 'JPEG')
#4. read the temp file back into a File
from django.core.files import File
thumb_data = open(os.path.join('/tmp',filename), 'r')
thumb_file = File(thumb_data)
new_file.thumb.save(str(new_file.id) + '.jpg', thumb_file)
This is actual working example for python 3.5 and django 1.10
in views.py:
from io import BytesIO
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import InMemoryUploadedFile
def pill(image_io):
im = Image.open(image_io)
ltrb_border = (0, 0, 0, 10)
im_with_border = ImageOps.expand(im, border=ltrb_border, fill='white')
buffer = BytesIO()
im_with_border.save(fp=buffer, format='JPEG')
buff_val = buffer.getvalue()
return ContentFile(buff_val)
def save_img(request)
if request.POST:
new_record = AddNewRecordForm(request.POST, request.FILES)
pillow_image = pill(request.FILES['image'])
image_file = InMemoryUploadedFile(pillow_image, None, 'foo.jpg', 'image/jpeg', pillow_image.tell, None)
request.FILES['image'] = image_file # really need rewrite img in POST for success form validation
new_record.image = request.FILES['image']
new_record.save()
return redirect(...)
Putting together comments and updates for Python 3+
from io import BytesIO
from django.core.files.base import ContentFile
import requests
# Read a file in
r = request.get(image_url)
image = r.content
scr = Image.open(BytesIO(image))
# Perform an image operation like resize:
width, height = scr.size
new_width = 320
new_height = int(new_width * height / width)
img = scr.resize((new_width, new_height))
# Get the Django file object
thumb_io = BytesIO()
img.save(thumb_io, format='JPEG')
photo_smaller = ContentFile(thumb_io.getvalue())
To complete for those who, like me, want to couple it with Django's FileSystemStorage:
(What I do here is upload an image, resize it to 2 dimensions and save both files.
utils.py
def resize_and_save(file):
size = 1024, 1024
thumbnail_size = 300, 300
uploaded_file_url = getURLforFile(file, size, MEDIA_ROOT)
uploaded_thumbnail_url = getURLforFile(file, thumbnail_size, THUMBNAIL_ROOT)
return [uploaded_file_url, uploaded_thumbnail_url]
def getURLforFile(file, size, location):
img = Image.open(file)
img.thumbnail(size, Image.ANTIALIAS)
thumb_io = BytesIO()
img.save(thumb_io, format='JPEG')
thumb_file = InMemoryUploadedFile(thumb_io, None, file.name, 'image/jpeg', thumb_io.tell, None)
fs = FileSystemStorage(location=location)
filename = fs.save(file.name, thumb_file)
return fs.url(filename)
In views.py
if request.FILES:
fl, thumbnail = resize_and_save(request.FILES['avatar'])
#delete old profile picture before saving new one
try:
os.remove(BASE_DIR + user.userprofile.avatarURL)
except Exception as e:
pass
user.userprofile.avatarURL = fl
user.userprofile.thumbnailURL = thumbnail
user.userprofile.save()
Here is an app that can do that: django-smartfields
from django.db import models
from smartfields import fields
from smartfields.dependencies import FileDependency
from smartfields.processors import ImageProcessor
class ImageModel(models.Model):
image = fields.ImageField(dependencies=[
FileDependency(processor=ImageProcessor(
scale={'max_width': 150, 'max_height': 150}))
])
Make sure to pass keep_orphans=True to the field, if you want to keep old files, otherwise they are cleaned up upon replacement.
For those using django-storages/-redux to store the image file on S3, here's the path I took (the example below creates a thumbnail of an existing image):
from PIL import Image
import StringIO
from django.core.files.storage import default_storage
try:
# example 1: use a local file
image = Image.open('my_image.jpg')
# example 2: use a model's ImageField
image = Image.open(my_model_instance.image_field)
image.thumbnail((300, 200))
except IOError:
pass # handle exception
thumb_buffer = StringIO.StringIO()
image.save(thumb_buffer, format=image.format)
s3_thumb = default_storage.open('my_new_300x200_image.jpg', 'w')
s3_thumb.write(thumb_buffer.getvalue())
s3_thumb.close()