I've read many answers and tried all of this in the shell, but does not want to save my local image correctly, he truncate files. When image size is 400kb, he create file in media dir with size 10-30kb. I don't know why. For example i have image with path d:/1.png. I tried
from django.core.files import File
fileObject=File(open("d:/1.png"))
object.image.save('1.png',fileObject,True)
fileObject.size show correct size of image, but object.image.size is not correct and file, what he save not full.
Also i tried
from django.core.files.temp import NamedTemporaryFile
temp = NamedTemporaryFile()#with delete=True TypeError: __init__() got an unexpected keywork argument 'delete'
temp.write(open('d:/1.png').read())
temp.flush()
f=File(temp)#f.size not correct
object.image.save('1.png',f,True)
and object.image.size and file not correct, file not full.
I tried using StringIO, but this not work too. I don't know what try to save this images correctly.
Please, help.
you have to change mode to binary
fileObject=File(open("d:/1.png", mode="rb"))
Related
using following code to save all tiffs to a single PDF
from PIL import Image
imagelist[0].save(fp=path_to_saving_location+"specified_name.pdf",save_all =True, append_images = imagelist[1:])
instead it is saving the PDF as 001.pdf.
Am I doing anything wrong here?
Create a variable called fullpath in the line above the save statement and set its value how you think.
Print it so you can check it. Then use:
imagelist[0].save(fullpath, save_all =True, append_images = imagelist[1:])
Also, consider deleting 001.pdf before you run your program, so you can be sure if your program really does create it.
I want to write an image using opencv to a temporary file, get the path of that temporary file and pass that path to a function.
import cv2 as cv
from tempfile import NamedTemporaryFile, SpooledTemporaryFile
img = create_my_awesome_image()
with NamedTemporaryFile(suffix=".png") as temp:
print(temp.name)
cv.imwrite(temp.name, img) # this one sparks joy
with SpooledTemporaryFile(max_size=1000000, suffix=".png") as temp:
print(temp.name)
cv.imwrite(temp.name, img) # this one does not
The first print prints C:\Users\FLORIA~1\AppData\Local\Temp\tmpl2i6nc47.png.
While the second print prints: None.
Using NamedTemporaryFile works perfectly find. However, because the second print prints None, I cannot use the SpooledTemporaryFile together with opencv. Any ideas why the prefix argument of SpooledTemporaryFile is ignored?
The problem is that a spooled file (such as a SpooledTemporaryFile) doesn't exist on the disk, so it also doesn't have a name.
However, note that cv2.imread() will take a file name as an argument, meaning that it will handle the file opening and it doesn't support spooled files.
If you are only working with png images, they are not encoded, meaning that the variable img already contains the image data in memory and there is nothing else for you to do, just call cv2.imwrite() when you want to save it to the disk. If you want to use a temporary file, it has to be a NamedTemporaryFile.
If you want to handle an encoded image format in memory, such as jpg, you can use cv2.imencode() for that purpose, as in this answer.
I'm trying to skimage.io.imread() an image (say a tiff file, for concreteness) that was previously written to a tempfile.TemporaryFile(). However, skimage complains by saying
ValueError: Cannot determine type of file b'<_io.BufferedRandom name=6>'
I am doing this because another program writes the image to standard output.
I collect it with subprocess.check_output and write it to the temporary file, thus avoiding saving the image to disk.
Does anyone know how to achieve this, or has got a better idea on how to pipe an image from stdout into a python image, ultimately to be treated as a numpy.ndarray?
A solution is the following
with NamedTemporaryFile() as f:
skimage.io.imread(f.name, plugin="tifffile")
Alternatively, one can replace freeimage with tifffile.
Earlier I was passing the file object, but imread actually wants a filename.
I took a look at the Split multi-page tiff with python file for Splitting a .TIFF File, however to be honest, I didn't fully understand the answers, and I'm hoping for a little clarification.
I am attempting to take a .Tif file with multiple Invoices in it and Split it into each page which will then be Zipped Up and uploaded into a database. PIL is installed on the computers that will be running this program, as such I'd like to stick with the PIL Library. I know that I can view information such as the Size of each Image using PIL after it's open, however when I attempt to Save each it gets dicey. (Example Code Below)
def Split_Images(img,numFiles):
ImageFile = Image.open(img)
print ImageFile.size[0]
print ImageFile.size[1]
ImageFile.save('InvoiceTest1.tif')[0]
ImageFile.save('InvoiceTest2.tif')[1]
However when I run this code I get the following Error:
TypeError: 'NoneType' object has no attribute '__getitem__'
Any Suggestions?
Thank you in advance,
You need the PIL Image "seek" method to access the different pages.
from PIL import Image
img = Image.open('multipage.tif')
for i in range(4):
try:
img.seek(i)
img.save('page_%s.tif'%(i,))
except EOFError:
break
I need open an image, verify the image, then reopen it (see last sentence of below quote from PIL docs)
im.verify()
Attempts to determine if the file is broken, without actually decoding
the image data. If this method finds any problems, it raises suitable
exceptions. This method only works on a newly opened image; if the
image has already been loaded, the result is undefined. Also, if you
need to load the image after using this method, you must reopen the
image file.
This is what I have in my code, where picture is a django InMemoryUploadedFile object:
img = Image.open(picture)
img.verify()
img = Image.open(picture)
The first two lines work fine, but I get the following error for the third line (where I'm attempting to "reopen" the image):
IOError: cannot identify image file
What is the proper way to reopen the image file, as the docs suggest?
This is no different than doing
f = open('x.png')
Image.open(f)
Image.open(f)
The code above does not work because PIL advances in the file while reading its first few bytes to (attempt to) identify its format. Trying to use a second Image.open in this situation will fail as noted because now the current position in the file is past its image's header. To confirm this, you can verify what f.tell() returns. To solve this issue you have to go back to the start of the file either by doing f.seek(0) between the two calls to Image.open, or closing and reopening the file.
Try doing a del img between the verify and second open.