Show a SVG image in a PDF render by reportlab - python

I'm facing an issue with Reportlab.
I have some troubles with images, for printing I need a full black image with no Cyan, Magenta or Yellow, I tried first with differents PNG, that's more simple to import but in all my tests there's still other colors.
So I need to import svg image but I have everytime an error, there is a simply way to do this ?
This is my code :
canvas = Canvas("*******\\test.pdf",pagesize=(dimX * mm,dimY* mm))
canvas.drawImage("app\static\img\Picto_CE.svg",25*mm,13*mm,width=22, height=5*mm,mask="auto")
canvas.save()
Everytime I tried this, I get the following error :
PIL.UnidentifiedImageError: fileName=<_io.BufferedReader
name='app\static\img\Picto_CE.svg'>
identity=[ImageReader#0x1decbca6f50] cannot identify image file
<_io.BytesIO object at 0x000001DECBDADFD0>
How can I solve this ? I already tried with renderPDF but it doesn't work.
drawing = svg2rlg("app\static\img\Picto_CE.svg")
renderPDF.draw(drawing, canvas, 25*mm, 13*mm)
Nothing appears in my PDF with that.

Related

python3.5 PIL Image not displaying image

The following code does not display the image lists.jpg (in current dir):
print(dir(Image)) displays components; im.size, im.filename, im.format all return correct values.
What have I not done to display this jpg file?
from PIL import Image
im = Image.open("lists.jpg")
im.show() # did not work - perhaps due to the environment Jupyter Notebooks
Solution: replaced module with another with immediate results.
from IPython.display import Image
Image(filename='lists.jpg')
I know it is quite late to post but I will do it for new readers.
This problem arises in case of Jupyter Notebooks. Using show() does not display the image. So discard calling show() like in the code below. This will display the image in the output of the cell.
from PIL import Image
im = Image.open("lists.jpg")
im
To display the image on screen:
from PIL import Image
im = Image.open("lists.jpg")
im.show()
See also http://pillow.readthedocs.io/en/4.0.x/reference/Image.html

Image pasted with PIL produces artifacts

I have a bunch of images I need to put a text-overlay on top of. I created the overlay with GIMP (PNG with transparency) and tried pasting it on top of the other image:
from PIL import Image
background = Image.open("hahn_echo_1.png")
foreground = Image.open("overlay_step_3.png")
background.paste(foreground, (0, 0), foreground)
background.save("abc.png")
However, instead of displaying a nice black text on top, I get this:
overlay.png looks like this in Gimp:
So I would expect some nice and black text instead of this colorful mess.
Any ideas? Some PIL option I am missing?
As vrs pointed out above, using alpha_composite like this answer: How to merge a transparent png image with another image using PIL
does the trick. Make sure to have the images in the correct mode (RGBA).
Complete solution:
from PIL import Image
background = Image.open("hahn_echo_1.png").convert("RGBA")
foreground = Image.open("overlay_step_3.png").convert("RGBA")
print(background.mode)
print(foreground.mode)
Image.alpha_composite(background, foreground).save("abc.png")
Result:

Python AttributeError: 'NoneType' object has no attribute 'save' when trying to save a cropped image

I am trying to make a portion of an image blurred. Eventually I want to blur faces, but I cannot get just a portion to blur. I am attempting to crop a portion of an image then paste it back onto the original image. I am able to crop it on, but when I go to save the image with the cropped region pasted on, I receive an "AttributeError: 'NoneType' object has no attribute 'save'"
Here is the code I am using:
import Image, ImageFilter
picture = Image.open("picture1.jpg")
#finds width and height of picture
width, height = picture.size
#crops the picture
box = (20, 20, width/2, height/2)
ic = picture.crop(box)
#blurs the cropped part of the picture
ic = ic.filter(ImageFilter.GaussianBlur(radius=20))
#pastes the image back
blurredPic = picture.paste(ic, box)
#saves the new image and the cropped image
blurredPic.save("BlurredPic.jpg")
ic.save("cropPic.jpg")
I really appreciate the help.
picture.paste(ic, box) mutates picture in-place and returns None.
ie.
#blurs the cropped part of the picture
ic = ic.filter(ImageFilter.GaussianBlur(radius=20))
#pastes the image back
picture.paste(ic, box)
#saves the new image and the cropped image
picture.save("BlurredPic.jpg")
ic.save("cropPic.jpg")
I'm not sure if this is the library you're using, but I found some examples for the Image library here: http://www.riisen.dk/dop/pil.html. Looking at the Transpose example, it looks like the paste function is in place.
When you call picture.paste(ic, box), what you are doing is updating picture, and not returning anything. As a result, blurredPic has the value of None, which naturally doesn't have the attribute "save".
I would change the last 3 lines to be this:
picture.paste(ic, box)
#saves the new image and the cropped image
picture.save("BlurredPic.jpg")
ic.save("cropPic.jpg")
If that's not the correct library, or that doesn't solve your problem, let me know.

PIL Image, cut from one image to another

I'm trying to cut a section from one image and paste it into another. I'm using the following code:
img1 = Image.open("Img1.png")
img2 = Image.open("Img2.png")
cut = img2.crop((31, 0, 47, 15))
cut.save("cut.png")
img1.paste(cut, (150,300))
img1.save("NewImg1.png")
When I look at cut.png I see the right part cut out. However when it's pasted into img1, the resulting image just has a black box where the pasted section should be.
I suspect this may more be an issue with my images than the code, (both were created in Paint.NET, saved with default options) but I don't really know where to look right now to figure out the problem.

Transparent PNG in PIL turns out not to be transparent

I have been hitting my head against the wall for a while with this, so maybe someone out there can help.
I'm using PIL to open a PNG with transparent background and some random black scribbles, and trying to put it on top of another PNG (with no transparency), then save it to a third file.
It comes out all black at the end, which is irritating, because I didn't tell it to be black.
I've tested this with multiple proposed fixes from other posts. The image opens in RGBA format, and it's still messed up.
Also, this program is supposed to deal with all sorts of file formats, which is why I'm using PIL. Ironic that the first format I tried is all screwy.
Any help would be appreciated. Here's the code:
from PIL import Image
img = Image.open(basefile)
layer = Image.open(layerfile) # this file is the transparent one
print layer.mode # RGBA
img.paste(layer, (xoff, yoff)) # xoff and yoff are 0 in my tests
img.save(outfile)
I think what you want to use is the paste mask argument.
see the docs, (scroll down to paste)
from PIL import Image
img = Image.open(basefile)
layer = Image.open(layerfile) # this file is the transparent one
print layer.mode # RGBA
img.paste(layer, (xoff, yoff), mask=layer)
# the transparancy layer will be used as the mask
img.save(outfile)

Categories

Resources